BookmarkSubscribeRSS Feed

How to Run SAS Programs with Parameters in Batch in SAS Viya

Started ‎06-05-2022 by
Modified ‎06-05-2022 by
Views 4,823

If you wondered how to pass parameters to a SAS program running in batch in SAS Viya (2020.1 or later), this post shows you a possible solution. The alternative titles could have been “How to write your SAS program for a CI/CD pipeline, when you have parameters” or "How does the autoexec work in batch with SAS Viya".

 

The SAS Program

 

Consider the following SAS program, that defines a PATH CASLIB. Suppose you wrote and tested the program in an environment, which we will call DEV.

 

* CAS host ;
cas casauto host="controller.sas-cas-server-default.gelazure.svc.cluster.local"
port=5570 sessopts=(metrics=true);

* CASLIB path and name;
%let SRC_PATH=/azuredm/gitrepo/content/;
%let DM_src=source;

* define a CASLIB on this specific path ;
proc cas;
table.dropcaslib caslib="&DM_SRC" quiet=true;
quit;

proc casutil;
caslib &DM_SRC path="&SRC_PATH" global;
list files;
quit;

cas casauto terminate ;

 

The Problem

 

Suppose you want to run the same program, in batch, in another SAS Viya environment, which we will call TEST. If the two SAS Viya environments are perfectly the same, then you do not have to do anything. However, what if in TEST, you have a different CAS host and CASLIB path?

 

%let cashost=controller.sas-cas-server-default.gelenv.svc.cluster.local;
%let SRC_PATH=/gelcontent/home/sasadm/gitrepo/content/;

 

Your program will fail in TEST.

 

To solve the problem you could create a "TEST version" of your program. What if you have hundreds of programs you need to promote? Do you multiply them by the number of environments? 

 

Solution

 

A better solution might be to define the parameters that change with the environment, in a second file. With other words, you avoid hardcoding elements that change with the environment.

 

For DEV, create a myauto-dev.sas program:

 

%let cashost=controller.sas-cas-server-default.gelazure.svc.cluster.local;
%let SRC_PATH=/azuredm/gitrepo/content/;

 

For TEST, create a myauto-test.sas program:

 

%let cashost=controller.sas-cas-server-default.gelenv.svc.cluster.local;
%let SRC_PATH=/gelcontent/home/sasadm/gitrepo/content/;

 

Your main program will use the parameters from the myauto-$env.sas program, where env is dev, test, etc. Let us name this program 1.sas:

 

* Print parameters from myauto file;
%put &cashost;
%put &SRC_PATH;

* CAS host ;
cas casauto host="&cashost" port=5570 sessopts=(metrics=true);

* CASLIB path and name;
%let DM_src=source;

* define a CASLIB on this specific path ;
proc cas;
table.dropcaslib caslib="&DM_SRC" quiet=true;
quit;

proc casutil;
caslib &DM_SRC path="&SRC_PATH" global;
list files;
quit;

cas casauto terminate ;

 

The Batch Execution

 

To run this program, in batch, use the SAS Viya Command Line Interface (CLI), submit-pgm command. Create a variable env with values dev, test, etc., that you will use in your parameter file name (one per environment): myauto-dev.sas, myauto-test.sas, etc. When you submit the programs for execution, specify --job-file myauto-$env.sas. In bash, it would be:

 

clidir=/opt/sas/viya/home/bin # the folder where the SAS Viya CLI is installed
cd ~ # the folder where 1.sas and myauto.sas programs are
echo Run the main SAS program 1.sas with myauto.sas as autoexec file
export env=test
cat ~/myauto-${env}.sas > ~/myauto.sas
$clidir/sas-viya batch jobs submit-pgm --pgm-path ~/1.sas --context default --job-file myauto.sas --sasoption '-autoexec !BATCHJOBDIR/myauto.sas' --watch-output --wait-log-list --results-dir ~/
echo Reprint the execution log
cat ~/1.log

 

The file myauto.sas is used as an autoexec file for the SAS session. The --job-file option is used to upload the myauto.sas file to the file set, where it is then copied to the batch job directory. There, it is accessible as !BATCHJOBDIR. The --sasoption option is used to specify the -autoexec SAS option that uses the myauto.sas file.

 

With SAS Viya on Kubernetes, the execution happens in a new pod. The pod is created when you run the submit-pgm command. The pod is destroyed when the job is complete. You therefore need to pass both files at the execution time.

 

How to Create a SAS Viya CLI Profile and Get a SAS Viya Access Token

 

Previous posts comments asked to add the code to create a SAS Viya CLI profile and get a token. There it is:

 

echo Create SAS-Viya CLI Profile
export SAS_CLI_PROFILE=gelenv
# gelenv is the Kubernetes namespace where SAS Viya is running
export current_namespace=$SAS_CLI_PROFILE
export CURL_CA_BUNDLE=~/.certs/${current_namespace}_trustedcerts.pem
export SSL_CERT_FILE=~/.certs/${current_namespace}_trustedcerts.pem
export REQUESTS_CA_BUNDLE=${SSL_CERT_FILE}
echo Adapt the SAS Viya INGRESS_URL to match your environment
export INGRESS_URL=https://Your-SAS-Viya_URL-here/ # for example https://gelenv.pdcesx08880.race.sas.com/
clidir=/opt/sas/viya/home/bin
echo This is the folder where the SAS-Viya CLI is installed $clidir
$clidir/sas-viya --profile ${SAS_CLI_PROFILE} profile set-endpoint ${INGRESS_URL}
$clidir/sas-viya --profile ${SAS_CLI_PROFILE} profile toggle-color on
$clidir/sas-viya --profile ${SAS_CLI_PROFILE} profile set-output fulljson
$clidir/sas-viya --profile ${SAS_CLI_PROFILE} profile show
echo Login in
$clidir/sas-viya --profile ${SAS_CLI_PROFILE} auth login -user sasadm -password *********
export SAS_OUTPUT=text
echo You can now run any sas-viya cli commands, including the submit-pgm

 

Conclusions

 

To execute a SAS program, in batch, using parameters, you can split your program in two: a program containing the parameters and a second program using them. Submit both programs using the SAS Viya CLI, the batch plug-in.

 

Acknowledgements

 

Many thanks to my SAS colleague @AjmalFarzam.

 

References

 

Thank you for your time reading this post. If you liked the post, give it a thumbs up!

 

Please comment and tell us what you think about post content. If you wish to get more information, please write me an email.

 

Find more articles from SAS Global Enablement and Learning here.

Version history
Last update:
‎06-05-2022 08:34 PM
Updated by:

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!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags