1
votes

I am working on writing a SAS code and since I am new to SAS (Have worked on R all the time), I am having trouble understanding the date formats in SAS.

I Have a SAS data set Sales_yyyymm and I am creating a code that takes the user's input of a date value, and if Sales data exists for that date, I need to create a flag as 1 else as 0. Currently I am using this code -

%Let check_date = 20010120;

 Data A;
  Set B;
 If date=&check_date then Date_Flag = 1;
 else Date_Flag = 0;
 run;

date is the Date column in my SAS data set Sales_yyyymm and the values are like 20130129, 20110412, 20140120 etc.

But if I run this code, I get all my Date_Flag values as 0. The IF condition is being ignored and I am not sure how or why this is happening.

Any idea?

Thanks!

1
I suspect the date column is infact a date which is formatted to show as yyyymmdd i.e., date is not a 8digit integer. Check proc contents data =<your dataset>; run; output and report back the type & format of date columnuser1509107
And another thing - in SAS date literals are supposed to be "DDMMMYYYY"d format. This will matter if in fact date column is really stored as a date type column.user1509107
I just checked and found that date column is of type NUM and Len 4 and format YYMMDDN8.RHelp
Format in SAS is solely a way to print something that looks nice to human eyes - akin to in c++ using sprintf. It has no bearing on the underlying value nor is it permitted to be used in equalities (without some other function, like put, which is the direct equivalent of sprintf).Joe

1 Answers

3
votes

You need to read Understanding How SAS Handles Dates article to get how SAS internally stores a date and how arithmetic on date including comparisions are carried out.

In SAS, every date is a unique number on a number line. Dates before January 1, 1960, are negative numbers; those after January 1, 1960, are positive. Because SAS date values are numeric variables, you can sort them easily, determine time intervals, and use dates as constants, as arguments in SAS functions, or in calculations.

As I mentioned in the comments you really need to specify your date literals in "ddmmmyyyy"d format.

So, %Let check_date = 20010120; should be written as:

%Let check_date = 20JAN2001;

 Data A;
  Set B;
 If date="&check_date."d then Date_Flag = 1;
 else Date_Flag = 0;
 run;

20010120 translated into SAS date goes beyond the valid range SAS can handle. Ex: 2001012 - note there is no zero at the end, corresponds to "03AUG7438" - yes, that's year 7438!!

Whereas 14995 is the integer that SAS understands to be the date 20JAN2001