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 token
27  @param [in] grant_type= valid values:
28 
29  * password
30  * authorization_code
31  * detect - will check if access_token exists, if not will use sas_services if
32  a SASStudioV session else authorization_code. Default option.
33  * 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: `"name":"value"`
38  or "name":"value","name2":42`. This will need to be wrapped in `%str()`.
39 
40  @param [in] contextName= Context name with which to run the job.
41  Default = `SAS Job Execution compute context`
42 
43  @param [out] outds= The output dataset containing links (Default=work.mv_jobexecute)
44 
45 
46  @version VIYA V.03.04
47  @author Allan Bowe, source: https://github.com/sasjs/core
48 
49  <h4> SAS Macros </h4>
50  @li mp_abort.sas
51  @li mf_getplatform.sas
52  @li mf_getuniquefileref.sas
53  @li mf_getuniquelibref.sas
54  @li mv_getfoldermembers.sas
55 
56 **/
57 
58 %macro mv_jobexecute(path=0
59  ,name=0
60  ,contextName=SAS Job Execution compute context
61  ,access_token_var=ACCESS_TOKEN
62  ,grant_type=sas_services
63  ,paramstring=0
64  ,outds=work.mv_jobexecute
65  );
66 %local oauth_bearer;
67 %if &grant_type=detect %then %do;
68  %if %symexist(&access_token_var) %then %let grant_type=authorization_code;
69  %else %let grant_type=sas_services;
70 %end;
71 %if &grant_type=sas_services %then %do;
72  %let oauth_bearer=oauth_bearer=sas_services;
73  %let &access_token_var=;
74 %end;
75 
76 %mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
77  and &grant_type ne sas_services
78  )
79  ,mac=&sysmacroname
80  ,msg=%str(Invalid value for grant_type: &grant_type)
81 )
82 
83 %mp_abort(iftrue=("&path"="0")
84  ,mac=&sysmacroname
85  ,msg=%str(Path not provided)
86 )
87 %mp_abort(iftrue=("&name"="0")
88  ,mac=&sysmacroname
89  ,msg=%str(Job Name not provided)
90 )
91 
92 options noquotelenmax;
93 
94 %local base_uri; /* location of rest apis */
95 %let base_uri=%mf_getplatform(VIYARESTAPI);
96 
97 data;run;
98 %local foldermembers;
99 %let foldermembers=&syslast;
100 %mv_getfoldermembers(root=&path
101  ,access_token_var=&access_token_var
102  ,grant_type=&grant_type
103  ,outds=&foldermembers
104 )
105 
106 %local joburi;
107 %let joburi=0;
108 data _null_;
109  set &foldermembers;
110  if name="&name" and uri=:'/jobDefinitions/definitions'
111  then call symputx('joburi',uri);
112 run;
113 
114 %mp_abort(iftrue=("&joburi"="0")
115  ,mac=&sysmacroname
116  ,msg=%str(Job &path/&name not found)
117 )
118 
119 /* prepare request*/
120 %local fname0 fname1;
121 %let fname0=%mf_getuniquefileref();
122 %let fname1=%mf_getuniquefileref();
123 
124 data _null_;
125  file &fname0;
126  length joburi contextname $128 paramstring $32765;
127  joburi=quote(trim(symget('joburi')));
128  contextname=quote(trim(symget('contextname')));
129  _program=quote("&path/&name");
130  paramstring=symget('paramstring');
131  put '{"jobDefinitionUri":' joburi ;
132  put ' ,"arguments":{"_contextName":' contextname;
133  put ' ,"_program":' _program;
134  if paramstring ne "0" then do;
135  put ' ,' paramstring;
136  end;
137  put '}}';
138 run;
139 
140 proc http method='POST' in=&fname0 out=&fname1 &oauth_bearer
141  url="&base_uri/jobExecution/jobs";
142  headers "Content-Type"="application/vnd.sas.job.execution.job.request+json"
143  "Accept"="application/vnd.sas.job.execution.job+json"
144  %if &grant_type=authorization_code %then %do;
145  "Authorization"="Bearer &&&access_token_var"
146  %end;
147  ;
148 run;
149 %if &SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 201 %then
150 %do;
151  data _null_;infile &fname0;input;putlog _infile_;run;
152  data _null_;infile &fname1;input;putlog _infile_;run;
153  %mp_abort(mac=&sysmacroname
154  ,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
155  )
156 %end;
157 
158 %local libref;
159 %let libref=%mf_getuniquelibref();
160 libname &libref JSON fileref=&fname1;
161 
162 data &outds;
163  set &libref..links;
164  _program="&path/&name";
165 run;
166 
167 /* clear refs */
168 filename &fname0 clear;
169 filename &fname1 clear;
170 libname &libref;
171 
172 %mend;