Is there a quick way to compare two dates but only up to the same lengths, whichever the shorter length is between the two?
For example, if we compare date A and date B, and if date A is 2019-07-08 and date B is 2019-07-08T10:30, then SAS would say date B is bigger after comparing 2019-07-08 and 2019-07-08T10:30. Can we ask SAS to compare only up to length=10 (compare 2019-07-08 vs. 2019-07-08 instead of 2019-07-08 vs. 2019-07-08T10:30)?
This rule would apply to any lengths of the two dates in the same format: if we have two dates 2019-07-08T08:09:11 and 2019-07-08T08:08, then SAS would drop seconds for the first date and compare 2019-07-08T08:09 vs 2019-07-08T08:08.
Use the colon modifier in your comparison, assuming you have character variables.
if date1 =: date2 then do;
.....
end;
You can compare only a specified prefix of a character expression by using a colon (:) after the comparison operator. SAS truncates the longer value to the length of the shorter value during the comparison. In the following example, the colon modifier after the equal sign tells SAS to look at only the first character of values of the variable LastName and to select the observations with names beginning with the letter
S
:
HI @gsk Are your date and datetime variables numeric variables formatted in the way you have shown or those are character variables. Can you check using proc contents plz
Thank you for the response! They are both character variables with length=20. Dates are in 2019-07-12 format, and hour, min, and sec come after T with colons (i.e. T14:29:30) if there is any. There doesn't need to be all components of year, month, date, hour, min or sec. A date could have only the year and month or can be missing.
Use the colon modifier in your comparison, assuming you have character variables.
if date1 =: date2 then do;
.....
end;
You can compare only a specified prefix of a character expression by using a colon (:) after the comparison operator. SAS truncates the longer value to the length of the shorter value during the comparison. In the following example, the colon modifier after the equal sign tells SAS to look at only the first character of values of the variable LastName and to select the observations with names beginning with the letter
S
:
Thank you everyone!
Either input(dateA, yymmdd10.)=input(dateB,yymmdd10.) or dateA=:dateB works
In the end, I did strip(dateA)=:strip(dateB). Thanks a lot!
Make sure that your dates, times and datetime variables are actual SAS date, time or datetime variables. These would show in proc contents or the data set description as 1) numeric values and 2) a format similar to Date9, MMDDYY10, YYMMDD10 for dates, TIME. for time values and DATETIME or E8601 and such.
Then when you need to compare "bits" of the values there are functions such as
If datevariable = datepart (datetimevariable)
if timevariable = timepart (datetimevariable)
or even use of the date functions:
If 12 = month(datepart(datetimevariable)); To get December records.
Hi @gsk Why not doing it clean with a yymmdd10. informat?
data test;
date_A='2019-07-08';
date_B= '2019-07-08T10:30';
if input(date_A,yymmdd10.)=input(date_B,yymmdd10.) then output;
run;
Hi @gsk So what. You are only comparing the first 10 chars in your words length=10 and I would call them a width of 10 in a informat, Therefore what is after 10 chars in my opinion is garbage for SAS's informat yymmdd10.
And the date check becomes real and safe
data test;
date_A='2019-07-08T03 ';
date_B= '2019-07-08T03:08 ';
if input(date_A,yymmdd10.)=input(date_B,yymmdd10.) then output;
run;
I agree with @Reeza's suggestion but, to account for leading and trailing blanks, I think you will want to add the strip function to the variables you're comparing. E.g., try the following code:
data have;
input (date1 date2) ($20.);
if _n_ eq 3 then date1=' '||trim(date1);
if date1 =: date2 then x=1;
if trim(date1) =: trim(date2) then y=1;
if strip(date1) =: strip(date2) then z=1;
cards;
2019-07-08T08:08:11 2019-07-08T08:08
2019-07-08T08:08 2019-07-08T08:08:11
2019-07-08T08:08 2019-07-08T08:08:11
;
run;
Art, CEO, AnalystFinder.com
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.