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 3,661

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=""
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;

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

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 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? 




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 program:


%let SRC_PATH=/azuredm/gitrepo/content/;


For TEST, create a program:


%let SRC_PATH=/gelcontent/home/sasadm/gitrepo/content/;


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


* 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;

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

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):,, etc. When you submit the programs for execution, specify --job-file myauto-$ In bash, it would be:


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


The file is used as an autoexec file for the SAS session. The --job-file option is used to upload the 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 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
echo Adapt the SAS Viya INGRESS_URL to match your environment
export INGRESS_URL=https://Your-SAS-Viya_URL-here/ # for example
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




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.




Many thanks to my SAS colleague @AjmalFarzam.




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:



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

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