BookmarkSubscribeRSS Feed

How To Get the Code From a SAS Studio Flow and Why It Matters

Started ‎02-17-2023 by
Modified ‎02-22-2023 by
Views 4,427

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.

 

Why is This Important?

 

SAS Customers want to run SAS Studio Flows:

  • In batch, from the command line.
  • From a CI/CD pipeline, for example from Azure Pipelines or Jenkins.
  • 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.

 

The Key

 

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.

 

The Answer: a REST API

 

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.

 

Practical Example

 

Watch the following video for an overview of the approach:

 

 

SAS Studio Flow

 

Suppose you developed a flow that performs a de-duplication of an existing table.

 

bt_1_SAS_Studio_Flow-1024x576.png

  

To generate the SAS code form this flow you could write a SAS program:

 

SAS Code Generation Program

 

You need to initialize several files:

 

  • input – will be posted to the REST API endpoint. Perhaps, the most important in this JSON file is the path to your SAS Studio Flow.
  • resp – has the REST API response.
  • flowcode – holds the generated SAS code.

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


Run Result

 

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.

 

bt_2_SAS_Code_Generation_REST_API.png

 

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.

 

bt_3_SAS_Code_Generated_with_REST_API_from_SAS_Studio_Flow-1024x576.png

 

SAS Code to a File

 

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;

 

Save and Version the SAS Code

 

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.

 

bt_4_SAS_Code_Generated_to_Git_Repository.png

 

 

Execute the SAS Code

 

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.

 

PyViyaTools

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.

 

Conclusion

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.

Acknowledgements

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. 

Resources

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.

Comments

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.

Version history
Last update:
‎02-22-2023 07:50 PM
Updated by:
Contributors

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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