BookmarkSubscribeRSS Feed
Smitha9
Fluorite | Level 6

Changing the data format

Example:

Date

19431010

Change Date to:

10/10/1943(mm/dd/yyyy)

1 REPLY 1
Tom
Super User Tom
Super User

SAS only has two types of variables, floating point numbers and fixed length character strings.

Which type is the source variable?  What type do you want to create?

Note that SAS stores date values as numbers, the number of days since 1960.

So to convert the string '19431010' to a date use the INPUT() function with the YYMMDD8. informat.

real_date = input(date,yymmdd8.);

To convert the number 19,431,010 to date first convert it to a string using the PUT() function and the Z8. format.  Then use the same method as above to convert string to a date.

real_date = input(put(date,z8.),yymmdd8.);

Once you have a date value you can then use any date type format with it you want so that the number of days prints as a string that humans can understand.  Do make it print like MM/DD/YYYY use the MMDDYY10. format.

format real_date mmddyy10.;

If you want to create a character variable instead then use the PUT() function to convert the date value to a string.

date_string=put(real_date,mmddyy10.);

PS Do not display dates in either MDY or DMY order as either choice will confuse half of your audience.  Better to use DATE or YYMMDD format so that users do not mistake October Twelfth for the Tenth of December.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 1 reply
  • 185 views
  • 0 likes
  • 2 in conversation