SAS customers need to run a SAS Studio Flow in batch or from a CI/CD pipeline. They also need to version the flow code, possibly in a Git repository and compare the changes from one version to another. To achieve these goals, you need the SAS code behind the SAS Studio Flow. A REST API helps you generate the code. Read the post to find out more.
SAS Customers want to run SAS Studio Flows:
From open-source programming languages, such as Python.
They also want to:
Version the flows in Git repositories.
Compare the code changes from one version to another.
To run a SAS Studio Flow in batch or in a pipeline, you need the SAS code behind the flow.
To compare two versions of the same SAS Studio Flow, you need the SAS code behind the flow.
To generate the SAS code, use the /studioDevelopment/code REST API. The API is also known as the "CodeGen" or the “Code Generation” API, because it “generates” the SAS code from a SAS Studio Flow file.
The studioDevelopment REST API has been available since the SAS Viya 2022.09 Long Term Support.
Watch the following video for an overview of the approach:
Suppose you developed a flow that performs a de-duplication of an existing table.
To generate the SAS code form this flow you could write a SAS program:
You need to initialize several files:
filename resp clear;
filename input clear;
filename flowcode clear;
filename input temp;
data _null_;
file input recfm=f lrecl=1;
put "{";
put """reference"": {";
put """type"": ""content"",";
put """path"": ""/Users/sasuser/My Folder/Flow.flw"",";
put """mediaType"": ""application/vnd.sas.dataflow""";
put "},";
put """initCode"": true,";
put """wrapperCode"": false";
put "}";
run;
You then POST to the studioDevelopment/code
REST API the input JSON file. For Authentication, you can reuse the SAS_SERVICES
token, if you are writing the program inside SAS Studio.
%let myviyaurl = %sysfunc(getoption(servicesbaseurl));
%put NOTE: &=myviyaurl;
filename resp temp;
proc http url = "&myviyaurl/studioDevelopment/code"
in=input
out= resp
method="POST"
OAUTH_BEARER=SAS_SERVICES VERBOSE;
headers
"Content-Type" = "application/json;charset=utf-8"
"Accept" = "application/json"
;
run;
libname resp clear;
libname resp json;
/* OUTPUT:
resp json
libname resp json:
ALLDATA with columns: P, P1, V, Value
ROOT with columns: ordinal_root, code
Both root.code and alldata.value = the SAS Program generated from the source FLOW */
When you run the program and it is successful, you should see a 200 OK. It means, the REST API generated the code from the SAS Studio Flow.
You can then define a JSON library on top of the resp JSON file. From Libraries, open RESP.ROOT and notice the SAS Studio Flow code is stored in the code field.
You can write this program to extract the code to a file called flowcode.
* Save the code in a file you want to execute:;
* The flowcode must not have references to paths unknown to the Batch service;
filename flowcode temp;
data _null_;
set resp.root;
file flowcode;
put code;
run;
quit;
Alternatively, you can save the code to a SAS file, on the file system.
filename flowcode '/azuredm/gitrepo/programs/Flow.sas';
data _null_;
set resp.root;
file flowcode;
put code;
run;
quit;
You can also save it directly to a Git repository, provided you added one in SAS Studio.
Depending on your use case, you can execute the code, using an %include
statement, effectively executing the flow code.
* Skip errors if any from the flow code;
options nosyntaxcheck;
* Execute the flow file;
%include flowcode;
quit;
I also noticed that the generated code sometimes throws small errors when executed, hence the nosyntaxcheck option will help.
This is not the only way to run the SAS code. According to my colleague Alexey Vodilin, you can [use the] Compute API or Job Execution Service API depending on [your] use case. All these APIs are publicly available.
And since we are discussing Python and code generation, Gerry Nelson has just created a new pyviyatool named exportstudioflowcode.py. The nice thing about it is that it can loop through a folder and convert all the SAS Studio Flows from that folder.
# usage:
exportstudioflowcode.py [-h] -t {Flow,Folder} -n NAME -d DIRECTORY [--includeinitcode] [--includewrappercode]
The pyviyatools are a set of command-line tools that call SAS Viya REST APIs from Python. These tools simplify common administration tasks, provide more complex functionality and are a great complement to the SAS Viya CLI. The tools are available on the public SAS Software GitHub.
The /studioDevelopment/code REST API, also known as "CodeGen" can help you generate SAS code from a SAS Studio Flow.
With the code, you can further run a SAS Studio Flow in batch or from a CI/CD pipeline. Or, you can version the flow code using Git repositories and compare the changes from one version to another.
In a next post, we will look How to Get the Code From a SAS Studio Flow Using Python.
Thank you, Alexey Vodilin, Mark Escauriaga, Patric Hamilton, Cecily Hoffritz and Bruno Mueller for helping me figure out how this SAS REST API works and suggesting code improvements.
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 SAS codeGen REST API. If you wish to get more information, please write me an email.
Hi,
Great post.
It is worth to mention that you can also extract the code from the flow which is placed on a compute server. Then the flow can also be stored in a Git repository.
To do that you have to just change the type from "content" to "compute" in the request and set the right path.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.