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
DOB | age |
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 |
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;
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?
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
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.
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.
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;
Many thanks.
My date is already in this format (mmddyy10), still I need to keep this code?
No need to use format in your program just use the given function statements.
If you have any decimal values use round(age) or rounde(age) to get integer values.
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.
please share your program
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.