Hi All,
Do you know if there is a way to retrieve details about how a job is scheduled while running it?
I have some jobs scheduled to run once per day and some jobs scheduled per hour and need to take different actions depending on what. For example, if Job A runs once per hour I would want it to meet a condition different from jobs scheduled once per day.
We have Sas Viya 3.5 running on Linux Rhel 7.
Regards Mattias
I didn't include a loop in my code to check keep going through all the scheduler job results. The below code has such a loop:
%let baseurl=%sysget(SAS_SERVICES_URL);
filename headout;
filename headout temp;
filename resp;
filename resp temp;
/* Define a macro */
%macro getsched();
/* Call the URL for this job to get its jobRequest ID */
proc http url="&baseurl&SYS_JES_JOB_URI" oauth_bearer=sas_services out=resp headerout=headout HEADEROUT_OVERWRITE;
headers "Accept"="application/json";
run;
/* Read in the response using the JSON libname engine. */
libname resp;
libname resp json fileref=resp;
/* Pull the ID into the macro variable "id" */
proc sql noprint;
select id into:id from resp.jobrequest ;
quit;
/* Call /scheduler/jobs to get all the scheduled jobs. */
proc http oauth_bearer=sas_services url="&baseurl/scheduler/jobs" out=resp headerout=headout HEADEROUT_OVERWRITE;
headers "Accept"="application/json";
run;
/* Read in the response using the JSON libname engine. */
libname resp;
libname resp json fileref=resp;
/* Read in the "next" URL for scheduler if present, this means we might have to keep looking for the jobRequest */
%let next=;
data _null_;
set resp.links;
if rel="next" then call execute('%let next=%nrstr('||href||')');
run;
/* Try to find the ordinal for the job request in the list of schedules. */
proc sql noprint;
select ordinal_items into:ordinal from resp.items_request where uri="/jobExecution/jobRequests/&id/jobs";
quit;
/* If we find it, then create a macro variable "retype" that gives us the type of the reoccurrence (daily,hourly,etc) */
%if %symexist(ordinal) %then %do;
proc sql noprint;
select b.type into:retype from resp.items_triggers as a,resp.triggers_recurrence as b where a.ordinal_items=&ordinal and a.ordinal_triggers=b.ordinal_triggers;
quit;
%end;
/* If not, we need to check the next URL, or stop if there isn't one. */
%else %do %while (%length(&next)>0);;
/* Call the next URL we pulled earlier. */
proc http oauth_bearer=sas_services url="&baseurl&next" out=resp headerout=headout HEADEROUT_OVERWRITE;
headers "Accept"="application/json";
run;
/* Read in the response using the JSON libname engine. */
libname resp;
libname resp json fileref=resp;
/* Empty the next variable from before. */
%let next=;
/* Try to find the ordinal in this response. */
proc sql noprint;
select ordinal_items into:ordinal from resp.items_request where uri="/jobExecution/jobRequests/&id/jobs";
quit;
/* If it exists, write the retype variable. */
%if %symexist(ordinal) %then %do;
proc sql noprint;
select b.type into:retype from resp.items_triggers as a,resp.triggers_recurrence as b where a.ordinal_items=&ordinal and a.ordinal_triggers=b.ordinal_triggers;
quit;
%end;
/* If not, read the next URL to pull the next set of schedules and continue the loop. */
%else %do;
data _null_;
set resp.links;
if rel="next" then call execute('%let next=%nrstr('||href||')');
run;
%end;
%end;
%if %symexist(retype) %then %put NOTE: Reoccurence type: %trim(&retype);
%else %put WARN: No reoccurence set. This job is not scheduled.;
%mend getsched;
%getsched();
Something like this (would probably need to build in a next relation check for the scheduler/jobs call), this sets the macro variable "retype" to whatever the reoccurrence type is (daily, weekly, etc):
%let baseurl=%sysget(SAS_SERVICES_URL);
filename headout;
filename headout temp;
filename resp;
filename resp temp;
proc http url="&baseurl&SYS_JES_JOB_URI" oauth_bearer=sas_services out=resp headerout=headout HEADEROUT_OVERWRITE;
headers "Accept"="application/json";
run;
libname resp;
libname resp json fileref=resp;
proc sql noprint;
select id into:id from resp.jobrequest ;
quit;
proc http oauth_bearer=sas_services url="&baseurl/scheduler/jobs" out=resp headerout=headout HEADEROUT_OVERWRITE;
headers "Accept"="application/json";
run;
libname resp;
libname resp json fileref=resp;
proc sql noprint;
select ordinal_items into:ordinal from resp.items_request where uri="/jobExecution/jobRequests/&id/jobs";
quit;
proc sql noprint;
select b.type into:retype from resp.items_triggers as a,resp.triggers_recurrence as b where a.ordinal_items=&ordinal and a.ordinal_triggers=b.ordinal_triggers;
quit;
Hi, thanks for the code. I'm just having some problems with the Proc http:
proc http oauth_bearer=sas_services url="&baseurl/scheduler/jobs" out=resp headerout=headout HEADEROUT_OVERWRITE;
headers "Accept"="application/json";
run;
It returns 10 obs in resp.items_request but among them are not the job I am searching for (&id).
So this next step returns 0 obs where url=/jobExecution/jobRequests/&id/jobs
proc sql noprint;
select ordinal_items into:ordinal from resp.items_request where uri="/jobExecution/jobRequests/&id/jobs";
quit;
Is there a way to filter proc http to return items just for job identified as &id?
I didn't include a loop in my code to check keep going through all the scheduler job results. The below code has such a loop:
%let baseurl=%sysget(SAS_SERVICES_URL);
filename headout;
filename headout temp;
filename resp;
filename resp temp;
/* Define a macro */
%macro getsched();
/* Call the URL for this job to get its jobRequest ID */
proc http url="&baseurl&SYS_JES_JOB_URI" oauth_bearer=sas_services out=resp headerout=headout HEADEROUT_OVERWRITE;
headers "Accept"="application/json";
run;
/* Read in the response using the JSON libname engine. */
libname resp;
libname resp json fileref=resp;
/* Pull the ID into the macro variable "id" */
proc sql noprint;
select id into:id from resp.jobrequest ;
quit;
/* Call /scheduler/jobs to get all the scheduled jobs. */
proc http oauth_bearer=sas_services url="&baseurl/scheduler/jobs" out=resp headerout=headout HEADEROUT_OVERWRITE;
headers "Accept"="application/json";
run;
/* Read in the response using the JSON libname engine. */
libname resp;
libname resp json fileref=resp;
/* Read in the "next" URL for scheduler if present, this means we might have to keep looking for the jobRequest */
%let next=;
data _null_;
set resp.links;
if rel="next" then call execute('%let next=%nrstr('||href||')');
run;
/* Try to find the ordinal for the job request in the list of schedules. */
proc sql noprint;
select ordinal_items into:ordinal from resp.items_request where uri="/jobExecution/jobRequests/&id/jobs";
quit;
/* If we find it, then create a macro variable "retype" that gives us the type of the reoccurrence (daily,hourly,etc) */
%if %symexist(ordinal) %then %do;
proc sql noprint;
select b.type into:retype from resp.items_triggers as a,resp.triggers_recurrence as b where a.ordinal_items=&ordinal and a.ordinal_triggers=b.ordinal_triggers;
quit;
%end;
/* If not, we need to check the next URL, or stop if there isn't one. */
%else %do %while (%length(&next)>0);;
/* Call the next URL we pulled earlier. */
proc http oauth_bearer=sas_services url="&baseurl&next" out=resp headerout=headout HEADEROUT_OVERWRITE;
headers "Accept"="application/json";
run;
/* Read in the response using the JSON libname engine. */
libname resp;
libname resp json fileref=resp;
/* Empty the next variable from before. */
%let next=;
/* Try to find the ordinal in this response. */
proc sql noprint;
select ordinal_items into:ordinal from resp.items_request where uri="/jobExecution/jobRequests/&id/jobs";
quit;
/* If it exists, write the retype variable. */
%if %symexist(ordinal) %then %do;
proc sql noprint;
select b.type into:retype from resp.items_triggers as a,resp.triggers_recurrence as b where a.ordinal_items=&ordinal and a.ordinal_triggers=b.ordinal_triggers;
quit;
%end;
/* If not, read the next URL to pull the next set of schedules and continue the loop. */
%else %do;
data _null_;
set resp.links;
if rel="next" then call execute('%let next=%nrstr('||href||')');
run;
%end;
%end;
%if %symexist(retype) %then %put NOTE: Reoccurence type: %trim(&retype);
%else %put WARN: No reoccurence set. This job is not scheduled.;
%mend getsched;
%getsched();
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.