BookmarkSubscribeRSS Feed

Get the Code From a SAS Studio Flow using pure SAS program code

Started 17 hours ago by
Modified 17 hours ago by
Views 46

Sometimes we want to get the code that is behind a SAS Studio flow. See this post How To Get the Code From a SAS Studio Flow and Why It Matters for more details. The post provides a SAS program to read the generated code. Now often not all the code will fit into the max length of character variable in a SAS data set.

 

In this post we will explore a different way to get the complete code that is returned by the Generates code for an item at the given location API.

 

What we will cover

  • Call the API
  • Write the code to a file

 

Call the API

To call the Generates code for an item at the given location API we are going to use Proc HTTP. Lets look at the code step by step.

 

Prepare the request body for the API call. There are various ways of creating this. Using Proc STREAM allows it to be part of a macro definition. The formatting of the json is not preserved when written to the file. As our text starts with braces we need the &streamdelim; after the begin keyword. References to macro triggers like &flowloc and &flowname are resolved. I found this the easiest way for me to create json based request bodies for Proc HTTP.

 

/* path of the flow */
%let flowname = /Users/&sysuserid/My Folder/simple-flow.flw;
/* location of flowname: content for SAS Content, compute for filesystem */
%let flowloc = content;
filename req temp;
proc stream outfile=req;
begin &streamdelim;
{
  "reference": {
    "type": "&flowloc",
    "path": "&flowname",
    "mediaType": "application/vnd.sas.dataflow"
  },
  "initCode": true,
  "wrapperCode": false
}
;;;;
 
Call the API. We call the API using the request body we prepared in the previous code. The response will be written to the RESP fileref. The code assumes that everything runs just fine. So the flow mentioned actually does exist.
 
/* get the URL to call API's */
%let myviyaurl = %sysfunc(getoption(servicesbaseurl));
%put NOTE: &=myviyaurl;
filename resp temp;
proc http
  method="POST"
  url = "&myviyaurl/studioDevelopment/code"
  in=req
  out= resp
  oauth_bearer=sas_services
verbose
;
  headers
    "Content-Type" = "application/json;charset=utf-8"
    "Accept" = "application/json"
  ;
run;
%put NOTE &=SYS_PROCHTTP_STATUS_CODE;

 

Now one would usually use the JSON libname engine to read the json file and convert it to SAS data sets. However, currently character variables are limited to 32767 bytes in length which is often not long enough for the content.
 
 

Write the code to a file

Let's look at an alternative way to process the resulting json file using CASL. Do not worry, there is no need to learn all the details of CASL. We just make use of some specific functions and statements. See set stdjson, readfilejson2casl and file for more details. CASL statements can be executed with Proc CAS.

 
/*
 * use of CASL to process the json file
 */
/* location where to store the generated code */
%let flowCode = %sysget(HOME)/a-flowcode.sas;
proc cas;
  /* tell CASL to use standard json processing */
  set stdjson;
run;
  /* read the contents of a fileref into a CASL variable */
  resp = readfile("resp");
  /* convert the json structure to a CASL dictionary */
  resp_dict = json2casl(resp);
  /* show the structure of the dictionary (no data for keys) */
  describe resp_dict;
  print(note) "length of code=" length(resp_dict.code);
run;
  /* "print" code to file */
  file mycode "&flowCode";
  print resp_dict.code;
run;
  /* switch back */
  file log;
run;
quit;

 

The code generated by the API is now stored in the file named by the macro variable flowcode.

 

Summary

 

We have seen how we can use CASL code to read a json file, convert to a CASL dictionary and write the value for a given key to a file. In CASL we do not have the length limitation of a character variable in a SAS data set.
The techniques shown here can of course be used for other use cases. See the doc on CASL dictionary to learn more. There is also a casl2json function to convert a CASL dictionary into a json structure.

 

 

Find more articles from SAS Global Enablement and Learning here.

Contributors
Version history
Last update:
17 hours ago
Updated by:

Catch up on SAS Innovate 2026

Dive into keynotes, announcements and breakthroughs on demand.

Explore Now →

SAS AI and Machine Learning Courses

The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.

Get started

Article Tags