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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

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_sas_1-1655843954809.png

 

Cynthia

 

 

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

Age as of today? Age as of some other date?

--
Paige Miller
Cynthia_sas
SAS Super FREQ

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_sas_1-1655843954809.png

 

Cynthia

 

 

SAS_Novice22
Quartz | Level 8
Hi Cyntia,
This is exactly what I was looking for, thank you for the code and the excellent information. Thanks for providing the solution to my question.
Best wishes,
T.
SAS_Novice22
Quartz | Level 8

@Cynthia_sas 

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.

Tom
Super User Tom
Super User

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.

SAS_Novice22
Quartz | Level 8
That was what I was doing wrong. Thank you!! It worked like a charm.
SAS_Novice22
Quartz | Level 8

@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.

Cynthia_sas
SAS Super FREQ
Hi: As @Tom pointed out, you did not need the INFILE statement. Since you did not provide test data in the form of a program that someone could run, I made some FAKE data using the INFILE and INPUT statements -- using DATALINES as a section to provide the "raw" data. The relevant statements were the 3 assignment statements, which you would need to put into your program and adjust the new variable names as you need.
Cynthia
(and thanks, @Tom for jumping in with the solution)

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 9 replies
  • 818 views
  • 8 likes
  • 5 in conversation