So you have set of values you want to use, let's assume you have those values in a dataset named HAVE with a variable named ITEM.
You could use that dataset to generate a program that looks like:
%let item=a001;
%include "script.sas";
%let item=s002;
%include "script.sas";
...
That is simple to do. Personally I prefer to do it by writing the lines of code to a file and including that file.
filename code temp;
data _null_;
set have;
put '%let ' item= ';';
put '%include "script.sas";';
run;
%include code / source2;
Sounds like you are close to converting your program into a macro. Just wrap the current connect with a %MACRO and %MEND statements. Let's say we call the macro SCRIPT. So you could copy your existing program into a new program we could call macro_script.sas. It would look like this:
%macro script(item);
.... lines from your program after the %LET ITEM statement ...
%mend script;
And now the lines of code you need to generate are like this instead.
%include "macro_script.sas";
%script(item=a001);
%script(item=a002);
...
If you want to feed values from outside to a SAS program, consider using environment variables:
export SAS_VALUE1=a001 /sasconf/Lev1/SASApp/BatchServer/sasbatch.sh $HOME/sas/script.sas
In your program script.sas you would do
%let item=%sysget(SAS_VALUE1);
I'm not understanding. Do you want to create one macro variable with a list of values, or do you want to create several macro variables?
Environment variables are an option, as @Kurt_Bremser mentioned.
Another way to do it is to have a program MakeGlobalMacVars.sas. That program could be just a bunch of %LET statements. And your main program could %include MakeGlobalMacVars.sas .
Another way to do it would be to make a SAS dataset (or database table, or text file) that stores name-value pairs (macro variable name, and value). Then your main program could read that dataset, and use CALL SYMPUTX to generate the macro variables.
Or search for papers/books on data-driven programming, e.g. Troy Hughes' book: https://www.amazon.com/SAS-Data-Driven-Development-Abstract-Functionality/dp/1726497739
So you have set of values you want to use, let's assume you have those values in a dataset named HAVE with a variable named ITEM.
You could use that dataset to generate a program that looks like:
%let item=a001;
%include "script.sas";
%let item=s002;
%include "script.sas";
...
That is simple to do. Personally I prefer to do it by writing the lines of code to a file and including that file.
filename code temp;
data _null_;
set have;
put '%let ' item= ';';
put '%include "script.sas";';
run;
%include code / source2;
Sounds like you are close to converting your program into a macro. Just wrap the current connect with a %MACRO and %MEND statements. Let's say we call the macro SCRIPT. So you could copy your existing program into a new program we could call macro_script.sas. It would look like this:
%macro script(item);
.... lines from your program after the %LET ITEM statement ...
%mend script;
And now the lines of code you need to generate are like this instead.
%include "macro_script.sas";
%script(item=a001);
%script(item=a002);
...
The simpler way seems to work fine. Thanks much!
Why would you choose to go the more complicated route?
A macro will allow you to create a more sophisticated program. You can use macro logic to make adjustments to the code being generated based on values that passed to it. You can use local macro variables to reduce risk of confusion caused by old macro variable values retaining values from previous runs. etc.
If it is something you need to do repeatedly you can store the macro code into your autocall library and skip the step of needing to %INCLUDE main code. You just need to write the macro call.
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!
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.