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

At first I thought there was a bug in SAS!  Later I realized it was a problem with one of my "if" conditions.  It was actually an issue of me not realizing that '01JAN1960' has a value of 0.

 

Let's say I want to calculate the age for some records, given a birthdate field that is usually missing.

Here is a sample data set:

data sample;
id='1';birthdate='01JAN1970'd; output;
id='2';birthdate='02JAN1970'd;output;
id='3';birthdate=.;output;
id='4';birthdate='01JAN1960'd;output;
run;

/*In a second data step, I calculate the ages:*/

data ages;
set sample;
if birthdate then age = floor((intck('month',birthdate,today()) - (day(today()) < day(birthdate))) / 12);
run;

 

Do you see the problem?  The intention is to calculate the age only if the birthdate is not missing.  However, if the birthdate is on January 1, 1960, then the value is 0, which is a numeric false.  The "if" resolves to false, so the "then" part of the statement doesn't get executed.

 

The "if" statement should read as follows:

  if birthdate ne . then age = floor((intck('month',birthdate,today()) - (day(today()) < day(birthdate))) / 12);

 

It was one of those gotcha moments.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
if not missing(birthdate) then age = intck('year',birthdate,today(),'c');
--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26
if not missing(birthdate) then age = intck('year',birthdate,today(),'c');
--
Paige Miller
ballardw
Super User

Might want to look at the YRDIF function with the Floor function if you don't need a decimal part of a year.

TCL
Calcite | Level 5 TCL
Calcite | Level 5

I'm slightly curious as to why SAS doesn't create an "age" function, since it's something that is often computed.  The "YRDIF" function doesn't immediately come to mind.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 329 views
  • 2 likes
  • 3 in conversation