SAS Enterprise Guide

Desktop productivity for business analysts and programmers
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Asmam
Fluorite | Level 6

 

Hello everyone, 

 

I am trying to read the value of a macro variable sas from my shell script 

code SAS

%let rc1=&x ;
x setenv var &rc1.;

Shell script 

period_ex=$(date --date='30 days ago' +%Y%m)
/sas/sasbin/SASFoundation/9.4/sas -sysin /sasdata/04_code_sas/0102_Flux_Client_sas.sas -autoexec /sasdata/04_code_sas/0100_autoexec.sas -NOPRINT -nosyntaxcheck -noerrabend -log /sasdata/07_logs/0102_Flux_Client_sas_$(date +"%Y%m%d_%H%M%S").log -SYSPARM $period_ex%$(date +"%Y%m%d_%H%M%S") -XCMD 
echo $var

but the value is empty 

Can someone help me please

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Short answer: you can't do that.

Longer answer:

When you run SAS from a shell script, the shell forks off a copy of itself that will run the SAS command via the exec() system call. What you manipulate is therefore the environment of a child of the shell that runs the shell script. Since the child has no access to the parent's environment, changes will be lost when the child terminates.

 

Possible workarounds:

- save the setting of the environment variable to a file and source that in the shell script:

SAS:

data _null_;
file '$HOME/tempscript';
put "VAR=xxx";
run;

shell script:

sas program.sas
. $HOME/tempscript
rm $HOME/tempscript

- you can set return codes with

abort return &rc.;

and evaluate them in the script

sas program.sas
RC=$?

BTW, do test external commands from the commandline before using them in an x statement.

AFAIK, setenv() is a system call (ie for C programming), but not a system command.

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

Short answer: you can't do that.

Longer answer:

When you run SAS from a shell script, the shell forks off a copy of itself that will run the SAS command via the exec() system call. What you manipulate is therefore the environment of a child of the shell that runs the shell script. Since the child has no access to the parent's environment, changes will be lost when the child terminates.

 

Possible workarounds:

- save the setting of the environment variable to a file and source that in the shell script:

SAS:

data _null_;
file '$HOME/tempscript';
put "VAR=xxx";
run;

shell script:

sas program.sas
. $HOME/tempscript
rm $HOME/tempscript

- you can set return codes with

abort return &rc.;

and evaluate them in the script

sas program.sas
RC=$?

BTW, do test external commands from the commandline before using them in an x statement.

AFAIK, setenv() is a system call (ie for C programming), but not a system command.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 3462 views
  • 1 like
  • 2 in conversation