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

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_0-1718047565759.png

 

Tom
Super User Tom
Super User

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

Tom_0-1718051534058.png

Notice how all three variables have the column header and values right aligned.

 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 16 replies
  • 1737 views
  • 8 likes
  • 5 in conversation