I am creating the new variable - date. I am not too sure what is wrong when the age comes out quite strange.
DATA Date; INPUT month 1-2 year 4-7 day 9-10; bday=MDY(month,day,year); age=INT(MDY(1,1,2008)-bday/365.25); CARDS; 6 1918 12 7 1948 11 1 1960 1 10 1970 15 12 1971 10 ; RUN; ODS RTF FILE="/folders/myfolders/Lecture_ Resources/Chapter4/Chap4p25"; PROC PRINT DATA=Date; FORMAT bday date9. age; RUN; ODS RTF CLOSE;
Can anyone tell me what 's wrong with my code?
Plz check the parenthesis
Here is a correction
age=INT((MDY(1,1,2008)-bday)/365.25);
Order of operations.
age=INT((MDY(1,1,2008)-bday)/365.25);
You have made a math error, which extra parentheses will fix.
age=INT((MDY(1,1,2008)-bday)/365.25);
By the way, a better method of accounting for leap years is the INTCK function, or the YRDIF function.
Try this data step
DATA Date;
INPUT month 1-2 year 4-7 day 9-10;
age=intck('year', MDY(month,day,year), MDY(1,1,2008));
CARDS;
6 1918 12
7 1948 11
1 1960 1
10 1970 15
12 1971 10
;
RUN;
Hope this helps,
Ahmed
After changed the code as above, the result comes out still the same. I doubt that my code still has some errors. Could you please show
me please?
DATA dates; INPUT month 1-2 year 4-7 day 9-10; bday=MDY(month,day,year); age=intck('year', MDY(month,day,year), MDY(1,1,2008)); CARDS; 6 18 12 6 1918 12 7 1948 11 1 1960 1 10 1970 15 12 1971 10 ; RUN; ODS RTF FILE="/folders/myfolders/Lecture_ Resources/Chapter4/Chap4p25"; PROC PRINT DATA=Date; FORMAT bday date8. age; RUN; ODS RTF CLOSE;
When I run the code you just showed with INTCK, I get meaningful age values, and I do not get the values you show.
I believe you have committed a typing error, and your PROC PRINT should be
PROC PRINT DATA=Dates; /* <== note the S on the end is needed here */
In regards of calculating age: Why not use the YRDIF() function which specifically provides an argument to calculate the age.
From the docu found here:
Example 2: Calculating a Person’s Age You can calculate a person’s age by using three arguments in the YRDIF function. The third argument, basis, must have a value of AGE: data _null_; sdate='16oct1998'd; edate='16feb2010'd; age=yrdif(sdate, edate, 'AGE'); put age= 'years'; run;
Your code is printing a different dataset than the data step is creating.
DATA dates;
...
PROC PRINT DATA=Date;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.