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.
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
}
;;;;
/* 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;
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, readfile, json2casl 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.
Find more articles from SAS Global Enablement and Learning here.
Dive into keynotes, announcements and breakthroughs on demand.
Explore Now →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.