Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mv_jobexecute.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Executes a SAS Viya Job
4 @details Triggers a SAS Viya Job, with optional URL parameters, using
5 the JES web app.
6
7 First, compile the macros:
8
9 filename mc url
10 "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
11 %inc mc;
12
13 Then, execute the job!
14
15 %mv_jobexecute(path=/Public/folder
16 ,name=somejob
17 )
18
19 Example with parameters:
20
21 %mv_jobexecute(path=/Public/folder
22 ,name=somejob
23 ,paramstring=%str("macvarname":"macvarvalue","answer":42)
24 )
25
26 @param [in] access_token_var= The global macro variable to contain the access
27 token
28 @param [in] grant_type= valid values:
29 @li password
30 @li authorization_code
31 @li detect - will check if access_token exists, if not will use sas_services
32 if a SASStudioV session else authorization_code. Default option.
33 @li sas_services - will use oauth_bearer=sas_services
34
35 @param [in] path= The SAS Drive path to the job being executed
36 @param [in] name= The name of the job to execute
37 @param [in] paramstring= A JSON fragment with name:value pairs, eg:
38 `"name":"value"` or "name":"value","name2":42`. This will need to be
39 wrapped in `%str()`.
40
41 @param [in] contextName= Context name with which to run the job.
42 Default = `SAS Job Execution compute context`
43 @param [in] mdebug= set to 1 to enable DEBUG messages
44 @param [out] outds= (work.mv_jobexecute) The output dataset containing links
45
46
47 @version VIYA V.03.04
48 @author Allan Bowe, source: https://github.com/sasjs/core
49
50 <h4> SAS Macros </h4>
51 @li mp_abort.sas
52 @li mf_getplatform.sas
53 @li mf_getuniquefileref.sas
54 @li mf_getuniquelibref.sas
55 @li mv_getfoldermembers.sas
56
57**/
58
59%macro mv_jobexecute(path=0
60 ,name=0
61 ,contextName=SAS Job Execution compute context
62 ,access_token_var=ACCESS_TOKEN
63 ,grant_type=sas_services
64 ,paramstring=0
65 ,outds=work.mv_jobexecute
66 ,mdebug=0
67 );
68%local dbg;
69%if &mdebug=1 %then %do;
70 %put &sysmacroname entry vars:;
71 %put _local_;
72%end;
73%else %let dbg=*;
74
75%local oauth_bearer;
76%if &grant_type=detect %then %do;
77 %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
78 %else %let grant_type=sas_services;
79%end;
80%if &grant_type=sas_services %then %do;
81 %let oauth_bearer=oauth_bearer=sas_services;
82 %let &access_token_var=;
83%end;
84
85%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
86 and &grant_type ne sas_services
87 )
88 ,mac=&sysmacroname
89 ,msg=%str(Invalid value for grant_type: &grant_type)
90)
91
92%mp_abort(iftrue=("&path"="0")
93 ,mac=&sysmacroname
94 ,msg=%str(Path not provided)
95)
96%mp_abort(iftrue=("&name"="0")
97 ,mac=&sysmacroname
98 ,msg=%str(Job Name not provided)
99)
100
101options noquotelenmax;
102
103%local base_uri; /* location of rest apis */
104%let base_uri=%mf_getplatform(VIYARESTAPI);
105
106data;run;
107%local foldermembers;
108%let foldermembers=&syslast;
109%mv_getfoldermembers(root=&path
110 ,access_token_var=&access_token_var
111 ,grant_type=&grant_type
112 ,outds=&foldermembers
113)
114
115%local joburi;
116%let joburi=0;
117data _null_;
118 set &foldermembers;
119 if name="&name" and uri=:'/jobDefinitions/definitions'
120 then call symputx('joburi',uri);
121run;
122
123%mp_abort(iftrue=("&joburi"="0")
124 ,mac=&sysmacroname
125 ,msg=%str(Job &path/&name not found)
126)
127
128/* prepare request*/
129%local fname0 fname1;
130%let fname0=%mf_getuniquefileref();
131%let fname1=%mf_getuniquefileref();
132
133data _null_;
134 file &fname0;
135 length joburi contextname $128 paramstring $32765;
136 joburi=quote(trim(symget('joburi')));
137 contextname=quote(trim(symget('contextname')));
138 _program=quote("&path/&name");
139 paramstring=symget('paramstring');
140 put '{"jobDefinitionUri":' joburi ;
141 put ' ,"arguments":{"_contextName":' contextname;
142 put ' ,"_program":' _program;
143 if paramstring ne "0" then do;
144 put ' ,' paramstring;
145 end;
146 put '}}';
147run;
148
149proc http method='POST' in=&fname0 out=&fname1 &oauth_bearer
150 url="&base_uri/jobExecution/jobs";
151 headers "Content-Type"="application/vnd.sas.job.execution.job.request+json"
152 "Accept"="application/vnd.sas.job.execution.job+json"
153 %if &grant_type=authorization_code %then %do;
154 "Authorization"="Bearer &&&access_token_var"
155 %end;
156 ;
157run;
158%if &SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 201 %then
159%do;
160 data _null_;infile &fname0;input;putlog _infile_;run;
161 data _null_;infile &fname1;input;putlog _infile_;run;
162 %mp_abort(mac=&sysmacroname
163 ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
164 )
165%end;
166
167%local libref;
168%let libref=%mf_getuniquelibref();
169libname &libref JSON fileref=&fname1;
170
171data &outds;
172 set &libref..links;
173 _program="&path/&name";
174run;
175
176%if &mdebug=1 %then %do;
177 %put &sysmacroname exit vars:;
178 %put _local_;
179%end;
180%else %do;
181 /* clear refs */
182 filename &fname0 clear;
183 filename &fname1 clear;
184 libname &libref;
185%end;
186%mend mv_jobexecute;