BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
idziemianczyk
Obsidian | Level 7

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. 

dandycomp_0-1614866977933.png

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:

dandycomp_1-1614867318699.png

 

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

1 ACCEPTED SOLUTION

Accepted Solutions
gwootton
SAS Super FREQ

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;

demo.png

--
Greg Wootton | Principal Systems Technical Support Engineer

View solution in original post

5 REPLIES 5
gwootton
SAS Super FREQ

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;
--
Greg Wootton | Principal Systems Technical Support Engineer
Tom
Super User Tom
Super User

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.

Tom
Super User Tom
Super User

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.

idziemianczyk
Obsidian | Level 7

 

@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:

  1. Extract CODE and PARAMETERS from the the Job (A). We can do it with 
    GET <server>/jobDefinitions/definitions/
  2. Create an input JSON file for Job creation, using code and parameters from the Job (A), new name of the job and parent folder.  There is still 1 step do be done, I would like to set the input parameters for Job(B) creation based on a code from job (A).
  3. Create a Job B based on the new input file.

 

 

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:

dandycomp_0-1614934453552.png

 

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).

 

@gwootton 

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. 

 

 

 

 

gwootton
SAS Super FREQ

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;

demo.png

--
Greg Wootton | Principal Systems Technical Support Engineer

suga badge.PNGThe SAS Users Group for Administrators (SUGA) is open to all SAS administrators and architects who install, update, manage or maintain a SAS deployment. 

Join SUGA 

Get Started with SAS Information Catalog in SAS Viya

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.

Discussion stats
  • 5 replies
  • 4781 views
  • 1 like
  • 3 in conversation