Proper use of date informat/format

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

Proper use of date informat/format

Let me preface by stating I'm new to SAS.

 

I'm having quite a bit of trouble with my dates. Format for input file is mm/dd/yyyy; however, if I've found that if I use anything OTHER THAN the informat FL_DATE yymmdd10. and format FL_DATE mmddyy10. combination, I get an error message.

 

So, great! I got the dates to appear in the file in the format I wanted: 

 

 

Capture.PNG

 

....but now when I try to do a subset of those with the date >=11/22/15 I'm getting errors, too.

 

The statement below isn't working - it bring back ALL data for November. I tried putting quotes around the dates, and I get "ERROR: WHERE clause operator requires compatible variables."  I suspect the issue is with my informat/format, but I'm not entirely sure.  This is driving me batty!

 

data HolidayWeeks ;
set Combined(where=(FL_DATE>=11/22/15));
run;


Accepted Solutions
Solution
‎11-02-2016 10:56 PM
Super User
Posts: 5,503

Re: Proper use of date informat/format

Welcome aboard the SAS train.  In working with dates, I'm afraid you didn't pick the easiest place to begin.

 

First, back to the original problelm ... you do need the informat of mmddyy10., but the format can be any valid date format.

 

To refer to a particular day, you need a different syntax entirely.  This would do the trick:

 

set Combined(where=(FL_DATE>='22Nov2015'd));

 

When you used 11/22/15 (with no quotes), the slashes were interpreted as division.  So you were comparing dates to a fractional value between 0 and 1.  That's why all the observations were being selected.

 

 

View solution in original post


All Replies
Solution
‎11-02-2016 10:56 PM
Super User
Posts: 5,503

Re: Proper use of date informat/format

Welcome aboard the SAS train.  In working with dates, I'm afraid you didn't pick the easiest place to begin.

 

First, back to the original problelm ... you do need the informat of mmddyy10., but the format can be any valid date format.

 

To refer to a particular day, you need a different syntax entirely.  This would do the trick:

 

set Combined(where=(FL_DATE>='22Nov2015'd));

 

When you used 11/22/15 (with no quotes), the slashes were interpreted as division.  So you were comparing dates to a fractional value between 0 and 1.  That's why all the observations were being selected.

 

 

Occasional Contributor
Posts: 13

Re: Proper use of date informat/format

Posted in reply to Astounding
Thank you!! Can't tell you the amount of time I spent trying to figure out what I did wrong!
Super User
Posts: 11,343

Re: Proper use of date informat/format

At expand a little on @Astounding's response: When you provide a value for a date you are using what is referred to as a date literal. The only format SAS will understand for a date literal is ddMONyy or ddMONyyyy enclosed in quotes and ending in d to tell SAS the value is supposed to be a date. "11/02/2012"d would not be acceptable but is commonly attempted. There are similar rules for Time or Datetime variables.

 

And do not give up on date values! The formats alone are very powerful in that a format can be used to create different "bins" or groups on the fly for analysis or reporting procedures.

 

Try runing proc freq on your data set for tha date variable with your current defined format.

The try

Proc freq data=combined;

   tables FL_DATE;

   format Fl_date yymon7.,

and again with Format FL_DATE year4.; of Format FL_DATE yyqr. ;

 

And there are several functions available such and INTNX and INTCK for incrementing or determining intervals between dates.

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 283 views
  • 1 like
  • 3 in conversation