BookmarkSubscribeRSS Feed
KP12
Fluorite | Level 6

Hello Everyone,

I have a following code which need to be run according to weekday; the difference is in the WHERE clause of the codes:


if weekday(today()) NE 2 then:

PROC SQL;

create table work.Amount as

SELECT A,B,C

FROM Amount_Month

WHERE DATE_AMT = today()-1;

QUIT;

if weekday(today()) EQ 2 then:

PROC SQL;

create table work.Amount as

SELECT A,B,C

FROM Amount_Month

WHERE DATE_AMT >= today()-3;

QUIT;

How can make this code run, as I tried using it inside data _null_ but I'm getting an error, please help. Thanks in advance.

5 REPLIES 5
KP12
Fluorite | Level 6

Tried achieving the task using following code, but again getting an error:

ERROR 180-322: Statement is not valid or it is used out of proper order.

%macro except_monday;

PROC SQL;

create table work.Amount as

SELECT A,B,C

FROM Amount_Month

WHERE DATE_AMT = today()-1;

QUIT;

%mend;

%macro for_monday;

PROC SQL;

create table work.Amount as

SELECT A,B,C

FROM Amount_Month

WHERE DATE_AMT >= today()-3;

QUIT;

%mend;

data _null_;

if (weekday(today()=2)) then %for_monday;

else %except_monday;

run;

KP12
Fluorite | Level 6

Hello everyone,

added call execute in the data step & the code is working fine now:

data _null_;

if weekday(today())=2 then call execute ('%for_monday');

else call execute('%except_monday');

run;


Any further improvements are welcome. Thank you Smiley Happy

Ksharp
Super User

Make a macro variable .

%let n=%sysfunc(ifn(%sysfunc(weekday( %sysfunc(today()) )) NE 2,1,3 )) ;

%put &n ;

Xia Keshan

KP12
Fluorite | Level 6

Hi Xia Keshan,

In your code I believe we can achieve the value of 1 & 3 depending on the weekday, but the actual issue is in the WHERE clause. So for other weekdays the WHERE clause should be an EQUAL TO (=) & for monday it should be GREATER THAN EQUAL TO (>=)

Ksharp
Super User

OH. I missed one condition . Try this one :

%let cond=%sysfunc(ifc(%sysfunc(weekday( %sysfunc(today()) )) NE 2, = today()-1 , >= today()-3 )) ;

%put &cond ;

Xia Keshan

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 2462 views
  • 1 like
  • 2 in conversation