SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
ybz12003
Rhodochrosite | Level 12

Hello:

 

I have a code below to compute the age.   I am trying to figure out what this codes are for. 

 

%macro age(date,birth);
	floor ((intck('month',&birth,&date)- (day(&date) < day(&birth))) / 12) ;
%mend age;

	Age_calc = %age(screendate, DOB);
age = round(Age_calc);

 

Or better codes to do this? 

3 REPLIES 3
ballardw
Super User

Depends somewhat on how you want the age but the typical use of age can be

 

age = floor ( yrdif (DOB, comparedate, 'AGE'));

 

if you want an integer age. Without the the FLOOR function to round down you get a decimal value.

 

 

The current macro, which has been around a long time it is calculating the months between two dates with the INTCK function and then if the day of the month is before the day of month of birth subtracting one month, then divides that by 12 to get approximate year and truncates that result to remove the decimal portion by rounding down.

PGStats
Opal | Level 21

No reason to define a macro anymore, you might as well call the INTCK function. The way to calculate the exact age with function INTCK is

 

intck("YEAR", birth, date, "CONTINUOUS");
PG
FreelanceReinh
Jade | Level 19

Note that the three methods suggested yield different results if DOB was a February 29 and DATE is a February 28:

data _null_;
dob='29FEB2000'd;
do date='28FEB2004'd, '28FEB2005'd;
  agemc=%age(date, dob);
  ageyd=floor(yrdif(dob, date, 'age'));
  ageic=intck('year', dob, date, 'c');
  put (d:)(=date9.) (a:)(=);
end;
run;
dob=29FEB2000 date=28FEB2004 agemc=3 ageyd=4 ageic=3
dob=29FEB2000 date=28FEB2005 agemc=4 ageyd=5 ageic=5

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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