BookmarkSubscribeRSS Feed
kngu022
Obsidian | Level 7

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;

Screen Shot 2020-01-24 at 3.50.20 AM.png

Can anyone tell me what 's wrong with my code? 

8 REPLIES 8
novinosrin
Tourmaline | Level 20

Plz check the parenthesis

 

Here is a correction

	age=INT((MDY(1,1,2008)-bday)/365.25);
data_null__
Jade | Level 19

 

Order of operations.

 

 

age=INT((MDY(1,1,2008)-bday)/365.25);
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
AhmedAl_Attar
Ammonite | Level 13

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

kngu022
Obsidian | Level 7

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;

Screen Shot 2020-01-24 at 4.39.56 AM.png

PaigeMiller
Diamond | Level 26

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 */

 

--
Paige Miller
Patrick
Opal | Level 21

@kngu022 

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;
Tom
Super User Tom
Super User

Your code is printing a different dataset than the data step is creating.

DATA dates;
...
PROC PRINT DATA=Date;