BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
gwilli31
Calcite | Level 5

Hello, I've been trying to do this through trial and error for hours, please help! I need to calculate age from a date variable in the YYYYMMDD format (Ex: January 1, 1909 is 19090101). Here is what I have so far:

 

options yearcutoff=1900;

data birth;
set birth;
age= INT ((01/01/2008 - BENE_BIRTH_DT) / 365.25);
run;

 

The ages that SAS returns are obviously not accurate when I look at them using proc freq, but I don't know why it's not helping. Thanks in advance.

 

1 ACCEPTED SOLUTION

Accepted Solutions
r_behata
Barite | Level 11

Use YRDIF Function. As per SAS documentation "The YRDIF function can compute a person’s age. The first two arguments, start-date and end-date, are required. If the value of basis is AGE, then YRDIF computes the age. The age computation takes into account leap years. No other values for basis are valid when computing a person’s age."

 

The system option "yearcutoff" is required only if there are 2 digit years involved. in your case yearcutoff does not have any effect.

 

data _null_;
   sdate=input('19090101',yymmdd8.);;
  
   age=yrdif(sdate, today(), 'AGE');
   put age= 'years';
run;

 

View solution in original post

3 REPLIES 3
r_behata
Barite | Level 11

Use YRDIF Function. As per SAS documentation "The YRDIF function can compute a person’s age. The first two arguments, start-date and end-date, are required. If the value of basis is AGE, then YRDIF computes the age. The age computation takes into account leap years. No other values for basis are valid when computing a person’s age."

 

The system option "yearcutoff" is required only if there are 2 digit years involved. in your case yearcutoff does not have any effect.

 

data _null_;
   sdate=input('19090101',yymmdd8.);;
  
   age=yrdif(sdate, today(), 'AGE');
   put age= 'years';
run;

 

PaigeMiller
Diamond | Level 26

A couple of words on why your code didn't work.

 

First, the format of a SAS date value is irrelevant, it could be YYYYMMDD. or DATE9. or any other valid SAS date format.

 

Your code

 

age= INT ((01/01/2008 - BENE_BIRTH_DT) / 365.25);

fails because the 01/01/2008 is not a SAS date value. It actually has the value of 01 divided by 01 divided by 2008, which equals 4.98...x10-4, that's how SAS interprets the slash in numeric expressions. That is not a SAS date, and it is not what you want. SAS date values are integers, they are the number of days since 01JAN1960. You might be thinking how would anyone know the number of days between 01JAN1960 and 01JAN2008? That's what the next piece of code takes care of, it translates calendar information such as January 1, 2008 to something SAS understands.

 

age= INT (('01JAN2008'd - BENE_BIRTH_DT) / 365.25);

 

Note that in the above line of code, '01JAN2008'd is an actual SAS date, recognized by SAS, and usable by SAS because this is how human dates are translated to SAS dates. 01/01/2008 is not a SAS date and not usable as a SAS date. Also please note that the format of the text string '01JAN2008'd is required, with the date in quotes and a letter d after the second quote — except that lower case letters can be used (and I believe you can tell SAS to recognize other languages so JAN could be something else if you set the right option). But '01/01/2008'd will not be recognized, it must be 'DDMONYYYY'd.

--
Paige Miller
Kurt_Bremser
Super User

Another hint:

do NOT do this:

data birth;
set birth;

If anything untoward happens in that step, you have trashed your dataset and must recreate it from where you started. Work in steps and keep the intermediate results, and delete those only when the space is needed later; SAS itself will take care of anything in WORK when the session ends.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 6571 views
  • 1 like
  • 4 in conversation