BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DavidPhillips2
Rhodochrosite | Level 12

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?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

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.

DavidPhillips2
Rhodochrosite | Level 12

I"m trying to check if the day of the week is X as expression text.  What you sent might work.  Currently testing.

 

Kurt_Bremser
Super User

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.

RichardDeVen
Barite | Level 11

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;

 

ballardw
Super User

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.

DavidPhillips2
Rhodochrosite | Level 12

Running this: 

 %if %sysfunc( WEEKDAY(  %sysfunc(today())) ))

as an expression worked. 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 4963 views
  • 0 likes
  • 4 in conversation