BookmarkSubscribeRSS Feed
Sikcion
Fluorite | Level 6

Hi guys! I have a question about how to compare date with different attributes?

For example, I have one variable "dthdt" which is a character, and the format is like 2010-08-10, and also I have one string “ 15MAY2020”.

Now I want to use the logical symbol to compare above 2 date, like if dthdt <-“ 15MAY2020”.

Both 2 are character, but it didn't work.

I don't know how to transfe their attributes? Thanks!

4 REPLIES 4
Tom
Super User Tom
Super User

FORMAT has a specific meaning in SAS that is different than the way you used it in your sentence.  A FORMAT in SAS is instructions for how to PRINT or DISPLAY the value of variable.  So a numeric variable with DATE values in it that has the YYMMDD10. format attached to it will print the date in a STYLE or PATTERN that looks like 2018-08-10.  But the actual value stored in the variable is the number 21,406.

509  data _null_;
510    date=input('2018-08-10',yymmdd10.);
511    put date= comma12. date=date9. date=yymmdd10.;
512  run;

date=21,406 date=10AUG2018 date=2018-08-10

If you want to use a date constant in your code you need to use a string that the DATE informat can interpret as a DATE value and enclose it in quotes and append the letter D (for DATE).

 

So if your variable DTHDT is NUMERIC with the YYMMDD10. format attached to it your code will look like:

if dthdt <= '15MAY2020'd then ...

But if the variable DTHDT is CHARCTER then you will want to convert the string into a DATE value before making the comparison.  If the character's string is in the style YYYY-MM-DD then the YYMMDD informat will be able to convert it into a date.

if input(dthdt,yymmdd10.) <= '15MAY2020'd then ...

 

NOTE: That character strings in the style YYYY-MM-DD style can actually be compared successfully since the strings will order the same as the date values themselves.  The same is not true of strings in the style produced by the DATE format.  But if you need do any other types of tests ( for example whether there are more than 3 years between two dates) then you will need to convert the strings into dates.

s_lassen
Meteorite | Level 14

When comparing dates stored as character variables in different formats, you should try to convert them to SAS dates, either by creating new variables

date1=input(dthdt,yymmdd10.); /* reading e.g. '2010-08-10' */
date2=input(string,date9.); /* reading e.g. '15MAY2020' */
if date1<date2 then...

or you can to it on the fly

if input(dthdt,yymmdd10.)<input(string,date9.) then...

if your "string" is actually a constant, not a variable, you can use e.g.

if input(dthdt,yymmdd10.)<'15MAY2020'd then...

- by putting the "d" after a string in DATE9. format, you convert it to a SAS date constant.

andreas_lds
Jade | Level 19

Actually you have two char variable containing strings which could be dates. So the very first step is to fix the step importing the data to store the strings as sas dates. Strings like "2010-08-10" can be read with the informat yymmdd or yyddmm - depending on the position of day and month in the string. The other string looks like the common sas date format, so using date. to read it should be working as expected. As soon as you have real dates, you can think about comparing them.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 480 views
  • 0 likes
  • 5 in conversation