BookmarkSubscribeRSS Feed
SASdevAnneMarie
Barite | Level 11

Hello Experts,

 

I create the macro variable in this way :

%let DatePublication = %sysfunc(today(), ddmmyy10.);
%put &DatePublication.;

But when I apply this condition :

if datepart(d_fin) > &DatePublication.

Unfortunately, I don't have the right observation in output. Do I need apply another function? 

When I apply %eval it also doesn't work. The format of d_fin is datetime22. format. Thank you for your help.

if datepart(d_fin) > %eval(&DatePublication.)
5 REPLIES 5
AMSAS
SAS Super FREQ

In short you are not comparing apples to apples.
The macro variable DatePublication contains a formatted date (character value e.g. 13/06/2023)

Whereas the datepart function is returning a SAS date value (numeric number of days since 01 Jan 1960) from a SAS date/time value

 

Run the following code in SAS and compare the log output

Unless you have some reason for using the macro variable I would remove that additional complexity from the code (see 2nd data step in example below)

 

%let DatePublication = %sysfunc(today(), ddmmyy10.); 
%put &DatePublication.;

data _null_ ;
datetime=datetime() ;
datepart=datepart(datetime) ;
put datetime= ;
put datepart= ;
run ;

data _null_ ;
datetime=datetime() ;
datepart=datepart(datetime) ;
do date=date()-1 to date()+1 ;
if date>datepart(datetime) then
put "Date " date " > DateTime " datetime " - DatePart " datepart ;
else
put "Date " date "<= DateTime " datetime " - DatePart " datepart ;
end ;
run ;

 

russt_sas
SAS Employee

You just need to remove the format from your statement, try changing it to this:

 

%let DatePublication = %sysfunc(today());

PaigeMiller
Diamond | Level 26

 

%let DatePublication = %sysfunc(today(), ddmmyy10.);

 

The macro variable &DatePublication is not a valid SAS date. It is the text string 13/06/2023. SAS does not recognize this as a date, even though humans do recognize this as a date.

 

SAS dates are the number of days since 01JAN1960, and so SAS would recognize this as a valid SAS date.

 

 

%let DatePublication = %sysfunc(today());

 

 

This is the value 23174, which is a valid SAS date. It is the number of days since 01JAN1960. 

 

So when you compare a date value in your DATA step, SAS always uses its internal representation of the number of days since 01JAN1960. Thus if you use this particular &DatePublication, this should work in your DATA step.

 

See Maxim 28: "Macro variables need no formats."

--
Paige Miller
Tom
Super User Tom
Super User

The macro variable &DatePublication is not a valid SAS date. It is the text string 13/06/2023. SAS does not recognize this as a date, even though humans do recognize this as a date.

Some humans might.  My calendar only has 12 months in a year so 13/06/2023 does not look like a date to me.

🙂 

 

 

Tom
Super User Tom
Super User

When working with macro variables make sure the generate valid code.  If I run these two statements:

%let DatePublication = %sysfunc(today(), ddmmyy10.);
%put &DatePublication.;

I see 13/06/2023 printed in the SAS log.

 

Does this look like valid SAS code?

if datepart(d_fin) > 13/06/2023

You are dividing 13 by 6 and then again by 2023.

 

To convert 13/06/2023 back into a date you would need to use the INPUT() function.

if datepart(d_fin) > input("&DatePublication.",ddmmyy10.)

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 791 views
  • 0 likes
  • 5 in conversation