Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you compare SAS date? 1

Status
Not open for further replies.

cosmid

Programmer
Feb 14, 2008
73
US
Hi,

I am trying to get all the data sets after 10/01/07. This is what I have:

Data new;
set old;
informat date mmddyy8.;
format date mmddyy8.;
length track $ 3;
track = 'No'; if date > '10/01/07' then track 'Yes';
run;

It says that '10/01/07' is not valid. What should be there? I googled all over the place and I can't find a solution. Thanks for help.
 
Ok. I kind solved the problem. But this wasn't what I was looking for. I mean, I got what I needed. I want to know the right way of doing this.

What I did is that I just got the raw number for 10/01/07 with the following code:

data test;
input date;
informat date mmddyy8.;
datalines;
10/01/07
run;

proc print data=test;
run;

And I got 17440 for 10/01/07 and I substituted 17440 for 10/01/07 for the above code.

track = 'No'; if date > 17440 then track 'Yes';

I just took out all the informats and formats. But what is the correct way, or the professional way of doing this?

Thanks.
 
A nice simple way to do this is as follows:-
Code:
Data new;
  set old;
  length track $ 3;
  track = 'No'; if date > '10JAN2007'd then track 'Yes';
run;

The 'd' after the quotes tells SAS that what was in the quotes was a date. You need to use that format for the date as well.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Ahh...I saw that on the website when I did my search. I thought that format is only for a particular date format and I was looking for mmddyy8. format.

Thanks a lot!
 
Sorry, I just want to make sure I got this right. Because you said I have to use that format as well. I am not sure what you mean when you say that. Here is the test code I wrote:

data old;
input date mmddyy6.;
datalines;
011007
102405
run;

Data new;
set old;
length track $ 3;
track = 'No'; if date > '10JAN2007'd then track = 'Yes';
run;

I used a different format just to see if it works. I have added removed formats and informats in both codes and they all come out to be the same. Of course, except the output for date in dataset new. Without a format, it just came out as the SAS number, like 17176.

I am not sure where to put informat and format in this situation. But it worked since I only want to create a variable track that will have a value of Yes if it is after a certain date and a value of No otherwise. All I know is that the format mmddyy6. was needed when it reads the raw data. If my objective is only to get a value for new variable named track, I don't need any more formats right?

Thanks. Sorry, my question is a little long. All the stuff I find on the website does not explain this.
 
Sorry, 102405 should be 102408. Just so you can get a different value for track.

I just got one more question. You can use '10JAN2007'd for comparisons on date regardless of the format of the date right?
 
Hi Cosmid, sorry, I was possibly a little unclear. In order to specify a date using that methodology, you have to specify the check as '01JAN1988'd for instance, as opposed to '01/01/1998'd.
The actual SAS date field you are comparing it to can be in any format you like.
Formats in SAS do not affect the data held in the field. In this case, the data itself is a number which is a count of the number of days since 01 January 1960. Whatever format you apply to that field, the underlying data will not change.
Here's a little test code for you:-
Code:
data _null_;
  date1 = '01JAN1960'd;
  date2 = '28DEC1959'd;
  date3 = '27OCT2008'd;
  date4 = '28OCT2008'd;

  put date1=;
  put date2=;
  put date3=;
  put date4=;

run;
As you can see, a date prior to 01JAN1960 gives a negative number.

Now, with formats:-
Code:
data _null_;
  date1 = '01JAN1960'd;
  date2 = '28DEC1959'd;
  date3 = '27OCT2008'd;
  date4 = '28OCT2008'd;

  put date1=;
  put date2=;
  put date3=;
  put date4=;

  format date1 date2 ddmmyy10. date3 date4 date9.;

run;
data _null_;
  date1 = '01JAN1960'd;
  date2 = '28DEC1959'd;
  date3 = '27OCT2008'd;
  date4 = '28OCT2008'd;

  put date1=;
  put date2=;
  put date3=;
  put date4=;

  format date1 date2 ddmmyy10. date3 date4 date9.;

run;
Ordinarily, I would put the format statement first as this is generally accepted I believe as best practice. However I wanted to demonstrate something here. FORMAT is a "non-executable" statement, it doesn't need to be compiled to run, therefore it is processed BEFORE anything else in the step runs, so even though it appears AFTER the put statements, it is actually processed BEFORE them.

I hope this helps.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Thanks Chris for the detailed tutorial! Really apprecaite your help and time!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top