BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
A_Halps
Obsidian | Level 7

I am new to SAS and need to answer this question, but really don't know what to do. Please help!

 

- I have DOB and Hire Date for each employee. I need to provide SAS code to calculate the age at when they were hired?

 

I'm wondering if I should be using intck('dtday', DOB, Hire_date)? 

 

Attached is a sample dataset I am working with.

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
SuryaKiran
Meteorite | Level 14

Importing your data using Proc Import is reading dates as character values. If this is same for you then you need to convert them to Dates first and also your excel file column name are not valid when it comes to SAS. SAS will read as literal ( eg: 'Hire Date'n) This might cause issue downstream, so to avoid this you can use VALIDVARNAME=V7.

 

For your age you need to be careful about the Birth dates that fall on leap year. For precise age use the following: 

 

OPTIONS VALIDVARNAME=V7;
PROC IMPORT DATAFILE="E:\SASUniversityEdition\SandBox\Files\Q3_Dates.xlsx" 
	OUT=Q3_Dates
	DBMS=EXCEL
	REPLACE;
GETNAMES=YES;
QUIT;

DATA Q3_Dates_Age;
Format DOB_ HD mmddyy10.;
SET Q3_Dates;
DOB_DT=INPUT(strip(DOB),mmddyy10.);
Hire_DT=INPUT(Strip(Hire_Date),mmddyy10.);
age = FLOOR((intck("month", INPUT(strip(DOB),mmddyy10.),INPUT(Strip(Hire_Date),mmddyy10.))-
 (day(INPUT(Hire_Date,mmddyy10.)) < min(day(INPUT(DOB,mmddyy10.)), day(intnx ("month",INPUT(Hire_Date,mmddyy10.), 1)-1) )))/12); 
run;

 

Thanks,
Suryakiran

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20
data have;
infile cards truncover;
input Patient_Name & $20.	Hire_Date :mmddyy10.	DOB :mmddyy10.;
format Hire_Date 	DOB mmddyy10.;
cards;
Gil Hart	12/23/2009	03/10/2008
Walker Shields	01/12/2003	07/29/1978
Sarah Shelton	04/10/2007	01/21/1942
;

data want;
set have;
 age=int(yrdif(dob,hire_date, 'AGE'));
 run;
A_Halps
Obsidian | Level 7

That did not work for me. When I plug that in, the new 'Age' cell is blank. And, it only shows the patients names that were typed into the code.

novinosrin
Tourmaline | Level 20

are your dates, a numeric sas date variable/s. If yes that function is pretty straight forward and there isn't much I did besides knowing what the function does. So, i am hoping your sample that you posted is a good representative of your real and if that's the case it's mere sas function doing the job and no logic whatsoever from my part. 

 

So kindly check, verify ,and post us the correct sample, your log results and the output you got and your expected output please

 

For further reading here is the link to the function http://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#p1pmmr2dtec...

 

Please use the solution (2nd part) as test that on your real data. Forget the data have 1st part

SuryaKiran
Meteorite | Level 14

Importing your data using Proc Import is reading dates as character values. If this is same for you then you need to convert them to Dates first and also your excel file column name are not valid when it comes to SAS. SAS will read as literal ( eg: 'Hire Date'n) This might cause issue downstream, so to avoid this you can use VALIDVARNAME=V7.

 

For your age you need to be careful about the Birth dates that fall on leap year. For precise age use the following: 

 

OPTIONS VALIDVARNAME=V7;
PROC IMPORT DATAFILE="E:\SASUniversityEdition\SandBox\Files\Q3_Dates.xlsx" 
	OUT=Q3_Dates
	DBMS=EXCEL
	REPLACE;
GETNAMES=YES;
QUIT;

DATA Q3_Dates_Age;
Format DOB_ HD mmddyy10.;
SET Q3_Dates;
DOB_DT=INPUT(strip(DOB),mmddyy10.);
Hire_DT=INPUT(Strip(Hire_Date),mmddyy10.);
age = FLOOR((intck("month", INPUT(strip(DOB),mmddyy10.),INPUT(Strip(Hire_Date),mmddyy10.))-
 (day(INPUT(Hire_Date,mmddyy10.)) < min(day(INPUT(DOB,mmddyy10.)), day(intnx ("month",INPUT(Hire_Date,mmddyy10.), 1)-1) )))/12); 
run;

 

Thanks,
Suryakiran

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 4 replies
  • 12128 views
  • 4 likes
  • 3 in conversation