BookmarkSubscribeRSS Feed

How to Run SAS Programs in Batch in the New SAS Viya

Started ‎04-07-2021 by
Modified ‎06-05-2022 by
Views 15,428

Did you know there is a new command line interface in the new SAS Viya (2020.1 and later)?

The Essential

To run in batch a .sas program called


In the new SAS Viya (2020.1 and later) you could use the Command-Line Interface (CLI) batch plug-in:


/opt/sas/viya/home/bin/sas-viya --profile ${SAS_CLI_PROFILE} batch jobs submit-pgm --pgm-path /tmp/ --context default  --watch-output --wait-log-list --results-dir /tmp


vs. SAS Viya 3.5 where you had to write:


/opt/sas/spre/home/SASFoundation/sas -autoexec "/opt/sas/viya/config/etc/workspaceserver/default/" /tmp/ -log /tmp/1.log


More Details


SAS Batch Command Line Utility is a command line interface that enables you to submit SAS programs or commands from a command line to a SAS Viya environment running in a Kubernetes cluster for batch processing. The principles are described Command-Line Interface: Overview.

The top-level command is sas-viya. It is used to initialize, authenticate, and execute the batch plug-in. For a complete plug-in list, see Command-Line Interface: Plug-Ins.



There are some extra steps before you can submit your sas-viya batch jobs submit-pgm command.

Setup a profile

Find your Kubernetes namespace. In this example, SAS Viya is running in the gelcorp namespace. If you don't know in which namespace Viya is running, type:


kubectl get ns


The SAS Viya applications (e.g. SAS Environment Manager, SAS Drive, etc.) can be accessed, by default, at the location described by $INGRESS_URL.


INGRESS_SUFFIX=$(hostname -f)


TLS case

CAS supports encrypted connections between the server and the clients. You can enable TLS to secure communications between the server and clients. The certificate used for client server communication needs to be signed by a certificate authority (CA) that is trusted by all potential clients.


Therefore, if you have a SAS Viya deployment, where TLS is enabled, you need to execute these commands to get the certificates once. You need a kubectl admin role.


mkdir -p ~/.certs
kubectl cp $(kubectl get pod -l app=sas-logon-app -o=jsonpath='{.items[0]}'):security/trustedcerts.pem ~/.certs/${current_namespace}_trustedcerts.pem



You need to run the following commands every time, before the sas-viya profile and auth commands. These will point the CLI to the location of the certificates and certificate authority bundle (TLS).


export SSL_CERT_FILE=~/.certs/${current_namespace}_trustedcerts.pem


Switch to the folder where the SAS VIYA CLI is installed. I assume you installed it in the $clidir.


cd $clidir


Create a functional profile called ${SAS_CLI_PROFILE}. This resolves to gelcorp.


echo ${current_namespace}
export SAS_CLI_PROFILE=${current_namespace}
./sas-viya --profile ${SAS_CLI_PROFILE} profile set-endpoint "${INGRESS_URL}"
./sas-viya --profile ${SAS_CLI_PROFILE} profile toggle-color off
./sas-viya --profile ${SAS_CLI_PROFILE} profile set-output fulljson

# login and create a token
./sas-viya --profile ${SAS_CLI_PROFILE} auth login -user sasadm -password *******


By default, your authentication remains active for 12 hours. You can use the sas-viya batch auth logout command to sign out.

Run a .sas program

Create a in the /tmp folder, for example:


echo 'proc setinit; run; quit;' | tee /tmp/

Submit the program for execution:


cd /tmp/
/opt/sas/viya/home/bin/sas-admin --profile ${SAS_CLI_PROFILE} batch jobs submit-pgm --pgm-path /tmp/ --context default  --watch-output --wait-log-list --results-dir /tmp


Run a cas statement

In SAS Environment Manager, find out your CAS controller host and port.



Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.



Create a program and submit it for execution:


echo 'cas casauto host="" port=5570;' | tee /tmp/
echo 'cas casauto terminate;' | tee -a /tmp/
cat /tmp/


Submit it for execution:


cd /tmp/
/opt/sas/viya/home/bin/sas-viya --profile ${SAS_CLI_PROFILE} batch jobs submit-pgm --pgm-path /tmp/ --context default  --watch-output --wait-log-list --results-dir /tmp


The log would look like this:


>>> File set created, ID="JOB_20201009_045619_929_1"
>>>   Uploading ""
>>> Job submitted, ID="64139ca3-84aa-4d6c-bed8-d7c25a3647fd".
>>> Waiting for job to complete...
>>> Job started.
1          cas casauto host="" port=5570;
NOTE: The session CASAUTO connected successfully to Cloud Analytic Services using port 5570. The UUID is
      fc406c5b-3ec4-914c-97b2-48c99121eb1c. The user is sasadm and the active caslib is CASUSER(sasadm).
NOTE: The SAS System used:
      real time           0.40 seconds
      cpu time            0.06 seconds
<<< Job ended.
<<< Downloading files in fileset to "/tmp/JOB_20201009_045619_929_1".
<<<   Downloading "2.log"
<<<   Downloading ""
<<< Copying log and list files from "/tmp/JOB_20201009_045619_929_1" to current directory
<<<   Copying "2.log"
<<< Deleting results directory.
<<< Deleting batch job.
<<< Deleting fileset.


The sas-viya batch CLI seems to function more like the RSUBMIT statement from SAS/Connect. Only the tasks executed are not in a "server" session, but in a Kubernetes cluster, while the results and the output are delivered to the "client" session.


Options used

  • --profile uses the token you obtained, and the preferences defined earlier.
  • --context specifies the batch context in which the program is run.
  • --pgm-path the path to the program on the Kubernetes node machine. The specified program is uploaded to the file set so that it can be downloaded to the job directory and accessed on the execution pod.
  • --watch-output prints the execution of the program.
  • --wait-log-list waits until the job has completed running (either successfully or with an error).
  • --results-dir specifies the directory to store the results of commands or SAS programs if you also specified a --wait option.


More details on how to Submit SAS Programs in Batch.


SAS Viya CLI installed

To perform the batch commands, you must have the SAS Viya CLI installed on the machine where you will execute the commands. Gerry Nelson provided more context in his post Keeping the SAS Administration Command-Line interfaces up-to-date. His post talks about the sas-admin executable, which has been replaced by sas-viya for SAS Viya release 2020.1 and later. Replace sas-admin with sas-viya in his  instructions and you should be fine.

pyviyatools installed

To authenticate with an .authinfo file you need the pyviyatools. The pyviatools installation instructions are found here: .

Think security

A more secure way is to avoid encoding credentials. You can use an .authinfo file in conjunction with the pyviya tools. See Gerry's post: Introducing the GEL pyviyatools.


# Instead of
# ./sas-viya --profile ${SAS_CLI_PROFILE} auth login -user sasadm -password *****

# Create .authinfo - replace ***** with real credentials
tee ~/.authinfo > /dev/null << EOF
default user sasadm password *****
chmod 600 ~/.authinfo
# Browse to PyViya Tools folder and use the loginviauthinfo command for authentification
cd ~/admin/pyviyatools
./ -f ~/.authinfo

The pyviatools installation instructions are found here: .


To execute sas programs in batch in the new SAS Viya (2020.1 and later), consider using the sas-viya batch CLI. It is not the only option, there are other CLIs that allow you to manage, transfer, automate. What if you have parameters to pass to a SAS program running in batch? Read How to Run SAS Programs with Parameters in Batch in SAS Viya. What if you need to run a SAS Studio flow instead of a SAS program? Read How to Run a SAS Studio Flow in Batch.


Full credits and many thanks to: Ajmal Farzam, Gerry Nelson, Mark Thomas and Stephen Foerster.



Thank you for your time reading this article. If you think the article was useful, like it.

Please comment and share your experience with SAS Viya and help others.


Find more articles from SAS Global Enablement and Learning here.


@Bogdan_Teleuca  - This is very informative. Is there a GUI that automates the command line interface in the same way Schedule Manager in SAS Management Console automates the SAS 9 batch job command line interface? SAS Workflow Scheduler perhaps?

I don't think there is one.

@Bogdan_Teleuca  - Are there any plans to provide one?

Interesting article. Only two weeks ago I had to come up with some solutions to batch submit jobs in Viya 3.5 from Control-M as part of the DI ETL processing and subsequent load into CAS.


I came up with two solutions.

- The first being a shell script to run a SPRE session in batch as described in the article above.

- The second more interesting one was to use use REST calls. I came up with a working proof of concept where by from a SAS9.4 DI Server using proc http, I made two calls:1) obtain an auth token :, then followed this with a call to the SAS Viya job execution service. I believe that Control-M can make REST calls (not tested), but otherwise Control-M could be used to call a SAS9.4 job and SAS9.4 can make the REST calls.


Part of the code for the second REST call to kick off the job:

proc http      url="&viyahostname/jobExecution/jobs"




        Headers "Content-Type"= "application/"


'Authorization'="Bearer &access_token"




Job information and code is passed in the json_in file and included code, as well as parameters that you could pass. These parameters would appear as macro variables in the SAS session for example (json fragment):

        "testparm": "test456",

        "testparmB": "test789"


in the SAS Session - shows the parameters appearing as macro variables:

18  +%put _all_;

GLOBAL DTSTAMP 2021_03_18T22_53_23

GLOBAL SYS_COMPUTE_DATA /opt/sas/viya/config/data/compsrv/default


GLOBAL SYS_COMPUTE_SESSION_ID 259db425-7750-4c90-8d25-f47e5cb8fcf8-ses0000


GLOBAL SYS_JES_JOB_URI %nrstr(/jobExecution/jobs/be57e9e9-e7b1-4370-8e54-14297f3997af)






I kept the SAS code in the job definition simple - being a couple of statements to do a %include. The included .sas file could then contain more extensive code - in this case CAS table load, save, promote etc. code.


The job would display in the Environment Manager - Jobs screen. Like this one (from our Viya 3.4 AWS playpen):



This REST call solution meant that the batch jobs could be called from SAS9 and therefore be made into a SAS9 DI Job and be scheduled using the usual SAS9 scheduling tools.

In this particular application, the existing SAS DI environment was an older - pre M5 release which meant that integration with Viya was limited. We couldn't use proc casutil or proc cas, for example to load data into CAS as part of the nightly ETL processing. Using proc http and REST calls meant that we could kick off a job on Viya that would load data into CAS after the nightly ETL batch had completed.


To develop this, I used simple CURL statements and Postman to test the REST calls



Thank you BogdanT and  MarkBodt_NewZealand .Very informative.

Sorry for a naive question; I don't have access to a Viya environment.  The above seems like a good bit of work just to batch submit.  For a casual Viya batch submit, is the SAS Studio background submit a reasonable option?  Obviously it's not CLI, but for most of my use cases, I'd be happy to manually open a program and click a "batch submit" button, as long as I got a logical batch submit (i.e. starts a new clean session, executes the program, and writes  a .log file and .lst file).

I see on a Viya 4 presentation roadmap that job scheduling equivalent to Platform Services / LSF is due in Viya 2021.2. Is this likely to fix the missing graphical user interface issue?

Version history
Last update:
‎06-05-2022 08:50 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