BookmarkSubscribeRSS Feed
Patrick
Opal | Level 21

"If I wanted to change to code to recognise dates as integers instead of SAS date values. How do I do I edit the code? As a lot of the dates I address are numbers not sas dates. Thanks for your input."

SAS has exactly 2 data types: Numeric and Character. A SAS date value is stored in a numeric variable. A SAS date value is the number of days since 1 January 1960. In storing dates this way one can easily handle them (eg. if you want the date a day later you simply add +1 to your variable). There are also a number of calendar functions which allow for simple date handling (eg. same date of month 6 months later: intnx('month',date,6,'s')  ). You find all these functions in the Base SAS documentation.

It's a bad idea to store dates as numbers like 20120522 as this doesn't allow you to do all these calculations and to use all these ready made calendar functions. What you do is apply a format to the numeric SAS variable containing a SAS date value (days since 1Jan1960). So if you want to look at your date in the form YYYYMMDD you just apply "format date yymmddn8.". All these formats are also documented in the Base SAS reference doc under Formats.

It's really worth to read up on how SAS deals with dates and it's the way you should do it. Every other way is sub-optimal and you will understand once you get how SAS handles dates. And by the way: It's not only SAS dealing in this way with dates - every programing language/database I know does it in a similar manner.

And just to add to the above:

Using a SAS date value it's really simple to select a date 6 months ago as I've done it the SQL I've posted to solve your problem. This would have been quite a bit harder when using a date string (no matter if this date string is now stored as an integer in a numeric variable or as a string in a character variable).

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 30 replies
  • 1419 views
  • 0 likes
  • 4 in conversation