BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
akimme
Obsidian | Level 7

Hi, I've tried several ways to calculate age and none of them have worked. Any advice?

 

Spoiler
*Data set HOSP;
data hosp;
   do j = 1 to 1000;
   AdmitDate = int(ranuni(1234)*1200 + 15500);
   quarter = intck('qtr','01jan2002'd,AdmitDate);
   do i = 1 to quarter;
   if ranuni(0) lt .1 and weekday(AdmitDate) eq 1 then
   AdmitDate = AdmitDate + 1;
   if ranuni(0) lt .1 and weekday(AdmitDate) eq 7 then
   AdmitDate = AdmitDate - int(3*ranuni(0) + 1);
   DOB = int(25000*Ranuni(0) + '01jan1920'd);
   DischrDate = AdmitDate + abs(10*rannor(0) + 1);
   Subject + 1;
   output;
   end;
   end;
   drop i j;
   format AdmitDate DOB DischrDate mmddyy10.;
   run;
   
   data hosp2; *creating copy;
   SET HOSP;
   run;
  
   
   data HOSP2; 
    today=03102021; *TODAY attempt 1;
    tday=put(td,date.);*TODAY attempt 2;
    format today mmddyy10.;
   AgeToday = (tday-DOB)/365.25; *Age attempt 1;
   AgeJan1 = yrdif(DOB, Tday, 'actual');*Age attempt 2;
run;

data HOSP2; *Age attempt 3;
   sdate=DOB;
   edate='01012006'd;
   age=yrdif(sdate, edate, 'AgeJan1');
   put AgeJan1= 'years';
   today=DAY();
   age=yrdif(sdate, today, 'AgeToday');
   put AgeToday= 'years';
run;
1 ACCEPTED SOLUTION

Accepted Solutions
sbxkoenk
SAS Super FREQ

Hello,

 

data _NULL_;
 myage = INTCK('YEAR','22FEB1970'd,today(),'CONTINUOUS');
 put myage=;
run;

CONTINUOUS method

specifies that continuous time is measured. The interval is shifted based on the starting date.

The continuous method is useful for calculating anniversaries.

 

[EDIT] : See also here
Sample 24574: Calculate the number of years, months, and days between two dates
https://support.sas.com/kb/24/574.html

 

Kind regards,

Koen

View solution in original post

4 REPLIES 4
sbxkoenk
SAS Super FREQ

Hello,

 

data _NULL_;
 myage = INTCK('YEAR','22FEB1970'd,today(),'CONTINUOUS');
 put myage=;
run;

CONTINUOUS method

specifies that continuous time is measured. The interval is shifted based on the starting date.

The continuous method is useful for calculating anniversaries.

 

[EDIT] : See also here
Sample 24574: Calculate the number of years, months, and days between two dates
https://support.sas.com/kb/24/574.html

 

Kind regards,

Koen

Tom
Super User Tom
Super User

There is a lot of other confusing code in there that does not seem to have anything to do with calculating AGE.

To calculate an AGE you need two dates.  Date of Birth and the date that you want to know the age.

You also need to define AGE.  Do you mean age as used by American's for deciding whether you can buy alcohol?  Or do you want a continuous measure with a fraction of years included?

data test;
   input dob date9.;
   today=date();
   format dob today yymmdd10.;
   age=intck('year',dob,today,'cont');
   age2=(today-dob)/365.25;
   age3=yrdif(dob,today,'act/act');
   age4=yrdif(dob,today,'act/360');
   age5=yrdif(dob,today,'act/365');
cards;
02OCt2000
03oct2000
04oct2000
01jan2021
;

Remember when specifying a date literal the value inside the quotes has to be something the DATE informat will understand.

"03OCT2021"d
'3-oct-21'd
"3 oct 2021"d

 

ballardw
Super User

Supply examples of the "didn't work".

Since you are showing code that creates random numbers that you treat as "date" values in the INTCK function, how do you know what to expect? Then you change the value of the randomly created variable in a random fashion so you have no clue what the first value of the variable Admitdate was when created.

You use stuff like this:

  if ranuni(0) lt .1 and weekday(AdmitDate) eq 1 then
   AdmitDate = AdmitDate + 1;

Multiple times. Then create another "date" by adding another random value from a normal distribution to create DischargeDate.

 

Then you use

    today=03102021; 

Which is a number that would translate to 18Jan in the year 10453. Your code has date literals correctly in several places. Why did you think this would be a valid date?

If you want the date for today use the TODAY () function.

 

And in this "example"

data HOSP2; *Age attempt 3;
   sdate=DOB;
   edate='01012006'd;
   age=yrdif(sdate, edate, 'AgeJan1');
   put AgeJan1= 'years';
   today=DAY();
   age=yrdif(sdate, today, 'AgeToday');
   put AgeToday= 'years';
run;

Where is DOB supposed to get a value to apply to SDATE? There is no data source. Plus the date literal is incorrect again. Data literals are in the form as the

'01jan2002'd

that you used. The reason you can't use just digits is because there are so many forms of dates out there.

 

 

Plus Doesn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the "<>" to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the "<>" icon or attached as text to show exactly what you have and that we can test code against.

 

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 4 replies
  • 629 views
  • 1 like
  • 5 in conversation