DATA Step, Macro, Functions and more

Comparing dates in SAS

Accepted Solution Solved
Reply
Regular Contributor
Posts: 216
Accepted Solution

Comparing dates in SAS

Hi

I am trying to determine if the dates in a variable are GE a specific date value and LE another date value (in bold). I have tried variations of the below and keep getting errors. Any syntax ideas?

data s1suffolkpre;

set s1;

if cnty_name="Suffolk";

format start mmddyy10.;

format stop mmddyy10.;

format filing1 mmddyy10.;

if filing1 ge '01/01/2008'd and filing1 le '12/31/2009'd;

end;

Paul


Accepted Solutions
Solution
‎06-13-2014 09:48 AM
Super Contributor
Posts: 578

Re: Comparing dates in SAS

You might try using '01Jan2008'd and '31Dec2009'd...if that doesn't work...you'll probably need to post some examples and the error message.

View solution in original post


All Replies
Solution
‎06-13-2014 09:48 AM
Super Contributor
Posts: 578

Re: Comparing dates in SAS

You might try using '01Jan2008'd and '31Dec2009'd...if that doesn't work...you'll probably need to post some examples and the error message.

Super Contributor
Posts: 418

Re: Comparing dates in SAS

Haha the general syntax for a date delimiter in sas HAS TO BE

DD Mon YYYY (no spaces).  Note I say Mon, meaning the character representation of the month.

so

05/01/2011 would HAVE to be 05Jan2001.

Sas does this because in different countries in the world, there is NOT a consistent way to format dates. Example in the US dates are in  MM/DD/YYYY, however in many parts of europe it is DD/MM/YYYY.

SO the field 05/01/2011 means May 1st 2011 in the US, however it means January 5th 2011 in other parts of the world.

Hope that helps!

Regular Contributor
Posts: 216

Re: Comparing dates in SAS

Thank you both--that worked. However, my format statements preceding the comparison statement both reference a date format of mm/dd/yyyy (format filing1 mmddyy10.Smiley Wink. This apparently is not an issue?

Paul

Super Contributor
Posts: 418

Re: Comparing dates in SAS

Short answer No.

Longer answer; Do a bit more research into Dates in Sas, and the date literal (that's what you're doing when you call 'DDMonYYYY'd.

A FORMAT statement only affects the way the data is displayed, not how it is handled by the system. Remember all "dates" in sas are really numbers (they're the number of days between 1/1/1960 and your input date).  Try to format one of your dates as best12. to see what I mean.

As such, you're basically telling sas "show my dates in the format mmddyyyy10.", however they're REALLY a number.

WHen you do the date literal of "01Jan2008"d you're telling sas "I DON'T KNOW the numeric value for the date January 1st 2008, so please take this input string and convert it to that number for me". Then compare THAT NUMBER against my date field, and apply my other logic.

You can format a date ANY way you want, but it doesn't change how sas "handles" the date, only how it displays it.

Hope that helps

Regular Contributor
Posts: 216

Re: Comparing dates in SAS

Posted in reply to Anotherdream

Yes, that's fine. I didn't know dates were just a display. Thanks a lot (again)!

Paul

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 3121 views
  • 6 likes
  • 3 in conversation