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

hello,

 

below is a simplified version of my code.  what's wrong with it and how can I correct it?

 

 

%macro test(mydate);

%put 1 &mydate.;

proc sql;
select * from xx where datepart(startdate) = &mydate.; *&mydate evaluates to '31AUG2018'd ;
quit;

/* calculate 1 year prior */
%let mydate = %sysfunc(intnx(year,&mydate,-1,same), date9.);
%put 2 &mydate.;

proc sql;
select * from xx where datepart(startdate) = &mydate.; *&mydate evaluates to 31AUG2017 and doesn't work;
quit;


%mend test;

%test('31AUG2018'd);

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

In the first case your macro variable's value looks like a date literal to SAS.

'31AUG2018'd

Then your %LET statements make a value that does NOT look like a date literal

31AUG2018

So you either need to change how you USE the value in the second query. 

For example add back the quotes and the D suffix.

datepart(startdate) = "&mydate"d

 

Or how change how you CREATE the new value. 

You can make a date literal, like you passed in the macro call.

%let mydate = "%sysfunc(intnx(year,&mydate,-1,same), date9.)"d ;

Or just let the macro variable have the raw number of days since 1960 value.

%let mydate = %sysfunc(intnx(year,&mydate,-1,same));

 

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

In the first case your macro variable's value looks like a date literal to SAS.

'31AUG2018'd

Then your %LET statements make a value that does NOT look like a date literal

31AUG2018

So you either need to change how you USE the value in the second query. 

For example add back the quotes and the D suffix.

datepart(startdate) = "&mydate"d

 

Or how change how you CREATE the new value. 

You can make a date literal, like you passed in the macro call.

%let mydate = "%sysfunc(intnx(year,&mydate,-1,same), date9.)"d ;

Or just let the macro variable have the raw number of days since 1960 value.

%let mydate = %sysfunc(intnx(year,&mydate,-1,same));

 

jffeudo86
Quartz | Level 8
Thank you for the explanation. It's educational for me. I'm not an experienced SAS coder and still finding my way. Some people reply with condescending attitude.
Astounding
PROC Star
So this works:

where datepart(startdate) = '31AUG2018'd

Why would you expect this to work:

where datepart(startdate) = 31AUG2017

Doesn't that look different?

The second time around try:

where datepart(startdate) = "&mydate"d

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 577 views
  • 0 likes
  • 3 in conversation