Hello @SASPreK
This is my humble opinion to your question based on your initial post.
(Often SAS provides many ways of doing the same thing. I am showing how I would do it.)
You need to tell SAS that you are reading date values and their format by using the informat statement.
This was not done in your code.
Optionally tell SAS how to store the date values by using the format statement.
Once done the age can be calculated. Add 'AGE" option to yrdiff function to tell that you are calculating age. Look here for details on yrdiff.
Your updated code will be as follows
data have;
informat dob sample_dt yymmdd8.;
format dob sample_dt yymmddn8.;
input dob sample_dt;
age=yrdif(dob, sample_dt, 'AGE');
datalines;
19650214 20100429
19800724 20210823
19991208 20090908
;
run;
The output will be as follows
@Sajid01 Two quick corrections.
You said:
You need to tell SAS that you are reading date values and their format by using the informat statement.
This was not done in your code.
Optionally tell SAS how to store the date values by using the format statement.
You need to tell SAS how to read the date values by either attaching an informat to the variable with an INFORMAT statement or specifying an informat in the INPUT statement.
You can tell SAS how to display the date value by attaching a format to the variable.
The TYPE of the variable (SAS only has two variable types: fixed length character strings and floating point numbers) is what determines how SAS stores the values. The INFORMAT and FORMAT are used for converting the values from or to text strings.
Also you picture seems to show DOB as character, since the name is left aligned above the column. When I run your data step the resulting dataset prints like this:
Notice how all three variables have the column header and values right aligned.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.