BookmarkSubscribeRSS Feed
wajmsu
Obsidian | Level 7

Hi,

I want to get age out of the date of birth (DOB) as given below.  I have following questions/points:

1. How can I get today's date in SAS dataset?

2. How can I get age in years, weeks and days?

3. How can I round off if it comes in decimals?

Thanks

 

 

DOBage
12/22/1994 
12/5/1990 
12/21/1978 
12/27/1977 
11/28/1980 
4/26/1987 
6/5/1977 
10/8/1980 
3/23/1990 
8/3/1991 
06/01/198 
8/26/1982 
5/17/1990 
10/28/1985 
8/8/1981 
5/26/1995 
13 REPLIES 13
amadhavi41
Fluorite | Level 6

Hi,

 

Please use below code for your requirement.

INTCK() function will not give values in decimals.

 

data test;
input dob mmddyy10.;
format dob mmddyy10.;
cards;
12/22/1994
12/05/1990
12/21/1978
12/27/1977
;
run;

 

data test1;
set test;
format date mmddyy10.;
date=today(); /*Get today's date*/
agey=intck('year',dob,date); /*Get age in years*/
agem=intck('month',dob,date); /*Get age in months*/
agew=intck('week',dob,date); /*Get age in weeks*/
aged=intck('days',dob,date); /*Get age in days*/
run;

wajmsu
Obsidian | Level 7

Hi,

Thanks.  My response is similar to what I wrote to Jag; I have already entered data, how can I get my required output by writing codes instead of entering data. 

Also, INTCK() will not give decimals but I have seen that even the value is 0.9758 it would give 0 years; is that not wrong?

 

Paul_OldenKamp_org
Obsidian | Level 7

The first subject won't be 22 years old until 12/22/16 but the program results in an age of 22 now even though the subject is only 21.

 

The yrdif function can be used to correctly calculate age in years in 9.3 and 9.4 but that won't work for months or weeks.

 

One can read about Billy Kreuter's classic solution to the age calcualtion problem here:

http://www.pauldickman.com/teaching/sas/age.php

http://staff.washington.edu/billyk/TechTips_SC4Q98.pdf

 

Billy's code can be modified to get age in months or weeks.

 

Paul

Jagadishkatam
Amethyst | Level 16

we have today() to get today date, the belwo code give the age in years

 

data have;
input DOB: mmddyy10.;
age=yrdif(dob,today(),'age');
format dob date9.;
cards;
12/22/1994	 
12/5/1990	 
12/21/1978	 
12/27/1977	 
11/28/1980	 
4/26/1987	 
6/5/1977	 
10/8/1980	 
3/23/1990	 
8/3/1991	 
06/01/1988	 
8/26/1982	 
5/17/1990	 
10/28/1985	 
8/8/1981	 
5/26/1995
;
Thanks,
Jag
wajmsu
Obsidian | Level 7

Thanks. 

My data are already entered and it is a dataset with around a thousand rows.  Is there any way I do not have to enter the rahter I could get the required outcome by using the codes.

amadhavi41
Fluorite | Level 6

Use set statement for your created dataset.

 

data test1;
set test;
format date mmddyy10.;
date=today(); /*Get today's date*/
agey=intck('year',dob,date); /*Get age in years*/
agem=intck('month',dob,date); /*Get age in months*/
agew=intck('week',dob,date); /*Get age in weeks*/
aged=intck('days',dob,date); /*Get age in days*/
run;

wajmsu
Obsidian | Level 7

Many thanks. 

 

My date is already in this format (mmddyy10), still I need to keep this code?

amadhavi41
Fluorite | Level 6

No need to use format in your program just use the given function statements.

 

 

 

amadhavi41
Fluorite | Level 6

If you have any decimal values use round(age) or rounde(age) to get integer values.

wajmsu
Obsidian | Level 7

I ran the code and it gives todays date and also creats columns for all categories for age but it does not calculate the age and cells/data are blank or missing.

amadhavi41
Fluorite | Level 6

please share your program

amadhavi41
Fluorite | Level 6

dob
date
agey
agem
agew
aged
1 12/22/1994 03/11/2016 22 255 1107 7750
2 12/05/1990 03/11/2016 26 303 1318 9228
3 12/21/1978 03/11/2016 38 447 1942 13595
4 12/27/1977 03/11/2016 39 459 1993 13954

I just checked in SAS the above result you will get.
amadhavi41
Fluorite | Level 6
obs dob date agey agem agew aged

1 12/22/1994 03/11/2016 22 255 1107 7750
2 12/05/1990 03/11/2016 26 303 1318 9228
3 12/21/1978 03/11/2016 38 447 1942 13595
4 12/27/1977 03/11/2016 39 459 1993 13954

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
  • 13 replies
  • 3907 views
  • 0 likes
  • 4 in conversation