SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
EAAugustine77
Calcite | Level 5
I've succesfully converted a date9. format to mmddyy10. and I'm trying to qualify in a data step where the date variable = '01/01/2012' but when I do that I get the following error: Invalid numeric data, '01/01/2012' I added a 'd' to try to get it to see it as a date and not a character string but I received this error: Invalid number conversion on '01/01/2012'd I've copied this query from Access where the dates are in this format so I'm trying to get them to match without having to change all of the hardcoded dates in my 'where' statement. How do I get the date comparison to work? I know if I left my data set formatted with date9. I could do '01JAN2012'd but I'm trying to avoid having to rewrite all of my dates.
3 REPLIES 3
PaigeMiller
Diamond | Level 26

I believe that date comparisons must be in the form of

 

IF DATE = '01JAN2012'd

 

and not

 

IF DATE = '01/01/2012'd

 

regardless of the format used for the variable.

--
Paige Miller
Steelers_In_DC
Barite | Level 11

If they are all formatted as dates it won't matter:

 

DATA have;
infile cards dsd;
informat date mmddyy10.;
format date mmddyy10.;
INPUT date;
cards;
01/01/2012
;

data want;
set have;
date2 = date;
date3 = date;
format date2 date9. date3 yymmdd8.;
if date = date2 = date3 then match = 'yes';
run;

 

if you have dates coming in as numeric or character formats you might want to change that.  If that doesn't help give an example of the data you have and what you are trying to do.

 

Mark

Reeza
Super User
A format only controls the display value, not the underlying value. If the variable type is numeric and you can apply a date format, then you can write the comparison in date9 format, or even as the numeric SAS date.

So you can write your conditions as DATE9 without modifying your date variables, as long as they're date variables

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 1277 views
  • 0 likes
  • 4 in conversation