mirror of
https://github.com/sasjs/core.git
synced 2025-12-11 06:24:35 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d9954ae777 | |||
| 364dc9f07f | |||
| d96125c3cf | |||
| 506695be56 | |||
|
|
45f858db15 | ||
|
|
b4d97a063a | ||
|
|
4df8f3b4c2 | ||
|
|
11aa484996 | ||
|
|
b9fd79bd5e | ||
|
|
1beb30d0ff |
43
README.md
43
README.md
@@ -40,6 +40,27 @@ Documentation: https://sasjs.github.io/core.github.io/files.html
|
||||
- X command enabled
|
||||
- Prefixes: _mmw_,_mmu_,_mmx_
|
||||
|
||||
**lua** library
|
||||
|
||||
Wait - this is a macro library - what is LUA doing here? Well, it is a little known fact that you CAN run LUA within a SAS Macro. It has to be written to a text file with a `.lua` extension, from where you can `%include` it. So, without using the `proc lua` wrapper.
|
||||
|
||||
To contribute, simply write your freeform LUA in the LUA folder. Then run the `build.py`, which will convert your LUA into a data step with put statements, and create the macro wrapper with a `ml_` prefix. You can then use your module in any program by running:
|
||||
|
||||
```
|
||||
/* compile the lua module */
|
||||
%ml_yourmodule()
|
||||
|
||||
/* Execute. Do not use the restart keyword! */
|
||||
proc lua;
|
||||
submit;
|
||||
print(yourStuff);
|
||||
endsubmit;
|
||||
run;
|
||||
```
|
||||
|
||||
- X command enabled
|
||||
- Prefixes: _mmw_,_mmu_,_mmx_
|
||||
|
||||
# Installation
|
||||
|
||||
First, download the repo to a location your SAS system can access. Then update your sasautos path to include the components you wish to have available,eg:
|
||||
@@ -72,6 +93,7 @@ filename mc url "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
|
||||
- _mm_ for metadata macros (interface with the metadata server).
|
||||
- _mmx_ for macros that use metadata and are XCMD enabled
|
||||
- _mx_ for macros that are XCMD enabled
|
||||
- _ml_ for macros that are used to compile LUA modules
|
||||
- _mv_ for macros that will only work in Viya
|
||||
- follow verb-noun convention
|
||||
- unix style line endings (lf)
|
||||
@@ -91,7 +113,25 @@ The **Macro Core** documentation is created using [doxygen](http://www.doxygen.n
|
||||
- version. The EARLIEST SAS version in which this macro is known to work.
|
||||
- author. Author name, contact details optional
|
||||
|
||||
All macros must be commented in the doxygen format, to enable the [online documentation](https://sasjs.github.io/core.github.io/).
|
||||
All macros must be commented in the doxygen format, to enable the [online documentation](https://core.sasjs.io).
|
||||
|
||||
### Dependencies
|
||||
SAS code can contain one of two types of dependency - SAS Macros, and SAS Programs. When compiling projects using the [SASjs CLI](https://cli.sasjs.io) the doxygen header is scanned for ` @li` items under the following headers:
|
||||
|
||||
```
|
||||
<h4> SAS Macros </h4>
|
||||
@li mf_nobs.sas
|
||||
@li mm_assignlib.sas
|
||||
|
||||
<h4> SAS Programs </h4>
|
||||
@li somefile.ddl SOMEFREF
|
||||
@li someprogram.sas FREFTWO
|
||||
```
|
||||
|
||||
The CLI can then extract all the dependencies and insert as precode (SAS Macros) or in a temp engine fileref (SAS Programs) when creating SAS Jobs and Services.
|
||||
|
||||
When contributing to this library, it is therefore important to ensure that all dependencies are listed in the header in this format.
|
||||
|
||||
|
||||
## Coding Standards
|
||||
|
||||
@@ -102,6 +142,7 @@ All macros must be commented in the doxygen format, to enable the [online docume
|
||||
- Mandatory parameters should be positional, all optional parameters should be keyword (var=) style.
|
||||
- All dataset references must be 2 level (eg `work.blah`, not `blah`). This is to avoid contention when options [DATASTMTCHK](https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000279064.htm)=ALLKEYWORDS is in effect.
|
||||
- Avoid naming collisions! All macro variables should be local scope. Use system generated work tables where possible - eg `data ; set sashelp.class; run; data &output; set &syslast; run;`
|
||||
- If you have a long-running SQL query, the use of a `quit;` statement is recommended in order to benefit from the timing statistics.
|
||||
|
||||
# General Notes
|
||||
|
||||
|
||||
516
all.sas
516
all.sas
@@ -5704,6 +5704,7 @@ run;
|
||||
rc=metadata_getattr(liburi,"Name",LibName);
|
||||
/* now try and assign it */
|
||||
if libname("&libref",,'meta',cats('liburi="',liburi,'";')) ne 0 then do;
|
||||
putlog "&libref could not be assigned";
|
||||
call symputx('msg',sysmsg(),'l');
|
||||
if "&mabort"='HARD' then call symputx('mp_abort',1,'l');
|
||||
end;
|
||||
@@ -8112,17 +8113,15 @@ run;
|
||||
blank to return all groups.
|
||||
Usage:
|
||||
|
||||
- all groups
|
||||
%mm_getGroups()
|
||||
- all groups: `%mm_getGroups()`
|
||||
|
||||
- all groups for a particular user
|
||||
%mm_getgroups(user=&sysuserid)
|
||||
- all groups for a particular user: `%mm_getgroups(user=&sysuserid)`
|
||||
|
||||
@param user= the metadata user to return groups for. Leave blank for all
|
||||
@param [in] user= the metadata user to return groups for. Leave blank for all
|
||||
groups.
|
||||
@param outds= the dataset to create that contains the list of groups
|
||||
@param repo= the metadata repository that contains the user/group information
|
||||
@param mDebug= set to 1 to show debug messages in the log
|
||||
@param [in] repo= the metadata repository that contains the user/group information
|
||||
@param [in] mDebug= set to 1 to show debug messages in the log
|
||||
@param [out] outds= the dataset to create that contains the list of groups
|
||||
|
||||
@returns outds dataset containing all groups in a column named "metagroup"
|
||||
- groupuri
|
||||
@@ -8570,9 +8569,9 @@ libname _XML_ clear;
|
||||
|
||||
Usage:
|
||||
|
||||
%mm_getroles()
|
||||
%mm_getroles()
|
||||
|
||||
@param outds the dataset to create that contains the list of roles
|
||||
@param [out] outds the dataset to create that contains the list of roles
|
||||
|
||||
@returns outds dataset containing all roles, with the following columns:
|
||||
- uri
|
||||
@@ -11489,8 +11488,8 @@ filename &fname1a clear;
|
||||
libname &libref1a clear;
|
||||
|
||||
%mend;/**
|
||||
@file mv_deletejes.sas
|
||||
@brief Creates a job execution service if it does not already exist
|
||||
@file
|
||||
@brief Deletes a Viya Job, if it exists
|
||||
@details If not executed in Studio 5+ will expect oauth token in a global
|
||||
macro variable (default ACCESS_TOKEN).
|
||||
|
||||
@@ -12248,7 +12247,157 @@ run;
|
||||
filename &fname1 clear;
|
||||
libname &libref1 clear;
|
||||
|
||||
%mend; /**
|
||||
%mend;/**
|
||||
@file
|
||||
@brief Extract the source code from a SAS Viya Job
|
||||
@details Extracts the SAS code from a Job into a fileref or physical file.
|
||||
Example:
|
||||
|
||||
%mv_getjobcode(
|
||||
path=/Public/jobs
|
||||
,name=some_job
|
||||
,outfile=/tmp/some_job.sas
|
||||
)
|
||||
|
||||
@param [in] access_token_var= The global macro variable to contain the access token
|
||||
@param [in] grant_type= valid values:
|
||||
* password
|
||||
* authorization_code
|
||||
* detect - will check if access_token exists, if not will use sas_services if
|
||||
a SASStudioV session else authorization_code. Default option.
|
||||
* sas_services - will use oauth_bearer=sas_services
|
||||
@param [in] path= The SAS Drive path of the job
|
||||
@param [in] name= The name of the job
|
||||
@param [out] outref= A fileref to which to write the source code
|
||||
@param [out] outfile= A file to which to write the source code
|
||||
|
||||
@version VIYA V.03.04
|
||||
@author Allan Bowe, source: https://github.com/sasjs/core
|
||||
|
||||
<h4> SAS Macros </h4>
|
||||
@li mp_abort.sas
|
||||
@li mf_getplatform.sas
|
||||
@li mf_getuniquefileref.sas
|
||||
@li mv_getfoldermembers.sas
|
||||
@li ml_json.sas
|
||||
|
||||
**/
|
||||
|
||||
%macro mv_getjobcode(outref=0,outfile=0
|
||||
,name=0,path=0
|
||||
,contextName=SAS Job Execution compute context
|
||||
,access_token_var=ACCESS_TOKEN
|
||||
,grant_type=sas_services
|
||||
);
|
||||
%local oauth_bearer;
|
||||
%if &grant_type=detect %then %do;
|
||||
%if %symexist(&access_token_var) %then %let grant_type=authorization_code;
|
||||
%else %let grant_type=sas_services;
|
||||
%end;
|
||||
%if &grant_type=sas_services %then %do;
|
||||
%let oauth_bearer=oauth_bearer=sas_services;
|
||||
%let &access_token_var=;
|
||||
%end;
|
||||
%put &sysmacroname: grant_type=&grant_type;
|
||||
%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
|
||||
and &grant_type ne sas_services
|
||||
)
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(Invalid value for grant_type: &grant_type)
|
||||
)
|
||||
%mp_abort(iftrue=("&path"="0")
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(Job Path not provided)
|
||||
)
|
||||
%mp_abort(iftrue=("&name"="0")
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(Job Name not provided)
|
||||
)
|
||||
%mp_abort(iftrue=("&outfile"="0" and "&outref"="0")
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(Output destination (file or fileref) must be provided)
|
||||
)
|
||||
options noquotelenmax;
|
||||
%local base_uri; /* location of rest apis */
|
||||
%let base_uri=%mf_getplatform(VIYARESTAPI);
|
||||
data;run;
|
||||
%local foldermembers;
|
||||
%let foldermembers=&syslast;
|
||||
%mv_getfoldermembers(root=&path
|
||||
,access_token_var=&access_token_var
|
||||
,grant_type=&grant_type
|
||||
,outds=&foldermembers
|
||||
)
|
||||
%local joburi;
|
||||
%let joburi=0;
|
||||
data _null_;
|
||||
set &foldermembers;
|
||||
if name="&name" and uri=:'/jobDefinitions/definitions'
|
||||
then call symputx('joburi',uri);
|
||||
run;
|
||||
%mp_abort(iftrue=("&joburi"="0")
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(Job &path/&name not found)
|
||||
)
|
||||
|
||||
/* prepare request*/
|
||||
%local fname1;
|
||||
%let fname1=%mf_getuniquefileref();
|
||||
proc http method='GET' out=&fname1 &oauth_bearer
|
||||
url="&base_uri&joburi";
|
||||
headers "Accept"="application/vnd.sas.job.definition+json"
|
||||
%if &grant_type=authorization_code %then %do;
|
||||
"Authorization"="Bearer &&&access_token_var"
|
||||
%end;
|
||||
;
|
||||
run;
|
||||
%if &SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 201 %then
|
||||
%do;
|
||||
data _null_;infile &fname1;input;putlog _infile_;run;
|
||||
%mp_abort(mac=&sysmacroname
|
||||
,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
|
||||
)
|
||||
%end;
|
||||
%local fname2 fname3 fpath1 fpath2 fpath3;
|
||||
%let fname2=%mf_getuniquefileref();
|
||||
%let fname3=%mf_getuniquefileref();
|
||||
%let fpath1=%sysfunc(pathname(&fname1));
|
||||
%let fpath2=%sysfunc(pathname(&fname2));
|
||||
%let fpath3=%sysfunc(pathname(&fname2));
|
||||
|
||||
/* compile the lua JSON module */
|
||||
%ml_json()
|
||||
/* read using LUA - this allows the code to be of any length */
|
||||
data _null_;
|
||||
file "&fpath3..lua";
|
||||
put '
|
||||
infile = io.open (sas.symget("fpath1"), "r")
|
||||
outfile = io.open (sas.symget("fpath2"), "w")
|
||||
io.input(infile)
|
||||
local resp=json.decode(io.read())
|
||||
local job=resp["code"]
|
||||
outfile:write(job)
|
||||
io.close(infile)
|
||||
io.close(outfile)
|
||||
';
|
||||
run;
|
||||
%inc "&fpath3..lua";
|
||||
/* export to desired destination */
|
||||
data _null_;
|
||||
%if &outref=0 %then %do;
|
||||
file "&outfile" lrecl=32767;
|
||||
%end;
|
||||
%else %do;
|
||||
file &outref;
|
||||
%end;
|
||||
infile &fname2;
|
||||
input;
|
||||
put _infile_;
|
||||
run;
|
||||
filename &fname1 clear;
|
||||
filename &fname2 clear;
|
||||
%mend;
|
||||
/**
|
||||
@file mv_getrefreshtoken.sas
|
||||
@brief deprecated - replaced by mv_tokenauth.sas
|
||||
|
||||
@@ -12511,23 +12660,24 @@ libname &libref1 clear;
|
||||
,paramstring=%str("macvarname":"macvarvalue","answer":42)
|
||||
)
|
||||
|
||||
@param access_token_var= The global macro variable to contain the access token
|
||||
@param grant_type= valid values:
|
||||
* password
|
||||
* authorization_code
|
||||
* detect - will check if access_token exists, if not will use sas_services if
|
||||
a SASStudioV session else authorization_code. Default option.
|
||||
* sas_services - will use oauth_bearer=sas_services
|
||||
@param [in] access_token_var= The global macro variable to contain the access token
|
||||
@param [in] grant_type= valid values:
|
||||
|
||||
@param path= The SAS Drive path to the job being executed
|
||||
@param name= The name of the job to execute
|
||||
@param paramstring= A JSON fragment with name:value pairs, eg: `"name":"value"`
|
||||
* password
|
||||
* authorization_code
|
||||
* detect - will check if access_token exists, if not will use sas_services if
|
||||
a SASStudioV session else authorization_code. Default option.
|
||||
* sas_services - will use oauth_bearer=sas_services
|
||||
|
||||
@param [in] path= The SAS Drive path to the job being executed
|
||||
@param [in] name= The name of the job to execute
|
||||
@param [in] paramstring= A JSON fragment with name:value pairs, eg: `"name":"value"`
|
||||
or "name":"value","name2":42`. This will need to be wrapped in `%str()`.
|
||||
|
||||
@param contextName= Context name with which to run the job.
|
||||
@param [in] contextName= Context name with which to run the job.
|
||||
Default = `SAS Job Execution compute context`
|
||||
|
||||
@param outds= The output dataset containing links (Default=work.mv_jobexecute)
|
||||
@param [out] outds= The output dataset containing links (Default=work.mv_jobexecute)
|
||||
|
||||
|
||||
@version VIYA V.03.04
|
||||
@@ -12613,9 +12763,11 @@ data _null_;
|
||||
length joburi contextname $128 paramstring $32765;
|
||||
joburi=quote(trim(symget('joburi')));
|
||||
contextname=quote(trim(symget('contextname')));
|
||||
_program=quote("&path/&name");
|
||||
paramstring=symget('paramstring');
|
||||
put '{"jobDefinitionUri":' joburi ;
|
||||
put ' ,"arguments":{"_contextName":' contextname;
|
||||
put ' ,"_program":' _program;
|
||||
if paramstring ne "0" then do;
|
||||
put ' ,' paramstring;
|
||||
end;
|
||||
@@ -12646,6 +12798,7 @@ libname &libref JSON fileref=&fname1;
|
||||
|
||||
data &outds;
|
||||
set &libref..links;
|
||||
_program="&path/&name";
|
||||
run;
|
||||
|
||||
/* clear refs */
|
||||
@@ -12653,6 +12806,209 @@ filename &fname0 clear;
|
||||
filename &fname1 clear;
|
||||
libname &libref;
|
||||
|
||||
%mend;/**
|
||||
@file
|
||||
@brief Takes a dataset of running jobs and waits for them to complete
|
||||
@details Will poll `/jobs/{jobId}/state` at set intervals until they are all
|
||||
completed. Completion is determined by reference to the returned _state_, as
|
||||
per the following table:
|
||||
|
||||
| state | Wait? | Notes|
|
||||
|-----------|-------|------|
|
||||
| idle | yes | We assume processing will continue. Beware of idle sessions with no code submitted! |
|
||||
| pending | yes | Job is preparing to run |
|
||||
| running | yes | Job is running|
|
||||
| canceled | no | Job was cancelled|
|
||||
| completed | no | Job finished - does not mean it was successful. Check stateDetails|
|
||||
| failed | no | Job failed to execute, could be a problem when calling the apis|
|
||||
|
||||
|
||||
## Example
|
||||
|
||||
First, compile the macros:
|
||||
|
||||
filename mc url
|
||||
"https://raw.githubusercontent.com/sasjs/core/main/all.sas";
|
||||
%inc mc;
|
||||
|
||||
Next, create a job (in this case, as a web service):
|
||||
|
||||
filename ft15f001 temp;
|
||||
parmcards4;
|
||||
data ;
|
||||
rand=ranuni(0)*1000000;
|
||||
do x=1 to rand;
|
||||
y=rand*x;
|
||||
output;
|
||||
end;
|
||||
run;
|
||||
;;;;
|
||||
%mv_createwebservice(path=/Public/temp,name=demo)
|
||||
|
||||
Then, execute the job,multiple times, and wait for them all to finish:
|
||||
|
||||
%mv_jobexecute(path=/Public/temp,name=demo,outds=work.ds1)
|
||||
%mv_jobexecute(path=/Public/temp,name=demo,outds=work.ds2)
|
||||
%mv_jobexecute(path=/Public/temp,name=demo,outds=work.ds3)
|
||||
%mv_jobexecute(path=/Public/temp,name=demo,outds=work.ds4)
|
||||
|
||||
data work.jobs;
|
||||
set work.ds1 work.ds2 work.ds3 work.ds4;
|
||||
where method='GET' and rel='state';
|
||||
run;
|
||||
|
||||
%mv_jobwaitfor(inds=work.jobs,outds=work.jobstates)
|
||||
|
||||
|
||||
Delete the job:
|
||||
|
||||
%mv_deletejes(path=/Public/temp,name=demo)
|
||||
|
||||
@param [in] access_token_var= The global macro variable to contain the access token
|
||||
@param [in] grant_type= valid values:
|
||||
|
||||
- password
|
||||
- authorization_code
|
||||
- detect - will check if access_token exists, if not will use sas_services if
|
||||
a SASStudioV session else authorization_code. Default option.
|
||||
- sas_services - will use oauth_bearer=sas_services
|
||||
|
||||
@param [in] inds= The input dataset containing the list of job uris, in the
|
||||
following format: `/jobExecution/jobs/&JOBID./state` and the corresponding
|
||||
job name. The uri should be in a `uri` variable, and the job path/name
|
||||
should be in a `_program` variable.
|
||||
@param [out] outds= The output dataset containing the list of states by job
|
||||
(default=work.mv_jobexecute)
|
||||
|
||||
|
||||
@version VIYA V.03.04
|
||||
@author Allan Bowe, source: https://github.com/sasjs/core
|
||||
|
||||
<h4> Dependencies </h4>
|
||||
@li mp_abort.sas
|
||||
@li mf_getplatform.sas
|
||||
@li mf_getuniquefileref.sas
|
||||
@li mf_existvar.sas
|
||||
@li mf_nobs.sas
|
||||
|
||||
**/
|
||||
|
||||
%macro mv_jobwaitfor(
|
||||
access_token_var=ACCESS_TOKEN
|
||||
,grant_type=sas_services
|
||||
,inds=0
|
||||
,outds=work.mv_jobwaitfor
|
||||
);
|
||||
%local oauth_bearer;
|
||||
%if &grant_type=detect %then %do;
|
||||
%if %symexist(&access_token_var) %then %let grant_type=authorization_code;
|
||||
%else %let grant_type=sas_services;
|
||||
%end;
|
||||
%if &grant_type=sas_services %then %do;
|
||||
%let oauth_bearer=oauth_bearer=sas_services;
|
||||
%let &access_token_var=;
|
||||
%end;
|
||||
%put &sysmacroname: grant_type=&grant_type;
|
||||
%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
|
||||
and &grant_type ne sas_services
|
||||
)
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(Invalid value for grant_type: &grant_type)
|
||||
)
|
||||
|
||||
%mp_abort(iftrue=("&inds"="0")
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(input dataset not provided)
|
||||
)
|
||||
%mp_abort(iftrue=(%mf_existvar(&inds,uri)=0)
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(The URI variable was not found in the input dataset(&inds))
|
||||
)
|
||||
%mp_abort(iftrue=(%mf_existvar(&inds,_program)=0)
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(The _PROGRAM variable was not found in the input dataset(&inds))
|
||||
)
|
||||
|
||||
%if %mf_nobs(&inds)=0 %then %do;
|
||||
%put NOTE: Zero observations in &inds, &sysmacroname will now exit;
|
||||
%return;
|
||||
%end;
|
||||
|
||||
options noquotelenmax;
|
||||
%local base_uri; /* location of rest apis */
|
||||
%let base_uri=%mf_getplatform(VIYARESTAPI);
|
||||
|
||||
data _null_;
|
||||
set &inds end=last;
|
||||
call symputx(cats('joburi',_n_),uri,'l');
|
||||
call symputx(cats('jobname',_n_),_program,'l');
|
||||
if last then call symputx('uricnt',_n_,'l');
|
||||
run;
|
||||
|
||||
%local fname0 ;
|
||||
%let fname0=%mf_getuniquefileref();
|
||||
|
||||
data &outds;
|
||||
format _program uri $128. state $32. timestamp datetime19.;
|
||||
stop;
|
||||
run;
|
||||
|
||||
%local i;
|
||||
%do i=1 %to &uricnt;
|
||||
%if "&&joburi&i" ne "0" %then %do;
|
||||
proc http method='GET' out=&fname0 &oauth_bearer url="&base_uri/&&joburi&i";
|
||||
headers "Accept"="text/plain"
|
||||
%if &grant_type=authorization_code %then %do;
|
||||
"Authorization"="Bearer &&&access_token_var"
|
||||
%end; ;
|
||||
run;
|
||||
%if &SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 201 %then
|
||||
%do;
|
||||
data _null_;infile &fname0;input;putlog _infile_;run;
|
||||
%mp_abort(mac=&sysmacroname
|
||||
,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
|
||||
)
|
||||
%end;
|
||||
|
||||
%let status=notset;
|
||||
data _null_;
|
||||
infile &fname0;
|
||||
input;
|
||||
call symputx('status',_infile_,'l');
|
||||
run;
|
||||
|
||||
%if &status=completed or &status=failed or &status=canceled %then %do;
|
||||
proc sql;
|
||||
insert into &outds set
|
||||
_program="&&jobname&i",
|
||||
uri="&&joburi&i",
|
||||
state="&status",
|
||||
timestamp=datetime();
|
||||
%let joburi&i=0;
|
||||
%end;
|
||||
%else %if &status=idle or &status=pending or &status=running %then %do;
|
||||
data _null_;
|
||||
call sleep(1,1);
|
||||
run;
|
||||
%end;
|
||||
%else %do;
|
||||
%mp_abort(mac=&sysmacroname
|
||||
,msg=%str(status &status not expected!!)
|
||||
)
|
||||
%end;
|
||||
%end;
|
||||
%if &i=&uricnt %then %do;
|
||||
%local goback;
|
||||
%let goback=0;
|
||||
proc sql noprint;
|
||||
select count(*) into:goback from &outds;
|
||||
%if &goback ne &uricnt %then %let i=0;
|
||||
%end;
|
||||
%end;
|
||||
|
||||
/* clear refs */
|
||||
filename &fname0 clear;
|
||||
|
||||
%mend;/**
|
||||
@file mv_registerclient.sas
|
||||
@brief Register Client and Secret (admin task)
|
||||
@@ -13386,20 +13742,21 @@ filename &fref1 clear;
|
||||
|
||||
%mend;
|
||||
/**
|
||||
@file ml_json2sas.sas
|
||||
@brief Creates the json2sas.lua file
|
||||
@details Writes json2sas.lua to the work directory
|
||||
@file ml_json.sas
|
||||
@brief Compiles the json.lua lua file
|
||||
@details Writes json.lua to the work directory
|
||||
and then includes it.
|
||||
Usage:
|
||||
|
||||
%ml_json2sas()
|
||||
%ml_json()
|
||||
|
||||
**/
|
||||
|
||||
%macro ml_json2sas();
|
||||
%macro ml_json();
|
||||
data _null_;
|
||||
file "%sysfunc(pathname(work))/json2sas.lua";
|
||||
file "%sysfunc(pathname(work))/ml_json.lua";
|
||||
put '-- ';
|
||||
put '-- json2sas.lua (modified from json.lua) ';
|
||||
put '-- json.lua ';
|
||||
put '-- ';
|
||||
put '-- Copyright (c) 2019 rxi ';
|
||||
put '-- ';
|
||||
@@ -13422,7 +13779,7 @@ data _null_;
|
||||
put '-- SOFTWARE. ';
|
||||
put '-- ';
|
||||
put ' ';
|
||||
put 'local json2sas = { _version = "0.1.2" } ';
|
||||
put 'json = { _version = "0.1.2" } ';
|
||||
put ' ';
|
||||
put '------------------------------------------------------------------------------- ';
|
||||
put '-- Encode ';
|
||||
@@ -13522,7 +13879,7 @@ data _null_;
|
||||
put ' error("unexpected type ''" .. t .. "''") ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'function json2sas.encode(val) ';
|
||||
put 'function json.encode(val) ';
|
||||
put ' return ( encode(val) ) ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
@@ -13756,7 +14113,7 @@ data _null_;
|
||||
put ' decode_error(str, idx, "unexpected character ''" .. chr .. "''") ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'function json2sas.decode(str) ';
|
||||
put 'function json.decode(str) ';
|
||||
put ' if type(str) ~= "string" then ';
|
||||
put ' error("expected argument of type string, got " .. type(str)) ';
|
||||
put ' end ';
|
||||
@@ -13768,90 +14125,9 @@ data _null_;
|
||||
put ' return res ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put '-- convert macro variable array into one variable and decode ';
|
||||
put 'function json2sas.go(macvar) ';
|
||||
put ' local x=1 ';
|
||||
put ' local cnt=0 ';
|
||||
put ' local mac=sas.symget(macvar..''0'') ';
|
||||
put ' local newstr='''' ';
|
||||
put ' if mac and mac ~= '''' then ';
|
||||
put ' cnt=mac ';
|
||||
put ' for x=1,cnt,1 do ';
|
||||
put ' mac=sas.symget(macvar..x) ';
|
||||
put ' if mac and mac ~= '''' then ';
|
||||
put ' newstr=newstr..mac ';
|
||||
put ' else ';
|
||||
put ' return print(macvar..x..'' NOT FOUND!!'') ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' else ';
|
||||
put ' return print(macvar..''0 NOT FOUND!!'') ';
|
||||
put ' end ';
|
||||
put ' -- print(''mac:''..mac..''cnt:''..cnt..''newstr''..newstr) ';
|
||||
put ' local oneVar=json2sas.decode(newstr) ';
|
||||
put ' local jsdata=oneVar["data"] ';
|
||||
put ' local meta={} ';
|
||||
put ' local attrs={} ';
|
||||
put ' for tablename, data in pairs(jsdata) do -- each table ';
|
||||
put ' print("Processing table: "..tablename) ';
|
||||
put ' attrs[tablename]={} ';
|
||||
put ' for k, v in ipairs(data) do -- each row ';
|
||||
put ' if(k==1) then -- column names ';
|
||||
put ' for a, b in pairs(v) do ';
|
||||
put ' attrs[tablename][a]={} ';
|
||||
put ' attrs[tablename][a]["name"]=b ';
|
||||
put ' end ';
|
||||
put ' elseif(k==2) then -- get types ';
|
||||
put ' for a, b in pairs(v) do ';
|
||||
put ' if type(b)==''number'' then ';
|
||||
put ' attrs[tablename][a]["type"]="N" ';
|
||||
put ' attrs[tablename][a]["length"]=8 ';
|
||||
put ' else ';
|
||||
put ' attrs[tablename][a]["type"]="C" ';
|
||||
put ' attrs[tablename][a]["length"]=string.len(b) ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' else --update lengths ';
|
||||
put ' for a, b in pairs(v) do ';
|
||||
put ' if (type(b)==''string'' and string.len(b)>attrs[tablename][a]["length"]) ';
|
||||
put ' then ';
|
||||
put ' attrs[tablename][a]["length"]=string.len(b) ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' print(json2sas.encode(attrs[tablename])) -- show results ';
|
||||
put ' ';
|
||||
put ' -- Now create the SAS table ';
|
||||
put ' sas.new_table("work."..tablename,attrs[tablename]) ';
|
||||
put ' local dsid=sas.open("work."..tablename, "u") ';
|
||||
put ' for k, v in ipairs(data) do ';
|
||||
put ' if k>1 then ';
|
||||
put ' sas.append(dsid) ';
|
||||
put ' for a, b in pairs(v) do ';
|
||||
put ' sas.put_value(dsid, attrs[tablename][a]["name"], b) ';
|
||||
put ' end ';
|
||||
put ' sas.update(dsid) ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' sas.close(dsid) ';
|
||||
put ' end ';
|
||||
put ' return json2sas.decode(newstr) ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put ' ';
|
||||
put 'function quote(str) ';
|
||||
put ' return sas.quote(str) ';
|
||||
put 'end ';
|
||||
put 'function sasvar(str) ';
|
||||
put ' print("processing: "..str) ';
|
||||
put ' print(sas.symexist(str)) ';
|
||||
put ' if sas.symexist(str)==1 then ';
|
||||
put ' return quote(str)..'':''..quote(sas.symget(str))..'','' ';
|
||||
put ' end ';
|
||||
put ' return '''' ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'return json2sas ';
|
||||
put 'return json ';
|
||||
run;
|
||||
|
||||
%inc "%sysfunc(pathname(work))/ml_json.lua";
|
||||
|
||||
%mend;
|
||||
|
||||
9
build.py
9
build.py
@@ -9,19 +9,22 @@ for file in files:
|
||||
ml = open('lua/' + name + '.sas', "w")
|
||||
ml.write("/**\n")
|
||||
ml.write(" @file " + name + '.sas\n')
|
||||
ml.write(" @brief Creates the " + basename + " file\n")
|
||||
ml.write(" @brief Compiles the " + basename + " lua file\n")
|
||||
ml.write(" @details Writes " + basename + " to the work directory\n")
|
||||
ml.write(" and then includes it.\n")
|
||||
ml.write(" Usage:\n\n")
|
||||
ml.write(" %" + name + "()\n\n")
|
||||
ml.write("**/\n\n")
|
||||
ml.write("%macro " + name + "();\n")
|
||||
ml.write("data _null_;\n")
|
||||
ml.write(" file \"%sysfunc(pathname(work))/" + basename + "\";\n")
|
||||
ml.write(" file \"%sysfunc(pathname(work))/" + name + ".lua\";\n")
|
||||
with open(file) as infile:
|
||||
for line in infile:
|
||||
ml.write(" put '" + line.rstrip().replace("'","''") + " ';\n")
|
||||
ml.write("run;\n")
|
||||
ml.write("run;\n\n")
|
||||
ml.write("%inc \"%sysfunc(pathname(work))/" + name + ".lua\";\n\n")
|
||||
ml.write("%mend;\n")
|
||||
|
||||
ml.close()
|
||||
|
||||
# prepare web files
|
||||
|
||||
@@ -19,7 +19,7 @@ HTML_FOOTER = ./doxy/new_footer.html
|
||||
HTML_EXTRA_STYLESHEET = ./doxy/new_stylesheet.css
|
||||
INHERIT_DOCS = NO
|
||||
INLINE_INFO = NO
|
||||
INPUT = base meta metax viya
|
||||
INPUT = base meta metax viya lua
|
||||
LAYOUT_FILE = ./doxy/DoxygenLayout.xml
|
||||
MAX_INITIALIZER_LINES = 0
|
||||
PROJECT_NAME = Macro Core
|
||||
|
||||
@@ -16,6 +16,7 @@ mkdir $BUILD_FOLDER
|
||||
cp -r base $BUILD_FOLDER
|
||||
cp -r meta $BUILD_FOLDER
|
||||
cp -r metax $BUILD_FOLDER
|
||||
cp -r lua $BUILD_FOLDER
|
||||
cp -r viya $BUILD_FOLDER
|
||||
cp -r doxy $BUILD_FOLDER
|
||||
cp main.dox $BUILD_FOLDER
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
--
|
||||
-- json2sas.lua (modified from json.lua)
|
||||
-- json.lua
|
||||
--
|
||||
-- Copyright (c) 2019 rxi
|
||||
--
|
||||
@@ -22,7 +22,7 @@
|
||||
-- SOFTWARE.
|
||||
--
|
||||
|
||||
local json2sas = { _version = "0.1.2" }
|
||||
json = { _version = "0.1.2" }
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Encode
|
||||
@@ -122,7 +122,7 @@ encode = function(val, stack)
|
||||
error("unexpected type '" .. t .. "'")
|
||||
end
|
||||
|
||||
function json2sas.encode(val)
|
||||
function json.encode(val)
|
||||
return ( encode(val) )
|
||||
end
|
||||
|
||||
@@ -356,7 +356,7 @@ parse = function(str, idx)
|
||||
decode_error(str, idx, "unexpected character '" .. chr .. "'")
|
||||
end
|
||||
|
||||
function json2sas.decode(str)
|
||||
function json.decode(str)
|
||||
if type(str) ~= "string" then
|
||||
error("expected argument of type string, got " .. type(str))
|
||||
end
|
||||
@@ -368,88 +368,4 @@ function json2sas.decode(str)
|
||||
return res
|
||||
end
|
||||
|
||||
-- convert macro variable array into one variable and decode
|
||||
function json2sas.go(macvar)
|
||||
local x=1
|
||||
local cnt=0
|
||||
local mac=sas.symget(macvar..'0')
|
||||
local newstr=''
|
||||
if mac and mac ~= '' then
|
||||
cnt=mac
|
||||
for x=1,cnt,1 do
|
||||
mac=sas.symget(macvar..x)
|
||||
if mac and mac ~= '' then
|
||||
newstr=newstr..mac
|
||||
else
|
||||
return print(macvar..x..' NOT FOUND!!')
|
||||
end
|
||||
end
|
||||
else
|
||||
return print(macvar..'0 NOT FOUND!!')
|
||||
end
|
||||
-- print('mac:'..mac..'cnt:'..cnt..'newstr'..newstr)
|
||||
local oneVar=json2sas.decode(newstr)
|
||||
local jsdata=oneVar["data"]
|
||||
local meta={}
|
||||
local attrs={}
|
||||
for tablename, data in pairs(jsdata) do -- each table
|
||||
print("Processing table: "..tablename)
|
||||
attrs[tablename]={}
|
||||
for k, v in ipairs(data) do -- each row
|
||||
if(k==1) then -- column names
|
||||
for a, b in pairs(v) do
|
||||
attrs[tablename][a]={}
|
||||
attrs[tablename][a]["name"]=b
|
||||
end
|
||||
elseif(k==2) then -- get types
|
||||
for a, b in pairs(v) do
|
||||
if type(b)=='number' then
|
||||
attrs[tablename][a]["type"]="N"
|
||||
attrs[tablename][a]["length"]=8
|
||||
else
|
||||
attrs[tablename][a]["type"]="C"
|
||||
attrs[tablename][a]["length"]=string.len(b)
|
||||
end
|
||||
end
|
||||
else --update lengths
|
||||
for a, b in pairs(v) do
|
||||
if (type(b)=='string' and string.len(b)>attrs[tablename][a]["length"])
|
||||
then
|
||||
attrs[tablename][a]["length"]=string.len(b)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
print(json2sas.encode(attrs[tablename])) -- show results
|
||||
|
||||
-- Now create the SAS table
|
||||
sas.new_table("work."..tablename,attrs[tablename])
|
||||
local dsid=sas.open("work."..tablename, "u")
|
||||
for k, v in ipairs(data) do
|
||||
if k>1 then
|
||||
sas.append(dsid)
|
||||
for a, b in pairs(v) do
|
||||
sas.put_value(dsid, attrs[tablename][a]["name"], b)
|
||||
end
|
||||
sas.update(dsid)
|
||||
end
|
||||
end
|
||||
sas.close(dsid)
|
||||
end
|
||||
return json2sas.decode(newstr)
|
||||
end
|
||||
|
||||
|
||||
function quote(str)
|
||||
return sas.quote(str)
|
||||
end
|
||||
function sasvar(str)
|
||||
print("processing: "..str)
|
||||
print(sas.symexist(str))
|
||||
if sas.symexist(str)==1 then
|
||||
return quote(str)..':'..quote(sas.symget(str))..','
|
||||
end
|
||||
return ''
|
||||
end
|
||||
|
||||
return json2sas
|
||||
return json
|
||||
@@ -1,18 +1,19 @@
|
||||
/**
|
||||
@file ml_json2sas.sas
|
||||
@brief Creates the json2sas.lua file
|
||||
@details Writes json2sas.lua to the work directory
|
||||
@file ml_json.sas
|
||||
@brief Compiles the json.lua lua file
|
||||
@details Writes json.lua to the work directory
|
||||
and then includes it.
|
||||
Usage:
|
||||
|
||||
%ml_json2sas()
|
||||
%ml_json()
|
||||
|
||||
**/
|
||||
|
||||
%macro ml_json2sas();
|
||||
%macro ml_json();
|
||||
data _null_;
|
||||
file "%sysfunc(pathname(work))/json2sas.lua";
|
||||
file "%sysfunc(pathname(work))/ml_json.lua";
|
||||
put '-- ';
|
||||
put '-- json2sas.lua (modified from json.lua) ';
|
||||
put '-- json.lua ';
|
||||
put '-- ';
|
||||
put '-- Copyright (c) 2019 rxi ';
|
||||
put '-- ';
|
||||
@@ -35,7 +36,7 @@ data _null_;
|
||||
put '-- SOFTWARE. ';
|
||||
put '-- ';
|
||||
put ' ';
|
||||
put 'local json2sas = { _version = "0.1.2" } ';
|
||||
put 'json = { _version = "0.1.2" } ';
|
||||
put ' ';
|
||||
put '------------------------------------------------------------------------------- ';
|
||||
put '-- Encode ';
|
||||
@@ -135,7 +136,7 @@ data _null_;
|
||||
put ' error("unexpected type ''" .. t .. "''") ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'function json2sas.encode(val) ';
|
||||
put 'function json.encode(val) ';
|
||||
put ' return ( encode(val) ) ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
@@ -369,7 +370,7 @@ data _null_;
|
||||
put ' decode_error(str, idx, "unexpected character ''" .. chr .. "''") ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'function json2sas.decode(str) ';
|
||||
put 'function json.decode(str) ';
|
||||
put ' if type(str) ~= "string" then ';
|
||||
put ' error("expected argument of type string, got " .. type(str)) ';
|
||||
put ' end ';
|
||||
@@ -381,90 +382,9 @@ data _null_;
|
||||
put ' return res ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put '-- convert macro variable array into one variable and decode ';
|
||||
put 'function json2sas.go(macvar) ';
|
||||
put ' local x=1 ';
|
||||
put ' local cnt=0 ';
|
||||
put ' local mac=sas.symget(macvar..''0'') ';
|
||||
put ' local newstr='''' ';
|
||||
put ' if mac and mac ~= '''' then ';
|
||||
put ' cnt=mac ';
|
||||
put ' for x=1,cnt,1 do ';
|
||||
put ' mac=sas.symget(macvar..x) ';
|
||||
put ' if mac and mac ~= '''' then ';
|
||||
put ' newstr=newstr..mac ';
|
||||
put ' else ';
|
||||
put ' return print(macvar..x..'' NOT FOUND!!'') ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' else ';
|
||||
put ' return print(macvar..''0 NOT FOUND!!'') ';
|
||||
put ' end ';
|
||||
put ' -- print(''mac:''..mac..''cnt:''..cnt..''newstr''..newstr) ';
|
||||
put ' local oneVar=json2sas.decode(newstr) ';
|
||||
put ' local jsdata=oneVar["data"] ';
|
||||
put ' local meta={} ';
|
||||
put ' local attrs={} ';
|
||||
put ' for tablename, data in pairs(jsdata) do -- each table ';
|
||||
put ' print("Processing table: "..tablename) ';
|
||||
put ' attrs[tablename]={} ';
|
||||
put ' for k, v in ipairs(data) do -- each row ';
|
||||
put ' if(k==1) then -- column names ';
|
||||
put ' for a, b in pairs(v) do ';
|
||||
put ' attrs[tablename][a]={} ';
|
||||
put ' attrs[tablename][a]["name"]=b ';
|
||||
put ' end ';
|
||||
put ' elseif(k==2) then -- get types ';
|
||||
put ' for a, b in pairs(v) do ';
|
||||
put ' if type(b)==''number'' then ';
|
||||
put ' attrs[tablename][a]["type"]="N" ';
|
||||
put ' attrs[tablename][a]["length"]=8 ';
|
||||
put ' else ';
|
||||
put ' attrs[tablename][a]["type"]="C" ';
|
||||
put ' attrs[tablename][a]["length"]=string.len(b) ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' else --update lengths ';
|
||||
put ' for a, b in pairs(v) do ';
|
||||
put ' if (type(b)==''string'' and string.len(b)>attrs[tablename][a]["length"]) ';
|
||||
put ' then ';
|
||||
put ' attrs[tablename][a]["length"]=string.len(b) ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' print(json2sas.encode(attrs[tablename])) -- show results ';
|
||||
put ' ';
|
||||
put ' -- Now create the SAS table ';
|
||||
put ' sas.new_table("work."..tablename,attrs[tablename]) ';
|
||||
put ' local dsid=sas.open("work."..tablename, "u") ';
|
||||
put ' for k, v in ipairs(data) do ';
|
||||
put ' if k>1 then ';
|
||||
put ' sas.append(dsid) ';
|
||||
put ' for a, b in pairs(v) do ';
|
||||
put ' sas.put_value(dsid, attrs[tablename][a]["name"], b) ';
|
||||
put ' end ';
|
||||
put ' sas.update(dsid) ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' sas.close(dsid) ';
|
||||
put ' end ';
|
||||
put ' return json2sas.decode(newstr) ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put ' ';
|
||||
put 'function quote(str) ';
|
||||
put ' return sas.quote(str) ';
|
||||
put 'end ';
|
||||
put 'function sasvar(str) ';
|
||||
put ' print("processing: "..str) ';
|
||||
put ' print(sas.symexist(str)) ';
|
||||
put ' if sas.symexist(str)==1 then ';
|
||||
put ' return quote(str)..'':''..quote(sas.symget(str))..'','' ';
|
||||
put ' end ';
|
||||
put ' return '''' ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'return json2sas ';
|
||||
put 'return json ';
|
||||
run;
|
||||
|
||||
%inc "%sysfunc(pathname(work))/ml_json.lua";
|
||||
|
||||
%mend;
|
||||
11
main.dox
11
main.dox
@@ -50,4 +50,15 @@
|
||||
* No X command
|
||||
* Prefixes: _mv_
|
||||
|
||||
*/
|
||||
|
||||
/*! \dir lua
|
||||
* \brief Lua macros
|
||||
* \details These macros have the following attributes:
|
||||
|
||||
* OS independent
|
||||
* Work as LUA functions (they are immediately executed/compiled)
|
||||
* Auto-generated from the plain source `.lua` files in the same directory
|
||||
* Prefixes: _ml_
|
||||
|
||||
*/
|
||||
@@ -38,6 +38,7 @@
|
||||
rc=metadata_getattr(liburi,"Name",LibName);
|
||||
/* now try and assign it */
|
||||
if libname("&libref",,'meta',cats('liburi="',liburi,'";')) ne 0 then do;
|
||||
putlog "&libref could not be assigned";
|
||||
call symputx('msg',sysmsg(),'l');
|
||||
if "&mabort"='HARD' then call symputx('mp_abort',1,'l');
|
||||
end;
|
||||
|
||||
@@ -5,17 +5,15 @@
|
||||
blank to return all groups.
|
||||
Usage:
|
||||
|
||||
- all groups
|
||||
%mm_getGroups()
|
||||
- all groups: `%mm_getGroups()`
|
||||
|
||||
- all groups for a particular user
|
||||
%mm_getgroups(user=&sysuserid)
|
||||
- all groups for a particular user: `%mm_getgroups(user=&sysuserid)`
|
||||
|
||||
@param user= the metadata user to return groups for. Leave blank for all
|
||||
@param [in] user= the metadata user to return groups for. Leave blank for all
|
||||
groups.
|
||||
@param outds= the dataset to create that contains the list of groups
|
||||
@param repo= the metadata repository that contains the user/group information
|
||||
@param mDebug= set to 1 to show debug messages in the log
|
||||
@param [in] repo= the metadata repository that contains the user/group information
|
||||
@param [in] mDebug= set to 1 to show debug messages in the log
|
||||
@param [out] outds= the dataset to create that contains the list of groups
|
||||
|
||||
@returns outds dataset containing all groups in a column named "metagroup"
|
||||
- groupuri
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
|
||||
Usage:
|
||||
|
||||
%mm_getroles()
|
||||
%mm_getroles()
|
||||
|
||||
@param outds the dataset to create that contains the list of roles
|
||||
@param [out] outds the dataset to create that contains the list of roles
|
||||
|
||||
@returns outds dataset containing all roles, with the following columns:
|
||||
- uri
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
@file mv_deletejes.sas
|
||||
@brief Creates a job execution service if it does not already exist
|
||||
@file
|
||||
@brief Deletes a Viya Job, if it exists
|
||||
@details If not executed in Studio 5+ will expect oauth token in a global
|
||||
macro variable (default ACCESS_TOKEN).
|
||||
|
||||
|
||||
150
viya/mv_getjobcode.sas
Normal file
150
viya/mv_getjobcode.sas
Normal file
@@ -0,0 +1,150 @@
|
||||
/**
|
||||
@file
|
||||
@brief Extract the source code from a SAS Viya Job
|
||||
@details Extracts the SAS code from a Job into a fileref or physical file.
|
||||
Example:
|
||||
|
||||
%mv_getjobcode(
|
||||
path=/Public/jobs
|
||||
,name=some_job
|
||||
,outfile=/tmp/some_job.sas
|
||||
)
|
||||
|
||||
@param [in] access_token_var= The global macro variable to contain the access token
|
||||
@param [in] grant_type= valid values:
|
||||
* password
|
||||
* authorization_code
|
||||
* detect - will check if access_token exists, if not will use sas_services if
|
||||
a SASStudioV session else authorization_code. Default option.
|
||||
* sas_services - will use oauth_bearer=sas_services
|
||||
@param [in] path= The SAS Drive path of the job
|
||||
@param [in] name= The name of the job
|
||||
@param [out] outref= A fileref to which to write the source code
|
||||
@param [out] outfile= A file to which to write the source code
|
||||
|
||||
@version VIYA V.03.04
|
||||
@author Allan Bowe, source: https://github.com/sasjs/core
|
||||
|
||||
<h4> SAS Macros </h4>
|
||||
@li mp_abort.sas
|
||||
@li mf_getplatform.sas
|
||||
@li mf_getuniquefileref.sas
|
||||
@li mv_getfoldermembers.sas
|
||||
@li ml_json.sas
|
||||
|
||||
**/
|
||||
|
||||
%macro mv_getjobcode(outref=0,outfile=0
|
||||
,name=0,path=0
|
||||
,contextName=SAS Job Execution compute context
|
||||
,access_token_var=ACCESS_TOKEN
|
||||
,grant_type=sas_services
|
||||
);
|
||||
%local oauth_bearer;
|
||||
%if &grant_type=detect %then %do;
|
||||
%if %symexist(&access_token_var) %then %let grant_type=authorization_code;
|
||||
%else %let grant_type=sas_services;
|
||||
%end;
|
||||
%if &grant_type=sas_services %then %do;
|
||||
%let oauth_bearer=oauth_bearer=sas_services;
|
||||
%let &access_token_var=;
|
||||
%end;
|
||||
%put &sysmacroname: grant_type=&grant_type;
|
||||
%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
|
||||
and &grant_type ne sas_services
|
||||
)
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(Invalid value for grant_type: &grant_type)
|
||||
)
|
||||
%mp_abort(iftrue=("&path"="0")
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(Job Path not provided)
|
||||
)
|
||||
%mp_abort(iftrue=("&name"="0")
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(Job Name not provided)
|
||||
)
|
||||
%mp_abort(iftrue=("&outfile"="0" and "&outref"="0")
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(Output destination (file or fileref) must be provided)
|
||||
)
|
||||
options noquotelenmax;
|
||||
%local base_uri; /* location of rest apis */
|
||||
%let base_uri=%mf_getplatform(VIYARESTAPI);
|
||||
data;run;
|
||||
%local foldermembers;
|
||||
%let foldermembers=&syslast;
|
||||
%mv_getfoldermembers(root=&path
|
||||
,access_token_var=&access_token_var
|
||||
,grant_type=&grant_type
|
||||
,outds=&foldermembers
|
||||
)
|
||||
%local joburi;
|
||||
%let joburi=0;
|
||||
data _null_;
|
||||
set &foldermembers;
|
||||
if name="&name" and uri=:'/jobDefinitions/definitions'
|
||||
then call symputx('joburi',uri);
|
||||
run;
|
||||
%mp_abort(iftrue=("&joburi"="0")
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(Job &path/&name not found)
|
||||
)
|
||||
|
||||
/* prepare request*/
|
||||
%local fname1;
|
||||
%let fname1=%mf_getuniquefileref();
|
||||
proc http method='GET' out=&fname1 &oauth_bearer
|
||||
url="&base_uri&joburi";
|
||||
headers "Accept"="application/vnd.sas.job.definition+json"
|
||||
%if &grant_type=authorization_code %then %do;
|
||||
"Authorization"="Bearer &&&access_token_var"
|
||||
%end;
|
||||
;
|
||||
run;
|
||||
%if &SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 201 %then
|
||||
%do;
|
||||
data _null_;infile &fname1;input;putlog _infile_;run;
|
||||
%mp_abort(mac=&sysmacroname
|
||||
,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
|
||||
)
|
||||
%end;
|
||||
%local fname2 fname3 fpath1 fpath2 fpath3;
|
||||
%let fname2=%mf_getuniquefileref();
|
||||
%let fname3=%mf_getuniquefileref();
|
||||
%let fpath1=%sysfunc(pathname(&fname1));
|
||||
%let fpath2=%sysfunc(pathname(&fname2));
|
||||
%let fpath3=%sysfunc(pathname(&fname2));
|
||||
|
||||
/* compile the lua JSON module */
|
||||
%ml_json()
|
||||
/* read using LUA - this allows the code to be of any length */
|
||||
data _null_;
|
||||
file "&fpath3..lua";
|
||||
put '
|
||||
infile = io.open (sas.symget("fpath1"), "r")
|
||||
outfile = io.open (sas.symget("fpath2"), "w")
|
||||
io.input(infile)
|
||||
local resp=json.decode(io.read())
|
||||
local job=resp["code"]
|
||||
outfile:write(job)
|
||||
io.close(infile)
|
||||
io.close(outfile)
|
||||
';
|
||||
run;
|
||||
%inc "&fpath3..lua";
|
||||
/* export to desired destination */
|
||||
data _null_;
|
||||
%if &outref=0 %then %do;
|
||||
file "&outfile" lrecl=32767;
|
||||
%end;
|
||||
%else %do;
|
||||
file &outref;
|
||||
%end;
|
||||
infile &fname2;
|
||||
input;
|
||||
put _infile_;
|
||||
run;
|
||||
filename &fname1 clear;
|
||||
filename &fname2 clear;
|
||||
%mend;
|
||||
@@ -23,23 +23,24 @@
|
||||
,paramstring=%str("macvarname":"macvarvalue","answer":42)
|
||||
)
|
||||
|
||||
@param access_token_var= The global macro variable to contain the access token
|
||||
@param grant_type= valid values:
|
||||
* password
|
||||
* authorization_code
|
||||
* detect - will check if access_token exists, if not will use sas_services if
|
||||
a SASStudioV session else authorization_code. Default option.
|
||||
* sas_services - will use oauth_bearer=sas_services
|
||||
@param [in] access_token_var= The global macro variable to contain the access token
|
||||
@param [in] grant_type= valid values:
|
||||
|
||||
@param path= The SAS Drive path to the job being executed
|
||||
@param name= The name of the job to execute
|
||||
@param paramstring= A JSON fragment with name:value pairs, eg: `"name":"value"`
|
||||
* password
|
||||
* authorization_code
|
||||
* detect - will check if access_token exists, if not will use sas_services if
|
||||
a SASStudioV session else authorization_code. Default option.
|
||||
* sas_services - will use oauth_bearer=sas_services
|
||||
|
||||
@param [in] path= The SAS Drive path to the job being executed
|
||||
@param [in] name= The name of the job to execute
|
||||
@param [in] paramstring= A JSON fragment with name:value pairs, eg: `"name":"value"`
|
||||
or "name":"value","name2":42`. This will need to be wrapped in `%str()`.
|
||||
|
||||
@param contextName= Context name with which to run the job.
|
||||
@param [in] contextName= Context name with which to run the job.
|
||||
Default = `SAS Job Execution compute context`
|
||||
|
||||
@param outds= The output dataset containing links (Default=work.mv_jobexecute)
|
||||
@param [out] outds= The output dataset containing links (Default=work.mv_jobexecute)
|
||||
|
||||
|
||||
@version VIYA V.03.04
|
||||
@@ -125,9 +126,11 @@ data _null_;
|
||||
length joburi contextname $128 paramstring $32765;
|
||||
joburi=quote(trim(symget('joburi')));
|
||||
contextname=quote(trim(symget('contextname')));
|
||||
_program=quote("&path/&name");
|
||||
paramstring=symget('paramstring');
|
||||
put '{"jobDefinitionUri":' joburi ;
|
||||
put ' ,"arguments":{"_contextName":' contextname;
|
||||
put ' ,"_program":' _program;
|
||||
if paramstring ne "0" then do;
|
||||
put ' ,' paramstring;
|
||||
end;
|
||||
@@ -158,6 +161,7 @@ libname &libref JSON fileref=&fname1;
|
||||
|
||||
data &outds;
|
||||
set &libref..links;
|
||||
_program="&path/&name";
|
||||
run;
|
||||
|
||||
/* clear refs */
|
||||
|
||||
204
viya/mv_jobwaitfor.sas
Normal file
204
viya/mv_jobwaitfor.sas
Normal file
@@ -0,0 +1,204 @@
|
||||
/**
|
||||
@file
|
||||
@brief Takes a dataset of running jobs and waits for them to complete
|
||||
@details Will poll `/jobs/{jobId}/state` at set intervals until they are all
|
||||
completed. Completion is determined by reference to the returned _state_, as
|
||||
per the following table:
|
||||
|
||||
| state | Wait? | Notes|
|
||||
|-----------|-------|------|
|
||||
| idle | yes | We assume processing will continue. Beware of idle sessions with no code submitted! |
|
||||
| pending | yes | Job is preparing to run |
|
||||
| running | yes | Job is running|
|
||||
| canceled | no | Job was cancelled|
|
||||
| completed | no | Job finished - does not mean it was successful. Check stateDetails|
|
||||
| failed | no | Job failed to execute, could be a problem when calling the apis|
|
||||
|
||||
|
||||
## Example
|
||||
|
||||
First, compile the macros:
|
||||
|
||||
filename mc url
|
||||
"https://raw.githubusercontent.com/sasjs/core/main/all.sas";
|
||||
%inc mc;
|
||||
|
||||
Next, create a job (in this case, as a web service):
|
||||
|
||||
filename ft15f001 temp;
|
||||
parmcards4;
|
||||
data ;
|
||||
rand=ranuni(0)*1000000;
|
||||
do x=1 to rand;
|
||||
y=rand*x;
|
||||
output;
|
||||
end;
|
||||
run;
|
||||
;;;;
|
||||
%mv_createwebservice(path=/Public/temp,name=demo)
|
||||
|
||||
Then, execute the job,multiple times, and wait for them all to finish:
|
||||
|
||||
%mv_jobexecute(path=/Public/temp,name=demo,outds=work.ds1)
|
||||
%mv_jobexecute(path=/Public/temp,name=demo,outds=work.ds2)
|
||||
%mv_jobexecute(path=/Public/temp,name=demo,outds=work.ds3)
|
||||
%mv_jobexecute(path=/Public/temp,name=demo,outds=work.ds4)
|
||||
|
||||
data work.jobs;
|
||||
set work.ds1 work.ds2 work.ds3 work.ds4;
|
||||
where method='GET' and rel='state';
|
||||
run;
|
||||
|
||||
%mv_jobwaitfor(inds=work.jobs,outds=work.jobstates)
|
||||
|
||||
|
||||
Delete the job:
|
||||
|
||||
%mv_deletejes(path=/Public/temp,name=demo)
|
||||
|
||||
@param [in] access_token_var= The global macro variable to contain the access token
|
||||
@param [in] grant_type= valid values:
|
||||
|
||||
- password
|
||||
- authorization_code
|
||||
- detect - will check if access_token exists, if not will use sas_services if
|
||||
a SASStudioV session else authorization_code. Default option.
|
||||
- sas_services - will use oauth_bearer=sas_services
|
||||
|
||||
@param [in] inds= The input dataset containing the list of job uris, in the
|
||||
following format: `/jobExecution/jobs/&JOBID./state` and the corresponding
|
||||
job name. The uri should be in a `uri` variable, and the job path/name
|
||||
should be in a `_program` variable.
|
||||
@param [out] outds= The output dataset containing the list of states by job
|
||||
(default=work.mv_jobexecute)
|
||||
|
||||
|
||||
@version VIYA V.03.04
|
||||
@author Allan Bowe, source: https://github.com/sasjs/core
|
||||
|
||||
<h4> Dependencies </h4>
|
||||
@li mp_abort.sas
|
||||
@li mf_getplatform.sas
|
||||
@li mf_getuniquefileref.sas
|
||||
@li mf_existvar.sas
|
||||
@li mf_nobs.sas
|
||||
|
||||
**/
|
||||
|
||||
%macro mv_jobwaitfor(
|
||||
access_token_var=ACCESS_TOKEN
|
||||
,grant_type=sas_services
|
||||
,inds=0
|
||||
,outds=work.mv_jobwaitfor
|
||||
);
|
||||
%local oauth_bearer;
|
||||
%if &grant_type=detect %then %do;
|
||||
%if %symexist(&access_token_var) %then %let grant_type=authorization_code;
|
||||
%else %let grant_type=sas_services;
|
||||
%end;
|
||||
%if &grant_type=sas_services %then %do;
|
||||
%let oauth_bearer=oauth_bearer=sas_services;
|
||||
%let &access_token_var=;
|
||||
%end;
|
||||
%put &sysmacroname: grant_type=&grant_type;
|
||||
%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password
|
||||
and &grant_type ne sas_services
|
||||
)
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(Invalid value for grant_type: &grant_type)
|
||||
)
|
||||
|
||||
%mp_abort(iftrue=("&inds"="0")
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(input dataset not provided)
|
||||
)
|
||||
%mp_abort(iftrue=(%mf_existvar(&inds,uri)=0)
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(The URI variable was not found in the input dataset(&inds))
|
||||
)
|
||||
%mp_abort(iftrue=(%mf_existvar(&inds,_program)=0)
|
||||
,mac=&sysmacroname
|
||||
,msg=%str(The _PROGRAM variable was not found in the input dataset(&inds))
|
||||
)
|
||||
|
||||
%if %mf_nobs(&inds)=0 %then %do;
|
||||
%put NOTE: Zero observations in &inds, &sysmacroname will now exit;
|
||||
%return;
|
||||
%end;
|
||||
|
||||
options noquotelenmax;
|
||||
%local base_uri; /* location of rest apis */
|
||||
%let base_uri=%mf_getplatform(VIYARESTAPI);
|
||||
|
||||
data _null_;
|
||||
set &inds end=last;
|
||||
call symputx(cats('joburi',_n_),uri,'l');
|
||||
call symputx(cats('jobname',_n_),_program,'l');
|
||||
if last then call symputx('uricnt',_n_,'l');
|
||||
run;
|
||||
|
||||
%local fname0 ;
|
||||
%let fname0=%mf_getuniquefileref();
|
||||
|
||||
data &outds;
|
||||
format _program uri $128. state $32. timestamp datetime19.;
|
||||
stop;
|
||||
run;
|
||||
|
||||
%local i;
|
||||
%do i=1 %to &uricnt;
|
||||
%if "&&joburi&i" ne "0" %then %do;
|
||||
proc http method='GET' out=&fname0 &oauth_bearer url="&base_uri/&&joburi&i";
|
||||
headers "Accept"="text/plain"
|
||||
%if &grant_type=authorization_code %then %do;
|
||||
"Authorization"="Bearer &&&access_token_var"
|
||||
%end; ;
|
||||
run;
|
||||
%if &SYS_PROCHTTP_STATUS_CODE ne 200 and &SYS_PROCHTTP_STATUS_CODE ne 201 %then
|
||||
%do;
|
||||
data _null_;infile &fname0;input;putlog _infile_;run;
|
||||
%mp_abort(mac=&sysmacroname
|
||||
,msg=%str(&SYS_PROCHTTP_STATUS_CODE &SYS_PROCHTTP_STATUS_PHRASE)
|
||||
)
|
||||
%end;
|
||||
|
||||
%let status=notset;
|
||||
data _null_;
|
||||
infile &fname0;
|
||||
input;
|
||||
call symputx('status',_infile_,'l');
|
||||
run;
|
||||
|
||||
%if &status=completed or &status=failed or &status=canceled %then %do;
|
||||
proc sql;
|
||||
insert into &outds set
|
||||
_program="&&jobname&i",
|
||||
uri="&&joburi&i",
|
||||
state="&status",
|
||||
timestamp=datetime();
|
||||
%let joburi&i=0;
|
||||
%end;
|
||||
%else %if &status=idle or &status=pending or &status=running %then %do;
|
||||
data _null_;
|
||||
call sleep(1,1);
|
||||
run;
|
||||
%end;
|
||||
%else %do;
|
||||
%mp_abort(mac=&sysmacroname
|
||||
,msg=%str(status &status not expected!!)
|
||||
)
|
||||
%end;
|
||||
%end;
|
||||
%if &i=&uricnt %then %do;
|
||||
%local goback;
|
||||
%let goback=0;
|
||||
proc sql noprint;
|
||||
select count(*) into:goback from &outds;
|
||||
%if &goback ne &uricnt %then %let i=0;
|
||||
%end;
|
||||
%end;
|
||||
|
||||
/* clear refs */
|
||||
filename &fname0 clear;
|
||||
|
||||
%mend;
|
||||
Reference in New Issue
Block a user