Help using Base SAS procedures

Calculate current age

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Calculate current age

Hi, 
I am attempting to calculate the age of individuals in my data set using their date of birth and the date their clinical treatment started.  Following that I will be attempting to create age groups according to their age at their addmitance.

 

Please note that this data is over a period of time, and individuals can have been admitted on multiple occasions.

 

An example of the data:

 

clientid           dob              clstartdate

123                10435           18309

122                3982             19452

156               -13125           20065

189                11497           17875

192                10977           19912

 201               3133              20391

 

How would I go about calculating their age? 

 


Accepted Solutions
Solution
‎11-21-2017 04:06 AM
Super User
Posts: 10,259

Re: Calculate current age

Use the intck() function:

data have;
input clientid dob clstartdate;
cards;
123 10435 18309
122 3982 19452
156 -13125 20065
189 11497 17875
192 10977 19912
201 3133 20391
;
run;

data want;
set have;
age_at_clt = intck('year',dob,clstartdate);
run;

Or you could use a basic calculation:

age_at_clt = (clstartdate - dob) / 365.25;

Or, for special types of "age":

age_at_clt = year(clstartdate) - year(dob);

So it's up to you to define what "age" is supposed to be in your context.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
‎11-21-2017 04:06 AM
Super User
Posts: 10,259

Re: Calculate current age

Use the intck() function:

data have;
input clientid dob clstartdate;
cards;
123 10435 18309
122 3982 19452
156 -13125 20065
189 11497 17875
192 10977 19912
201 3133 20391
;
run;

data want;
set have;
age_at_clt = intck('year',dob,clstartdate);
run;

Or you could use a basic calculation:

age_at_clt = (clstartdate - dob) / 365.25;

Or, for special types of "age":

age_at_clt = year(clstartdate) - year(dob);

So it's up to you to define what "age" is supposed to be in your context.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,599

Re: Calculate current age

Posted in reply to KurtBremser

Those calculations you give Kurt, are they accurate, I know the 365.25 is just an approximation, but how about the others?  I know for a long time we used:

int((intck('month',<birth>,<start>)-(day(<birth>)> day(<start>))) /12)

To get the most accurate age reading, but maybe the functions like year now cover this?

Super User
Posts: 10,259

Re: Calculate current age

Those three calculations were just the proverbial "tip of the iceberg".

There are so many ways to go about it, and so much more ways to fine-tune those, that it will be up to the developer to determine which one to use in the end.

It comes down to try those methods and compare their results with the desired outcome, over a sufficient lot of test cases. Just defining the test cases can be a major challenge (and is often the most important part of developing, I'm a big supporter of test-driven development).

 

The 365.25 approximation should work reasonably well for timespans in the range of lifetimes. It depends if a "legal" age value is needed, or a value best suited for statistics.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 10,784

Re: Calculate current age

Posted in reply to KurtBremser

I would like to use 

 

age_at_clt = yrdif(dob,clstartdate,'age');

 

Occasional Contributor
Posts: 7

Re: Calculate current age

Posted in reply to KurtBremser
The intck expression did the trick.
Thanks!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 240 views
  • 3 likes
  • 4 in conversation