BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
NKormanik
Barite | Level 11
Greetings.
 
I have one script/program file full of steps (let's call it "script.sas").  At the beginning of the script I place a macro variable, which is referred to throughout the script.
 
%let item=a001;
 
In the above macro variable, the value "a001" is one of many, let's say 100 different values in total.
 
i.e.,
a001
a002
a003
.
.
a100
 
 
Is there any way of passing all these macro variable values from a batch file?
 
Something along the lines of:
 
%include "a001" ;
%include "a002" ;
%include "a003" ;
 
 
Another idea might be to use the file name, so change:
 
script.sas --> a001.sas
 
Create a different file for each macro variable value.  Then tell SAS to use the name of the script as the macro variable value? 
 
Any suggestions greatly appreciated.
 
Nicholas Kormanik
 
 
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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);
...

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

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);
Quentin
Super User

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

 

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
Tom
Super User Tom
Super User

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);
...
NKormanik
Barite | Level 11

The simpler way seems to work fine.  Thanks much!

 

Why would you choose to go the more complicated route?

 

Tom
Super User Tom
Super User

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.

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!

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
  • 816 views
  • 3 likes
  • 4 in conversation