DATA Step, Macro, Functions and more

global macro variables with inter-program scope?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

global macro variables with inter-program scope?

Hello,

I'm running a series of SAS programs which calculate bootstrap confidence intervals. In order to cut the run time, I'm running a series of programs in a timed sequence, with 5 bootstrap programs running simultaneously:

data_prep -> bootstrap1 & bootstrap2 & bootstrap3 & bootstrap4 & bootstrap5 -> compile_results

It would be awesome if I could declare a macro variable only once, say in the initial data_prep program, and then pass on the value of that macro variable to the subsequent programs rather than having to "hard code" the macro variable value in each bootstrap program. The fewer parameters I have to tweak the fewer errors I'll make. Is it possible to create global macro variables with inter-program scope?

Any thoughts/suggestions are welcome!

Thank you,

Robert


Accepted Solutions
Solution
‎04-24-2012 01:01 PM
Super User
Posts: 11,343

Re: global macro variables with inter-program scope?

If they are run in the same SAS session global macro variables will be available in any program.

I use this approach to set a date stamp string in a specific format in my AUTOEXEC.SAS file so it doesn't have to be created in each program.

View solution in original post


All Replies
Solution
‎04-24-2012 01:01 PM
Super User
Posts: 11,343

Re: global macro variables with inter-program scope?

If they are run in the same SAS session global macro variables will be available in any program.

I use this approach to set a date stamp string in a specific format in my AUTOEXEC.SAS file so it doesn't have to be created in each program.

Occasional Contributor
Posts: 14

Re: global macro variables with inter-program scope?

Great, thank you - will global macro variables work when running my programs in batch mode from Unix?

Super User
Posts: 10,028

Re: global macro variables with inter-program scope?

It looks like you are looking for system options SYSPARM= .

SYSPARM Details

The value of the SYSPARM automatic macro variable is the same as the value of the

SYSPARM= system option, which is equivalent to the return value of the SAS language

function SYSPARM. The default value is null. Because you can use the SYSPARM=

system option at SAS invocation, you can set the value of the SYSPARM automatic

macro variable before your SAS session begins.

Ksharp

Occasional Contributor
Posts: 14

Re: global macro variables with inter-program scope?

Thanks Ksharp, the -SYSPARM option worked well as a command line option when running individual SAS programs in batch mode:

>>sasb bootprogram1 -sysparm 100

where the value of "100" is passed on to the sample size variable in my bootstrap program:

%let numsamp=&sysparm;

When running multiple SAS programs in a shell script, I noticed the SYSPARM option has to be added to each program in the script text file rather than on the actual command line.

So executing:

>>shell.sh -sysparm 2

where shell.sh contains

sasb bootprogram1 & sasb bootprogram2

does not execute properly, but

>>shell.sh

where shell.sh contains

sasb bootprogram1 -sysparm 2 & sasb bootprogram2 -sysparm 2

does work.

Robert

Super User
Super User
Posts: 7,042

Re: global macro variables with inter-program scope?

Store your parameters in a file or dataset.  Then each job can read them from there.

If you use a CSV text file then you can do something like:

data _null_;

  infile 'parms' dsd ;

  length name $32 value $200;

  input name value;

  call symputx(name,value);

run;

If you use a dataset then you can do something like:

data _null_;

  set 'parms.sas7bdat';

  call symputx(name,value);

run;

Super User
Posts: 10,028

Re: global macro variables with inter-program scope?

Yes. system options SYSPARM= is a SAS system option ,not UNIX system option.

But you can set its value in AUTOEXEC.SAS . so everytime start a sas session ,sas will take care of this option.

Respected Advisor
Posts: 3,799

Re: global macro variables with inter-program scope?

You can use SYSGET to retrieve the value of a system/environment variable.    There is a macro and a data step version of the function.

%SYSGET 

returns the character string that is the value of the environment variable passed as the argument. Both UNIX and SAS environment variables can be translated using the %SYSGET function. A warning message is written if the global variable does not exist. Here is the form of the %SYSGET function:

%SYSGET(environment-variable);

For example, the following code writes the value of the HOME environment variable to the SAS log:

%let var1=%sysget(HOME);  %put &var1;
Respected Advisor
Posts: 3,799

Re: global macro variables with inter-program scope?

I expect you don't need to run multiple jobs to speed up your bootstrap.  I think you just need to fix it to work better/faster.  Start a new thread we will have lots of fun fixing it.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 582 views
  • 3 likes
  • 5 in conversation