Hello community!
I have some REST API query that creates response file:
PROC HTTP METHOD="GET" oauth_bearer=sas_services OUT=rep_id
URL="http://pvmict002.health.fgov.be/jobDefinitions/definitions/7a0c60aa-393f-42a8-96b7-21831400f4b4";
HEADERS "Accept"="application/vnd.sas.job.definition+json";
RUN;
If we open the JSON response file with text editor we will see that content combined in a single line.
When I try to extract this content using JSON libname, the JSON file is automatically parsed to "human readable" multiple lines.
The code:
libname rep_id json;
data code;
set rep_id.ROOT;
keep code;
run;
The output table:
The question is:
How can I convert code column(that contains multiple lines) to one-line string, the same we have in JSON file?
Maybe you have other ideas how to reuse this part of the JSON file to build another input JSON file for POST request?
If possible I would like to avoid assigning the "code" parameter into macro variable to not exceed 65k characters.
Regards,
Ivan
You could replace the newline and tab characters \n and \t with their literal values by specifying \\n in the replacement field for prxchange.
For example:
data items;
set init.root;
code=prxchange('s/\n/\\n/', -1, code);
code=prxchange('s/\t/\\t/', -1, code);
keep code;
run;
Replace \\n and \\t with nothing and it will remove them.
data items;
set init.root;
code=prxchange('s/\n//', -1, code);
code=prxchange('s/\t//', -1, code);
keep code;
run;
It looks like the job definition includes format characters: \n and \t. Perhaps you could remove them in your data step, maybe something like:
libname rep_id json;
data code;
set rep_id.ROOT;
code=prxchange('s/\n\t//', -1, code);
keep code;
run;
It is hard the tell from photographs of GUI screens what is actually in your data.
It sounds like most likely the JSON engine has converted the "\n" in the source string into the actual new line characters. The values are still in one variable since they appear in only once cell in that last photograph.
What is it that you want to do with the value now that you have it in the CODE variable in the WORK.CODE dataset?
Are you going to write it to a file?
Move it to some other database?
Try to execute it as SAS code? SAS doesn't care whether you have extra line breaks in the code.
Note that the 64K limit on macro variables is probably going to be less of problem than the 32K limit on character variables in SAS datasets.
@Tom @gwootton thanks for your responses
1. The main plan to have a macro that can do a copy (B) of Job Definition (A) with the same code and the parameters. I had this question some time ago and closed it a bit too early, before solving all the steps (Solved: Re: Copy Job Definition using REST API or CLI - SAS Support Communities)
My plan is:
GET <server>/jobDefinitions/definitions/
1. I have a job with this code:
%let test_var=Hello SAS!;
%put &test_var;
/* We can add some comment */
data a;
set sashelp.class;
run;
proc print data=a;
quit;
2. I can export the content of this job with the parameters using REST API query:
PROC HTTP METHOD="GET" oauth_bearer=sas_services OUT=rep_id
URL="http://pvmict002.health.fgov.be/jobDefinitions/definitions/7a0c60aa-393f-42a8-96b7-21831400f4b4";
HEADERS "Accept"="application/vnd.sas.job.definition+json";
RUN;
After the extraction the code from the Job:
The full file rep_id.txt attached.
3. We need to create a new JSON file. In the line "code" put the code extracted in previous step. The new input file should look like:
{ "version":1, "name":"New Job with old code", "description":"Some description", "type":"Compute", "parameters":[{"version": 1, "name": "_contextName", "defaultValue": "SAS Job Execution compute context", "type": "CHARACTER", "label": "Context Name", "required": false}], "code":"%let test_var=Hello SAS!;\n%put &test_var;\n/* We can add some comment */\n\tset sashelp.class;\nrun;\nproc print data=a; \nquit;" }
I'm looking for a good way to extract the "code" value and put it into the file. Perfectly to not have limitations of 65k of macro length and 32k of max size of column (thanks for a good remark).
It seems we have to exchange \n \t symbols to text(not to delete). I'm also not sure it would be enough or we need some extra "masking" of special characters. When I created a question I was thinking about some sas function similar to htmlencode(string) but for JSON. But it probably was the wrong way, and I should check another solution.
You could replace the newline and tab characters \n and \t with their literal values by specifying \\n in the replacement field for prxchange.
For example:
data items;
set init.root;
code=prxchange('s/\n/\\n/', -1, code);
code=prxchange('s/\t/\\t/', -1, code);
keep code;
run;
Replace \\n and \\t with nothing and it will remove them.
data items;
set init.root;
code=prxchange('s/\n//', -1, code);
code=prxchange('s/\t//', -1, code);
keep code;
run;
The SAS Users Group for Administrators (SUGA) is open to all SAS administrators and architects who install, update, manage or maintain a SAS deployment.
SAS technical trainer Erin Winters shows you how to explore assets, create new data discovery agents, schedule data discovery agents, and much more.
Find more tutorials on the SAS Users YouTube channel.