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);
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));
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));
Nearly 200 sessions are now available on demand in the Innovate Hub.
Watch Now →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.