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.)
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 ;
You just need to remove the format from your statement, try changing it to this:
%let DatePublication = %sysfunc(today());
%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."
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.
🙂
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.