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

I am looking for a solution to elegantly display age.

 

Say I have two datetime varoiables: DOB and a variable called School Entry, at which age were the children schooled ?

Time_in_Seconds = (School Entry - DOB) - what is the best way to convert this into age including years, months and days?

 

Often it is just displayed in years, but I would want a more exact approach, therefore something like : Tane is 12 years, 5 months and 4 days old.

 

I could create someting over multiple colums, whereby

age_min  =ROUND ((age_sec)/60);
age_h    =ROUND  ((age_sec)/3600);
age_d     =ROUND ((age_sec)/86400);
age_y   =ROUND ((age_sec)/31536000);

 

but that is not very elegant. Would anybody know a better way to deal with this?

many thanks

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

Try the INTCK function. This will adjust properly for issues like leap years, differing number of days in months for example.

 

 

years = intck('dtyear', DOB, SchoolEntry, 'C');

months = intck('dtmonth', DOB, SchoolEntry, 'C');

 

Check out the documentation here:

http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#p1md4mx2crz...

 

View solution in original post

3 REPLIES 3
SASKiwi
PROC Star

Try the INTCK function. This will adjust properly for issues like leap years, differing number of days in months for example.

 

 

years = intck('dtyear', DOB, SchoolEntry, 'C');

months = intck('dtmonth', DOB, SchoolEntry, 'C');

 

Check out the documentation here:

http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#p1md4mx2crz...

 

Shmuel
Garnet | Level 18

Asuming DIF is the difference in seconds then you may try

 

age = put(datepart(DIF),yymmdd10.);

BrunoMueller
SAS Super FREQ

Find below a sample program that calculates the age in years months and days.

 

The sample is based on a SAS date value

 

data ageCalc;
  infile cards dlm=",";
  input
    min_date : date9.
    max_date : date9.
  ;
  years    = intck('year',min_date,max_date,'c');
  months   = intck('month',intnx('year',min_date,years,'same'),max_date,'c');
  days     = intck('day',intnx('month',intnx('year',min_date,years,'same'),months,'same'),max_date,'c');
  format min_date max_date ddmmyyp10.;
cards;
10NOV1963,26sep2013
15mar1961,11oct2001
31jan2000,01mar2000
29feb2000,28feb2001
;

proc print data=ageCalc;
run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 842 views
  • 6 likes
  • 4 in conversation