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!
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;
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;
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.
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
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.