Hi Experts,
I have a numeric DOB variable that I would like to use to generate an Age variable. I am not sure how to get started. I would like to use the new age variable to generate descriptive statistics on age etc.
Any advice would be greatly appreciated.
Here is what my data looks like:
DOB |
04/13/1950 |
09/12/1965 |
03/12/1960 |
05/17/1952 |
Here is what I would like to create:
Age (years) |
72.2 |
... |
... |
... |
Here is what my code looks like: The DOB variable is already in numeric format.
PROC IMPORT OUT = DEMO
DATAFILE= "M:\SAS\BI EYE-DMI\XLS Data Files/DM.xlsx"
DBMS = xlsx;
RUN;
/* create overall variable */
DATA DEMO2;
SET DEMO;
OVERALL = "Overall";
RUN;
TITLE "Subject AGE Overall - converting date of birth to age";
PROC MEANS DATA=WORK.DEMO2 MEAN STDDEV MIN MAX KURT SKEW;
CLASS OVERALL;
VAR DOB;
RUN;
Thank you in advance.
T.
Hi:
To chime in with a contribution once you provide some more context for what you want as the age (as of today, as of Jan 1, etc), you also need to test the various ways you can calculate age. I am a fan for simple age of subtraction, plus division, as shown in the program below for the AGE_TODAY variable, which uses the today() function to get today's date and then subtracts the internal value for DOB. Then the resulting number of days is how many days old -- which still needs to have a calculation done to get fractional years old for the age. I use 365.25 as the denominator, to account for leap year every 4 years and the age that comes out is fairly close. The INTCK function provides an alternate way to provide interval checking between 2 dates. But if you pick years as the interval, then it will count whole years. So depending on what you want/need, you can get different values for age:
Cynthia
Some approaches are outlined in the links here:
https://blogs.sas.com/content/sasdummy/2011/07/12/computing-age-in-sas-9-3/
https://blogs.sas.com/content/iml/2017/05/15/intck-intnx-intervals-sas.html
Age as of today? Age as of some other date?
Hi:
To chime in with a contribution once you provide some more context for what you want as the age (as of today, as of Jan 1, etc), you also need to test the various ways you can calculate age. I am a fan for simple age of subtraction, plus division, as shown in the program below for the AGE_TODAY variable, which uses the today() function to get today's date and then subtracts the internal value for DOB. Then the resulting number of days is how many days old -- which still needs to have a calculation done to get fractional years old for the age. I use 365.25 as the denominator, to account for leap year every 4 years and the age that comes out is fairly close. The INTCK function provides an alternate way to provide interval checking between 2 dates. But if you pick years as the interval, then it will count whole years. So depending on what you want/need, you can get different values for age:
Cynthia
Hi Cyntia,
I tried out the code and here is what I am getting. What I am not sure about is what I should put in the infile section since I have more than 266 subject datalines that I would like to have DOB converted for. I would like to see all options as well (today, Jan 1, and intck).
Here is what my SAS code looks like:
DATA WORK.DEMO2;
SET DEMO;
INPUT NAME $ DOB : MMDDYY.;
AGE_TODAY = ((today()-DOB)/365.25);
AGE_JAN1 = (('01JAN2022'D - DOB)/365.25);
AGE_INTCK = INTCK('YEAR', DOB, TODAY(), 'CONTINUOUS');
RETURN;
RUN;
Here is what my log looks like:
121 DATA WORK.DEMO2; 122 SET DEMO; 123 INPUT NAME $ DOB : MMDDYY.; 124 AGE_TODAY = ((today()-DOB)/365.25); 125 AGE_JAN1 = (('01JAN2022'D - DOB)/365.25); 126 AGE_INTCK = INTCK('YEAR', DOB, TODAY(), 'CONTINUOUS'); 127 RETURN; 128 RUN; ERROR: No DATALINES or INFILE statement. NOTE: The SAS System stopped processing this step because of errors. WARNING: The data set WORK.DEMO2 may be incomplete. When this step was stopped there were 0 observations and 39 variables. WARNING: Data set WORK.DEMO2 was not replaced because this step was stopped. NOTE: DATA statement used (Total process time): real time 0.01 seconds cpu time 0.01 seconds
Any further assistance you could provide would be greatly appreciated.
Best wishes,
T.
SET is for reading from SAS datasets.
INPUT is for reading from text files.
So which do you have? You will not normally use both in the same data step.
@Tom and @Cynthia_sas - thank you both for your help. The revised code worked exactly how I was hoping.
DATA WORK.DEMO2; SET DEMO; AGE_TODAY = ((today()- DOB)/365.25); AGE_JAN1 = (('01JAN2022'D - DOB)/365.25); AGE_INTCK = INTCK('YEAR', DOB, TODAY(), 'CONTINUOUS'); RUN;
Thank you Thank you.
T.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.