I want to create a variable that I can then use in any where statement. Something like this
DATA CREATE_JOB_SPECIFIC_VARS;
%let year_filter_1 = 2019;
%let year_filter_2 = extract(year from sysdate)-5;
RUN;
The intention is to create the variable in a "sql execute" or "user written" statement.
When I create the variable as mentioned in the example, the variable seems to have no content over time.
You do not need that data step around the macro statements; macro code is resolved before any other code is handed off to the data step compiler or any procedures.
This creates and shows your macro variables:
%let year_filter_1 = 2019;
%let year_filter_2 = %eval(%sysfunc(year(%sysfunc(today())))-5);
%put &=year_filter_1;
%put &=year_filter_2;
Log:
73 %let year_filter_1 = 2019; 74 %let year_filter_2 = %eval(%sysfunc(year(%sysfunc(today())))-5); 75 %put &=year_filter_1; YEAR_FILTER_1=2019 76 %put &=year_filter_2; YEAR_FILTER_2=2016
You do not need that data step around the macro statements; macro code is resolved before any other code is handed off to the data step compiler or any procedures.
This creates and shows your macro variables:
%let year_filter_1 = 2019;
%let year_filter_2 = %eval(%sysfunc(year(%sysfunc(today())))-5);
%put &=year_filter_1;
%put &=year_filter_2;
Log:
73 %let year_filter_1 = 2019; 74 %let year_filter_2 = %eval(%sysfunc(year(%sysfunc(today())))-5); 75 %put &=year_filter_1; YEAR_FILTER_1=2019 76 %put &=year_filter_2; YEAR_FILTER_2=2016
I have included Kurt's solution in "SQL execute" transformation.
In the job, this transformation is performed first. This solution works.
However, if the job crashes, the variable is no longer available and the first step must be performed again.
What tool are you using to build that work flow? Is it Enterprise Guide? SAS/Studio? Something else?
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.