BookmarkSubscribeRSS Feed
Patty
Calcite | Level 5

I am having trouble calculating age from from date;

Attached is the code for someone who's birthday is 19591207

IF YEAR_TERM EQ '20144' THEN DO;             v;

  CCYY = YEAR(BRTHDATE_ERSS);

  MM = MONTH(BRTHDATE_ERSS);

  DD = DAY(BRTHDATE_ERSS);

  BIRTHDATE = (CCYY*10000)+(MM*100)+DD;

  IF MM LT 10 THEN AGE = 2014-CCYY;

  IF MM EQ 10 THEN DO; 

  IF DD LE 10 THEN AGE = 2014-CCYY;      

  IF DD GT 10 THEN AGE = 2013-CCYY; 

  END;

  IF MM GT 10 THEN AGE = 2013-CCYY;     ;

  END;

Any help would be appreciated, Patty

4 REPLIES 4
Patty
Calcite | Level 5

Sorry, I should have been more clear, the birth date above was an example of the format. I have over 20,000 dates.

I am getting the error messages with all the birth dates dates including the one mentioned above:

NOTE: Invalid argument to function YEAR(19591207) at line 9224 column 16.

NOTE: Invalid argument to function MONTH(19591207) at line 9225 column 14.

NOTE: Invalid argument to function DAY(19591207) at line 9226 column 14.

Patrick
Opal | Level 21

Functions like "year()" require a SAS Date value as input. A SAS Date value is the count of days since 1/1/1960. You need to convert your "dates" to a SAS Date value. See code below.

data test;

  date_as_number=19591207;

  date_as_SASDate_value=input(put(date_as_number,8.),yymmdd8.);

  year_part=YEAR(date_as_SASDate_value);

  age=yrdif(date_as_SASDate_value,today(),'age');

  format date_as_SASDate_value date9.;

  output;

  stop;

run;

proc print data=test;

run;

ballardw
Super User


The largest date value SAS currently will accept (at least in 9.2) is 6589335 corresponding to 31 Dec 20,000 (yes 5 digit year!)

Using 19591207 would be about the year 50,000.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1436 views
  • 0 likes
  • 4 in conversation