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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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