# Calculate age

**URL:** <https://techcommunity.softwareag.com/t/calculate-age/260445>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural\
**Created:** [August 22, 2022, 11:09am UTC](https://techcommunity.softwareag.com/t/calculate-age/260445 "2022-08-22T11:09:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam85](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@Adam85](https://techcommunity.softwareag.com/u/Adam85)\
**Post date:** [August 22, 2022, 11:09am UTC](https://techcommunity.softwareag.com/t/calculate-age/260445/1 "2022-08-22T11:09:09Z")

</div>

Hello everyone,

I’m totally new with Natural and I made a PGM to calculate age but I have a Bug with move edited  
The PGM is:

```auto
define data
LOCAL
1 AGE-CALCULATION
  2 NAME (A10)
  2 YOB (N4)
  2 DATE-CURRENT (D) INIT <*DATX>
  2 DATE-AGE (N10)
  2 OUTPUT (N4)
end-define

MOVE EDITED DATE-CURRENT (EM=YYYY) TO DATE-AGE

INPUT NAME 
INPUT YOB

COMPUTE OUTPUT := DATE-AGE - YOB

WRITE 'Hello' NAME 
WRITE 'You are' OUTPUT 'Years old'

END

```

---

<div class="post-metadata">

**Author:** ![Ralph\_Zbrog](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/ralph_zbrog/32/78_2.png) [@Ralph\_Zbrog](https://techcommunity.softwareag.com/u/Ralph_Zbrog)\
**Post date:** [August 22, 2022, 12:17pm UTC](https://techcommunity.softwareag.com/t/calculate-age/260445/2 "2022-08-22T12:17:32Z")

</div>

The MOVE EDITED target must be an alpha field.

```auto
2 DATE-AGE-A (A4) 2 REDEFINE DATE-AGE-A
  3 DATE-AGE (N4)

```

```auto
MOVE EDITED DATE-CURRENT (EM=YYYY) TO DATE-AGE-A

```

---

<div class="post-metadata">

**Author:** ![Jerome\_LeBlanc](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/jerome_leblanc/32/111_2.png) [@Jerome\_LeBlanc](https://techcommunity.softwareag.com/u/Jerome_LeBlanc)\
**Post date:** [August 22, 2022, 2:35pm UTC](https://techcommunity.softwareag.com/t/calculate-age/260445/3 "2022-08-22T14:35:01Z")

</div>

I’ll point out that this only works if today is after the user’s birthday. I would write it:

```auto
DEFINE DATA LOCAL
1 TODAY (N8)
1 REDEFINE TODAY 
  2 YYYY (N4)
  2 MMDD (N4)
1 DOB (N8)
1 REDEFINE TODAY 
  2 YYYY (N4)
  2 MMDD (N4)
1 AGE (N4)
etc
END-DEFINE

MOVE *DATN TO TODAY
MOVE WORKER.DOB TO DOB /* this assumes the worker.dob is N8 yyyymmdd.

AGE = TODAY.YYYY - DOB.YYYY
IF TODAY.MMDD LT DOB.MMDD
  SUBTRACT 1 FROM AGE
END-IF

```

---

<div class="post-metadata">

**Author:** ![MICK.FITZPATRICK](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@MICK.FITZPATRICK](https://techcommunity.softwareag.com/u/MICK.FITZPATRICK)\
**Post date:** [August 22, 2022, 2:52pm UTC](https://techcommunity.softwareag.com/t/calculate-age/260445/4 "2022-08-22T14:52:40Z")

</div>

OK so I know it’s not Friday when this kind of thing is usually brought up but, adding one more complication, in some cultures you are considered 1 on the day of birth rather than our 1 on the first anniversary of birth.

Let’s factor that into the intellectual exercise.

Mick Fitzpatrick  
Expert Technology Consultant  
o: 630-944-1154  
c: 301-529-8022

---

<div class="post-metadata">

**Author:** ![Adam85](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@Adam85](https://techcommunity.softwareag.com/u/Adam85)\
**Post date:** [August 23, 2022, 1:46pm UTC](https://techcommunity.softwareag.com/t/calculate-age/260445/5 "2022-08-23T13:46:15Z")

</div>

Well Gentlemen many thanks for your support and quick replies.  
This is the final version of my Code, in Order to share our knowledge

```auto
define data
LOCAL
1 AGE-CALCULATION
  2 NAME (A10)
  2 YOB (N4)
  2 DATE-CURRENT (D) INIT <*DATX>
  2 DATE-AGE (A10)
  2 REDEFINE DATE-AGE
    3 DATE-AGE-YYYY (N4)
  2 OUTPUT (N4)
end-define

MOVE EDITED DATE-CURRENT (EMU=YYYY) TO DATE-AGE

INPUT NAME YOB

IF SUBSTR (DATE-AGE,1,4) IS (N4)
  COMPUTE OUTPUT := DATE-AGE-YYYY - YOB
ELSE
  WRITE 'FEHLER'
END-IF

WRITE 'HELLO' NAME 
WRITE 'YOU ARE' OUTPUT 'YEARS OLD'

END

```

---

<div class="post-metadata">

**Author:** ![Ralph\_Zbrog](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/ralph_zbrog/32/78_2.png) [@Ralph\_Zbrog](https://techcommunity.softwareag.com/u/Ralph_Zbrog)\
**Post date:** [August 23, 2022, 3:01pm UTC](https://techcommunity.softwareag.com/t/calculate-age/260445/6 "2022-08-23T15:01:12Z")

</div>

Pretty good for a first program. In the interest of knowledge sharing:

- The edit mask YYYY extracts 4 digits, so DATE-AGE should be defined as A4. If the IF statement was necessary (see below), you could eliminate the extra CPU for the SUBSTR clause.

- \*DATX is always a valid value, so DATE-CURRENT is always valid, and the edit mask YYYY will always extract 4 valid digits. The IF statement is unnecessary. It just wastes CPU.

- Because Natural is an interpreted run-time, as a rule, we try to reduce the number of executable statements, for performance. (Of course the trade-off is legibility.) You could replace the third WRITE keyword with a slash (/).

```auto
/ 'YOU ARE' OUTPUT 'YEARS OLD'

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [February 19, 2023, 3:02pm UTC](https://techcommunity.softwareag.com/t/calculate-age/260445/7 "2023-02-19T15:02:00Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
