BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
andresmontoya
Calcite | Level 5
Hi guys, I am trying to match a macro variable called & today 
that contains a date that it extracts from a variable suppose it contains

 

andresmontoya_0-1615476255499.png

 

then I create the macro variable from today which contains the date in date9 format.

 

andresmontoya_1-1615476452524.png

I have tried to change each of the dates to be either date9 format.
or datetime16 format, but when I do the% put to see what is inside it only comes out ******

andresmontoya_2-1615476680503.png

 

I have tried subtring to transform from ddmmyy but it doesn't work either

y too

andresmontoya_3-1615476959041.png

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The value you have the variable HOY is a datetime value (or more accurate the string of characters the DATETIME format would generate for that datetime value) which is a number of seconds.  A better name for the variable might be AHORA.

 

The value you are putting into TODAYSDATE is a date value (number of days).

 

So to compare them you will need to convert them. Both convert them to the same type of values (days or seconds) but also convert the representations to be the same style (string that represents the raw number or a string that is formatted for human understanding).

 

Since you want to compare to a date you probably want to convert your number of seconds to the number of days and then compare.  Then INPUTN() function can convert a string into a number.  The proper informat to use with that string is the DATE9 informat as it will convert the first 9 characters in &HOY into a date value.

%if %sysfunc(inputn(&hoy,date9.)) = &todaysdate %then ...

 

Another method is to use the fact that the date literals ignore any time component. So SAS code would treat "&hoy"d as a date value and ignore the :00:00 .  But to use date literals in macro code you need to explicitly use the %SYSEVALF() function instead of the implied %EVAL() function.

%if %sysevalf( "&hoy"d = &todaysdate ) %then ...

View solution in original post

4 REPLIES 4
ballardw
Super User

First thing: MACRO language is basically text. If you have values that are actually numeric, like the result of the today() function you are causing extra work by formatting them.

Second, your &hoy value is displaying the characteristics of a DATETIME value, not a date which is critical for comparison.

 

Where is &hoy created? If you want to compare to a date why is there a time component?

 

To have something close to comparison you want have to either create a date value for &hoy to compare with the result of Today() or create a datetime to compare  possibly:

%let td =%sysfunc(putn(%sysfunc(dhms(%sysfunc(today()),0,0,0)), datetime18.));
%put &td.;

Note: I frequently observe lazy programming resulting in date values stored as datetimes from some sources. If there is NO time component ever supplied, as in all the times are 00:00:00 then the value should not bother with the time component.

Tom
Super User Tom
Super User

Note: There is a "feature" of the DATETIME format that makes the width of 18 not work as expected. 18 characters  should be enough room for 9 character date a separator and 8 character time but SAS will use only 7 character date.  Use a width of at least 19 with datetime to get four digit years.

1277  data _null_;
1278    ahora = datetime();
1279    put ahora / ahora datetime18. / ahora datetime19. ;
1280  run;

1931080325.2
  11MAR21:11:12:05
 11MAR2021:11:12:05
Tom
Super User Tom
Super User

The value you have the variable HOY is a datetime value (or more accurate the string of characters the DATETIME format would generate for that datetime value) which is a number of seconds.  A better name for the variable might be AHORA.

 

The value you are putting into TODAYSDATE is a date value (number of days).

 

So to compare them you will need to convert them. Both convert them to the same type of values (days or seconds) but also convert the representations to be the same style (string that represents the raw number or a string that is formatted for human understanding).

 

Since you want to compare to a date you probably want to convert your number of seconds to the number of days and then compare.  Then INPUTN() function can convert a string into a number.  The proper informat to use with that string is the DATE9 informat as it will convert the first 9 characters in &HOY into a date value.

%if %sysfunc(inputn(&hoy,date9.)) = &todaysdate %then ...

 

Another method is to use the fact that the date literals ignore any time component. So SAS code would treat "&hoy"d as a date value and ignore the :00:00 .  But to use date literals in macro code you need to explicitly use the %SYSEVALF() function instead of the implied %EVAL() function.

%if %sysevalf( "&hoy"d = &todaysdate ) %then ...
andresmontoya
Calcite | Level 5

MUCHAS GRACIAS ,  ME SIRVIO  ,   THANKS  I LOVE YOU

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
  • 1601 views
  • 5 likes
  • 3 in conversation