I’ll point out that this only works if today is after the user’s birthday. I would write it:
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
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.
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 (/).