68%macro mv_createjob(path=
70 ,desc=Created by the mv_createjob.sas macro
73 ,access_token_var=ACCESS_TOKEN
74 ,grant_type=sas_services
78 ,addjesbeginendmacros=
false
81%
if &grant_type=detect %then %
do;
82 %
if %symexist(&access_token_var) %then %let grant_type=authorization_code;
83 %
else %let grant_type=sas_services;
85%
if &grant_type=sas_services %then %
do;
86 %let oauth_bearer=oauth_bearer=sas_services;
87 %let &access_token_var=;
91%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
92 and &grant_type ne sas_services
95 ,msg=%str(Invalid value
for grant_type: &grant_type)
97%mp_abort(iftrue=(%mf_isblank(&path)=1)
99 ,msg=%str(path value must be provided)
101%mp_abort(iftrue=(%length(&path)=1)
103 ,msg=%str(path value must be provided)
105%mp_abort(iftrue=(%mf_isblank(&name)=1)
107 ,msg=%str(name value must be provided)
110options noquotelenmax;
112*
remove any trailing slash ;
113%
if "%substr(&path,%length(&path),1)" =
"/" %then
114 %let path=%substr(&path,1,%length(&path)-1);
117%put &sysmacroname: Path &path being checked / created;
118%mv_createfolder(path=&path)
121%let base_uri=%mf_getplatform(VIYARESTAPI);
125%let fname1=%mf_getuniquefileref();
126proc http method=
'GET' out=&fname1 &oauth_bearer
127 url=
"&base_uri/folders/folders/@item?path=&path";
128%
if &grant_type=authorization_code %then %
do;
129 headers
"Authorization"=
"Bearer &&&access_token_var";
139%mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200)
141 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
146%let libref1=%mf_getuniquelibref();
147libname &libref1 JSON fileref=&fname1;
151 if rel=
'members' then call symputx(
'membercheck',quote(
"&base_uri"!!trim(href)),
'l');
152 else if rel=
'self' then call symputx(
'parentFolderUri',href,
'l');
156 call symputx(
'folderid',
id,
'l');
159%let fname2=%mf_getuniquefileref();
160proc http method=
'GET'
163 url=%unquote(%superq(membercheck));
165 %
if &grant_type=authorization_code %then %
do;
166 "Authorization"=
"Bearer &&&access_token_var"
168 'Accept'=
'application/vnd.sas.collection+json'
169 'Accept-Language'=
'string';
170%
if &debug=1 %then %
do;
175%mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 200)
177 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
180%
if %upcase(&replace)=YES %then %
do;
181 %mv_deletejes(path=&path, name=&name)
186 %let libref2=%mf_getuniquelibref();
187 libname &libref2 JSON fileref=&fname2;
188 %local exists; %let exists=0;
191 if contenttype=
'jobDefinition' and upcase(name)=
"%upcase(&name)" then
192 call symputx(
'exists',1,
'l');
194 %mp_abort(iftrue=(&exists=1)
196 ,msg=%str(Job &name already exists in &path)
198 libname &libref2 clear;
203%let fname3=%mf_getuniquefileref();
205 file &fname3 TERMSTR=
' ';
206 length
string $32767;
207 string=cats(
'{"version": 0,"name":"'
209 ,
'","type":"Compute","parameters":['
210%
if &addjesbeginendmacros ne 0 %then %
do;
211 ,
'{"name":"_addjesbeginendmacros"'
212 ,
',"type":"CHARACTER","defaultValue":"'
213 ,
"&addjesbeginendmacros"
218 context=quote(cats(symget(
'contextname')));
219 if context ne
'""' then
do;
222 ,
'{"version": 1,"name": "_contextName","defaultValue":'
223 ,context,
',"type":"CHARACTER","label":"Context Name","required": false}'
226 string=cats(
string,
'],"code":"');
232%local x fref freflist;
233%let freflist= &precode &code ;
234%
do x=1 %to %sysfunc(countw(&freflist));
235 %let fref=%scan(&freflist,&x);
236 %put &sysmacroname: adding &fref;
238 length filein 8 fileid 8;
239 filein = fopen(
"&fref",
"I",1,
"B");
240 fileid = fopen(
"&fname3",
"A",1,
"B");
242 do while(fread(filein)=0);
243 rc = fget(filein,rec,1);
245 rc =fput(fileid,
'\');rc =fwrite(fileid);
246 rc =fput(fileid,
'"');rc =fwrite(fileid);
248 else if rec=
'0A'x then
do;
249 rc =fput(fileid,
'\');rc =fwrite(fileid);
250 rc =fput(fileid,
'n');rc =fwrite(fileid);
252 else if rec=
'0D'x then
do;
253 rc =fput(fileid,
'\');rc =fwrite(fileid);
254 rc =fput(fileid,
'r');rc =fwrite(fileid);
256 else if rec=
'09'x then
do;
257 rc =fput(fileid,
'\');rc =fwrite(fileid);
258 rc =fput(fileid,
't');rc =fwrite(fileid);
260 else if rec=
'5C'x then
do;
261 rc =fput(fileid,
'\');rc =fwrite(fileid);
262 rc =fput(fileid,
'\');rc =fwrite(fileid);
264 else if rec=
'01'x then
do;
265 rc =fput(fileid,
'\');rc =fwrite(fileid);
266 rc =fput(fileid,
'u');rc =fwrite(fileid);
267 rc =fput(fileid,
'0');rc =fwrite(fileid);
268 rc =fput(fileid,
'0');rc =fwrite(fileid);
269 rc =fput(fileid,
'0');rc =fwrite(fileid);
270 rc =fput(fileid,
'1');rc =fwrite(fileid);
272 else if rec=
'07'x then
do;
273 rc =fput(fileid,
'\');rc =fwrite(fileid);
274 rc =fput(fileid,
'u');rc =fwrite(fileid);
275 rc =fput(fileid,
'0');rc =fwrite(fileid);
276 rc =fput(fileid,
'0');rc =fwrite(fileid);
277 rc =fput(fileid,
'0');rc =fwrite(fileid);
278 rc =fput(fileid,
'7');rc =fwrite(fileid);
280 else if rec=
'1B'x then
do;
281 rc =fput(fileid,
'\');rc =fwrite(fileid);
282 rc =fput(fileid,
'u');rc =fwrite(fileid);
283 rc =fput(fileid,
'0');rc =fwrite(fileid);
284 rc =fput(fileid,
'0');rc =fwrite(fileid);
285 rc =fput(fileid,
'1');rc =fwrite(fileid);
286 rc =fput(fileid,
'B');rc =fwrite(fileid);
289 rc =fput(fileid,rec);
300 file &fname3 mod TERMSTR=
' ';
306%let fname4=%mf_getuniquefileref();
307proc http method=
'POST'
311 url=
"&base_uri/jobDefinitions/definitions?parentFolderUri=&parentFolderUri";
312 headers
'Content-Type'=
'application/vnd.sas.job.definition+json'
313 %
if &grant_type=authorization_code %then %
do;
314 "Authorization"=
"Bearer &&&access_token_var"
316 "Accept"=
"application/vnd.sas.job.definition+json";
317%
if &debug=1 %then %
do;
322%mp_abort(iftrue=(&SYS_PROCHTTP_STATUS_CODE ne 201)
324 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
327filename &fname1 clear;
328filename &fname2 clear;
329filename &fname3 clear;
330filename &fname4 clear;
331libname &libref1 clear;
336 if symexist(
'_baseurl') then do;
337 url=symget('_baseurl');
338 if subpad(url,length(url)-9,9)='SASStudio'
339 then url=substr(url,1,length(url)-11);
340 else url=
"&systcpiphostname";
342 else url=
"&systcpiphostname";
343 call symputx('url',url);
347%put &sysmacroname: Job &name successfully created in &path;
349%put &sysmacroname: Check it out here:;
350%put &sysmacroname:;%put;
351%put &url/SASJobExecution?_PROGRAM=&path/&name;%put;