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).

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 30 replies
  • 2896 views
  • 0 likes
  • 4 in conversation