I'm trying to check if the day of the week is X. Can I run the below outside of a datastep?
%macro temp1(); %if WEEKDAY(datepart(today()))= 3 %then %do; %let Tuesday = 1; %end; %mend; %temp1();
If not how can I check this in a datastep?
And if using nested data step functions you have to use %sysfunc for EACH function
%if %sysfunc( WEEKDAY( %sysfunc(today())) ))
Don't use DATEPART on TODAY as you will get very strange results since the result of Today is a date value.
data example; x=today(); put x= date9.; y = datepart(x); put "Datepart applied to x: " y= date9.; run;
Which when I run the above code:
x=26MAY2020 Datepart applied to x: y=01JAN1960
The DATEPART function expects a datetime value which uses seconds since midnight 01JAN1960 as the units. So when given a date value, which uses days from basically the same start point you get the days treated as seconds and the result is way off. Plus the day of the week is almost always going to be wrong because of the same conversion.
How do you intend to use the result? If you just need a yes/no for "weekday is a Tuesday", you can always use
WEEKDAY(datepart(today()))= 3
as a formula.
I"m trying to check if the day of the week is X as expression text. What you sent might work. Currently testing.
Just found that you tried to use datepart() on the result of today(), which is incorrect. today() already returns a date.
Mind that you can always do this:
%let tuesday = WEEKDAY(today()) = 3;
and later, in a data step
if &tuesday then do;
......
end;
But to speed up things, you will do
data _null_;
call symputx('tuesday',(WEEKDAY(today()) = 3),'g');
run;
as this evaluates the formula once and stores a literal "0" or "1" in the macro variable.
You need to use macro function %SYSFUNC() to call DATA step functions, and optionally format the result.
Example:
Use the WEEKDAY. format for the result of the TODAY() function.
%macro temp1(); %local tuesday_flag; %let tuesday_flag = %eval(%sysfunc(today(), weekday) = 3); %put NOTE: &=tuesday_flag (session started &SYSDATE);
%mend; %temp1()
------ LOG ------
NOTE: TUESDAY_FLAG=1 (session started 26MAY20)
If you want a macro that code gens a result for use in a macro expression, it might look like
%macro today_is_tuesday(); %eval(%sysfunc(today(), weekday) = 3) %mend; %if %today_is_tuesday %then %do; %put NOTE: Today IS tuesday; %end; %else %do; %put NOTE: Todays IS NOT tuesday; %end;
And if using nested data step functions you have to use %sysfunc for EACH function
%if %sysfunc( WEEKDAY( %sysfunc(today())) ))
Don't use DATEPART on TODAY as you will get very strange results since the result of Today is a date value.
data example; x=today(); put x= date9.; y = datepart(x); put "Datepart applied to x: " y= date9.; run;
Which when I run the above code:
x=26MAY2020 Datepart applied to x: y=01JAN1960
The DATEPART function expects a datetime value which uses seconds since midnight 01JAN1960 as the units. So when given a date value, which uses days from basically the same start point you get the days treated as seconds and the result is way off. Plus the day of the week is almost always going to be wrong because of the same conversion.
Running this:
%if %sysfunc( WEEKDAY( %sysfunc(today())) ))
as an expression worked.
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.