mirror of
https://github.com/sasjs/core.git
synced 2025-12-24 03:31:19 +00:00
Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a45d280a51 | ||
|
|
2536e299ad | ||
|
|
8b5238230b | ||
|
|
0ce7efee3e | ||
|
|
357677e45c | ||
|
|
a4a332926e | ||
|
|
0a29006914 | ||
|
|
0885bad859 | ||
|
|
42bd1750bd | ||
|
|
58784b2f28 | ||
|
|
e125aace9b | ||
|
|
b02a9e3478 | ||
|
|
3d3c76c836 | ||
|
|
e039f1cd83 | ||
|
|
6c8165601d | ||
|
|
596624c1bf | ||
|
|
4aca34d4c2 |
@@ -1,2 +1,11 @@
|
||||
#!/bin/sh
|
||||
sasjs lint
|
||||
#!/bin/bash
|
||||
sasjs lint
|
||||
|
||||
# Avoid commits to the master branch
|
||||
BRANCH=`git rev-parse --abbrev-ref HEAD`
|
||||
|
||||
if [[ "$BRANCH" =~ ^(master|main|develop)$ ]]; then
|
||||
echo "You are on branch $BRANCH. Are you sure you want to commit to this branch?"
|
||||
echo "If so, commit with -n to bypass the pre-commit hook."
|
||||
exit 1
|
||||
fi
|
||||
197
all.sas
197
all.sas
@@ -79,7 +79,7 @@ options noquotelenmax;
|
||||
Run without arguments to see a list of detectable features.
|
||||
Note - this list is based on known versions of SAS rather than
|
||||
actual feature detection, as that is tricky / impossible to do
|
||||
without generating errors in most cases.
|
||||
without generating errs in most cases.
|
||||
|
||||
%put %mf_existfeature(PROCLUA);
|
||||
|
||||
@@ -362,7 +362,7 @@ https://github.com/yabwon/SAS_PACKAGES/blob/main/packages/baseplus.md#functionex
|
||||
@param attr full list in [documentation](
|
||||
https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000147794.htm)
|
||||
@return output returns result of the attrc value supplied, or -1 and log
|
||||
message if error.
|
||||
message if err.
|
||||
|
||||
@version 9.2
|
||||
@author Allan Bowe
|
||||
@@ -395,7 +395,7 @@ https://github.com/yabwon/SAS_PACKAGES/blob/main/packages/baseplus.md#functionex
|
||||
@param attr Common values are NLOBS and NVARS, full list in [documentation](
|
||||
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212040.htm)
|
||||
@return output returns result of the attrn value supplied, or -1 and log
|
||||
message if error.
|
||||
message if err.
|
||||
|
||||
@version 9.2
|
||||
@author Allan Bowe
|
||||
@@ -1340,6 +1340,38 @@ Usage:
|
||||
&is_directory
|
||||
|
||||
%mend mf_isdir;/**
|
||||
@file
|
||||
@brief Returns 1 if the variable contains only digits 0-9, else 0
|
||||
@details Note that numerics containing any punctuation (including decimals
|
||||
or exponents) will be flagged zero.
|
||||
|
||||
If you'd like support for this, then do raise an issue (or even better, a
|
||||
pull request!)
|
||||
|
||||
Usage:
|
||||
|
||||
%put %mf_isint(1) returns 1;
|
||||
%put %mf_isint(1.1) returns 0;
|
||||
%put %mf_isint(%str(1,1)) returns 0;
|
||||
|
||||
@param [in] arg input value to check
|
||||
|
||||
@version 9.2
|
||||
**/
|
||||
|
||||
%macro mf_isint(arg
|
||||
)/*/STORE SOURCE*/;
|
||||
/* remove minus sign if exists */
|
||||
|
||||
%local val;
|
||||
%if "%substr(%str(&arg),1,1)"="-" %then %let val=%substr(%str(&arg),2);
|
||||
%else %let val=&arg;
|
||||
|
||||
/* check remaining chars */
|
||||
%if %sysfunc(findc(%str(&val),,kd)) %then %do;0%end;
|
||||
%else %do;1%end;
|
||||
|
||||
%mend mf_isint;/**
|
||||
@file
|
||||
@brief Returns physical location of various SAS items
|
||||
@details Returns location of the PlatformObjectFramework tools
|
||||
@@ -1421,7 +1453,7 @@ Usage:
|
||||
%end;
|
||||
|
||||
/*
|
||||
Now create the directory. Complain loudly of any errors.
|
||||
Now create the directory. Complain loudly of any errs.
|
||||
*/
|
||||
|
||||
%let dname = %sysfunc(dcreate(&child, &parent));
|
||||
@@ -1468,7 +1500,7 @@ Usage:
|
||||
@param libds library.dataset
|
||||
|
||||
@return output returns result of the attrn value supplied, or log message
|
||||
if error.
|
||||
if err.
|
||||
|
||||
|
||||
@version 9.2
|
||||
@@ -2790,6 +2822,8 @@ run;
|
||||
|
||||
/* create folders and copy content */
|
||||
data _null_;
|
||||
length msg $200;
|
||||
call missing(msg);
|
||||
set work.&tempds;
|
||||
if _n_ = 1 then dpos+sum(length(directory),2);
|
||||
filepath2="&target/"!!substr(filepath,dpos);
|
||||
@@ -2799,9 +2833,9 @@ run;
|
||||
rc1=filename(fref1,filepath,'disk','recfm=n');
|
||||
rc2=filename(fref2,filepath2,'disk','recfm=n');
|
||||
if fcopy(fref1,fref2) ne 0 then do;
|
||||
sysmsg=sysmsg();
|
||||
msg=sysmsg();
|
||||
putlog "%str(ERR)OR: Unable to copy " filepath " to " filepath2;
|
||||
putlog sysmg=;
|
||||
putlog msg=;
|
||||
end;
|
||||
end;
|
||||
rc=filename(fref1);
|
||||
@@ -3876,17 +3910,23 @@ run;
|
||||
|
||||
%mend mp_ds2csv;/**
|
||||
@file
|
||||
@brief Converts every value in a dataset to it's formatted value
|
||||
@brief Converts every value in a dataset to formatted value
|
||||
@details Converts every value to it's formatted value. All variables will
|
||||
become character, and will be in the same order.
|
||||
become character, and will be in the same order as the original dataset.
|
||||
|
||||
Lengths will be adjusted according to the format lengths, where applicable.
|
||||
|
||||
Usage:
|
||||
|
||||
%mp_ds2fmtds(sashelp.cars,work.cars)
|
||||
%mp_ds2fmtds(sashelp.vallopt,vw_vallopt)
|
||||
|
||||
@param [in] libds The library.dataset to be converted
|
||||
@param [out] outds The dataset to create.
|
||||
|
||||
<h4> SAS Macros </h4>
|
||||
@li mf_existds.sas
|
||||
|
||||
<h4> Related Macros <h4>
|
||||
@li mp_jsonout.sas
|
||||
|
||||
@@ -3898,8 +3938,9 @@ run;
|
||||
)/*/STORE SOURCE*/;
|
||||
|
||||
/* validations */
|
||||
%if not %sysfunc(exist(&libds)) %then %do;
|
||||
%put %str(WARN)ING: &libds does not exist;
|
||||
|
||||
%if not %mf_existds(libds=&libds) %then %do;
|
||||
%put %str(WARN)ING: &libds does not exist as either a VIEW or DATASET;
|
||||
%return;
|
||||
%end;
|
||||
%if %index(&libds,.)=0 %then %let libds=WORK.&libds;
|
||||
@@ -4089,6 +4130,7 @@ data _null_;
|
||||
if _n_>&maxobs then stop;
|
||||
%end;
|
||||
length _____str $32767;
|
||||
call missing(_____str);
|
||||
format _numeric_ best.;
|
||||
format _character_ ;
|
||||
%local i comma var vtype vfmt;
|
||||
@@ -4489,8 +4531,8 @@ filename &outref temp;
|
||||
@param [in] targetds The target dataset against which to verify the query
|
||||
@param [out] abort= (YES) If YES will call mp_abort.sas on any exceptions
|
||||
@param [out] outds= (work.mp_filtervalidate) Output dataset containing the
|
||||
error / warning message, if one exists. If this table contains any rows,
|
||||
there are problems!
|
||||
err / warning message, if one exists. If this table contains any rows,
|
||||
there are problems!
|
||||
|
||||
<h4> SAS Macros </h4>
|
||||
@li mf_getuniquefileref.sas
|
||||
@@ -4674,17 +4716,17 @@ run;
|
||||
%let vw=%mf_getuniquename(prefix=mp_getconstraints_vw_);
|
||||
data &vw /view=&vw;
|
||||
set sashelp.vcncolu;
|
||||
where TABLE_CATALOG="&lib";
|
||||
where table_catalog="&lib";
|
||||
|
||||
/* use retain approach to reset the constraint order with each constraint */
|
||||
length tmp $1000;
|
||||
retain tmp;
|
||||
drop tmp;
|
||||
if tmp ne catx('|',libref,table_name,constraint_type,constraint_name) then do;
|
||||
if tmp ne catx('|',table_catalog,table_name,constraint_name) then do;
|
||||
constraint_order=1;
|
||||
end;
|
||||
else constraint_order+1;
|
||||
tmp=catx('|',libref, table_name, constraint_type,constraint_name);
|
||||
tmp=catx('|',table_catalog, table_name,constraint_name);
|
||||
run;
|
||||
|
||||
/* must use SQL as proc datasets does not support length changes */
|
||||
@@ -5194,7 +5236,7 @@ run;
|
||||
%let curds=%scan(&dsnlist,&x);
|
||||
data _null_;
|
||||
file &fref mod;
|
||||
length nm lab $1024 typ $20;
|
||||
length lab $1024 typ $20;
|
||||
set &colinfo (where=(upcase(memname)="&curds")) end=last;
|
||||
|
||||
if _n_=1 then do;
|
||||
@@ -6169,6 +6211,7 @@ create table &outds as
|
||||
|
||||
@param [in] libds dataset to hash
|
||||
@param [in] salt= Provide a salt (could be, for instance, the dataset name)
|
||||
@param [in] iftrue= A condition under which the macro should be executed.
|
||||
@param [out] outds= (work.mf_hashdataset) The output dataset to create. This
|
||||
will contain one column (hashkey) with one observation (a hex32.
|
||||
representation of the input hash)
|
||||
@@ -6183,10 +6226,14 @@ create table &outds as
|
||||
%macro mp_hashdataset(
|
||||
libds,
|
||||
outds=,
|
||||
salt=
|
||||
salt=,
|
||||
iftrue=%str(1=1)
|
||||
)/*/STORE SOURCE*/;
|
||||
|
||||
%if not(%eval(%unquote(&iftrue))) %then %return;
|
||||
|
||||
%if %mf_getattrn(&libds,NLOBS)=0 %then %do;
|
||||
%put %str(WARN)ING: Dataset &libds is empty;, or is not a dataset;
|
||||
%put %str(WARN)ING: Dataset &libds is empty, or is not a dataset;
|
||||
%end;
|
||||
%else %if %mf_getattrn(&libds,NLOBS)<0 %then %do;
|
||||
%put %str(ERR)OR: Dataset &libds is not a dataset;
|
||||
@@ -6328,15 +6375,25 @@ filename &tempref clear;
|
||||
%mend mp_include;/**
|
||||
@file
|
||||
@brief Initialise session with useful settings and variables
|
||||
@details Implements a set of recommended options for general SAS use. This
|
||||
macro is NOT used elsewhere within the core library (other than in tests),
|
||||
but it is used by the SASjs team when building web services for
|
||||
SAS-Powered applications elsewhere.
|
||||
@details Implements a "strict" set of SAS options for use in defensive
|
||||
programming. Highly recommended, if you want your code to run on some
|
||||
other machine.
|
||||
|
||||
If you have a good idea for an option, setting, or useful global variable -
|
||||
feel free to [raise an issue](https://github.com/sasjs/core/issues/new)!
|
||||
This macro is recommended to be compiled and invoked in the `initProgram`
|
||||
for SASjs [Jobs](https://cli.sasjs.io/sasjsconfig.html#jobConfig_initProgram
|
||||
), [Services](
|
||||
https://cli.sasjs.io/sasjsconfig.html#serviceConfig_initProgram) and [Tests]
|
||||
(https://cli.sasjs.io/sasjsconfig.html#testConfig_initProgram).
|
||||
|
||||
All global variables are prefixed with "SASJS_" (unless modfied with the
|
||||
For non SASjs projects, you could invoke in the autoexec, or in your own
|
||||
solution initialisation macro.
|
||||
|
||||
|
||||
If you have a good idea for another useful option, setting, or global
|
||||
variable - feel free to [raise an issue](
|
||||
https://github.com/sasjs/core/issues/new)!
|
||||
|
||||
All global variables are prefixed with "SASJS" (unless modified with the
|
||||
prefix parameter).
|
||||
|
||||
@param [in] prefix= (SASJS) The prefix to apply to the global macro variables
|
||||
@@ -6366,16 +6423,17 @@ filename &tempref clear;
|
||||
autocorrect /* disallow mis-spelled procedure names */
|
||||
compress=CHAR /* default is none so ensure we have something! */
|
||||
datastmtchk=ALLKEYWORDS /* protection from overwriting input datasets */
|
||||
errorcheck=STRICT /* catch errors in libname/filename statements */
|
||||
fmterr /* ensure error when a format cannot be found */
|
||||
mergenoby=ERROR /*
|
||||
missing=. /* some sites change this which causes hard to detect errors */
|
||||
%str(err)orcheck=STRICT /* catch errs in libname/filename statements */
|
||||
fmterr /* ensure err when a format cannot be found */
|
||||
mergenoby=%str(ERR)OR /* throw err when a merge has no BY variables */
|
||||
missing=. /* changing this can cause hard to detect errs */
|
||||
noquotelenmax /* avoid warnings for long strings */
|
||||
noreplace /* avoid overwriting permanent datasets */
|
||||
ps=max /* reduce log size slightly */
|
||||
validmemname=COMPATIBLE /* avoid special characters etc in table names */
|
||||
validvarname=V7 /* avoid special characters etc in variable names */
|
||||
varlenchk=ERROR /* fail hard if truncation (data loss) can result */
|
||||
varinitchk=%str(ERR)OR /* avoid data mistakes from variable name typos */
|
||||
varlenchk=%str(ERR)OR /* fail hard if truncation (data loss) can result */
|
||||
;
|
||||
|
||||
%mend mp_init;/**
|
||||
@@ -6443,7 +6501,7 @@ filename &tempref clear;
|
||||
%if &action=OPEN %then %do;
|
||||
options nobomfile;
|
||||
data _null_;file &jref encoding='utf-8';
|
||||
put '{"START_DTTM" : "' "%sysfunc(datetime(),datetime20.3)" '"';
|
||||
put '{"PROCESSED_DTTM" : "' "%sysfunc(datetime(),E8601DT26.6)" '"';
|
||||
run;
|
||||
%end;
|
||||
%else %if (&action=ARR or &action=OBJ) %then %do;
|
||||
@@ -7005,7 +7063,7 @@ run;
|
||||
%let abortme=1;
|
||||
%end;
|
||||
|
||||
/* catch errors - mp_abort must be called outside of a logic block */
|
||||
/* catch errs - mp_abort must be called outside of a logic block */
|
||||
%mp_abort(iftrue=(&abortme=1),
|
||||
msg=%superq(msg),
|
||||
mac=&sysmacroname
|
||||
@@ -7737,22 +7795,28 @@ drop table &tempds;
|
||||
Usage:
|
||||
|
||||
%mp_searchdata(lib=sashelp, string=Jan)
|
||||
%mp_searchdata(lib=sashelp, numval=1)
|
||||
%mp_searchdata(lib=sashelp, ds=bird, numval=1)
|
||||
%mp_searchdata(lib=sashelp, ds=class, string=l,outobs=5)
|
||||
|
||||
|
||||
Outputs zero or more tables to an MPSEARCH library with specific records.
|
||||
|
||||
@param lib= the libref to search (should be already assigned)
|
||||
@param ds= the dataset to search (leave blank to search entire library)
|
||||
@param string= the string value to search
|
||||
@param numval= the numeric value to search (must be exact)
|
||||
@param outloc= the directory in which to create the output datasets with
|
||||
matching rows. Will default to a subfolder in the WORK library.
|
||||
@param outobs= set to a positive integer to restrict the number of
|
||||
@param [in] lib= The libref to search (should be already assigned)
|
||||
@param [in] ds= The dataset to search (leave blank to search entire library)
|
||||
@param [in] string= String value to search (case sensitive, can be partial)
|
||||
@param [in] numval= Numeric value to search (must be exact)
|
||||
@param [out] outloc= (0) Optionally specify the directory in which to
|
||||
create the the output datasets with matching rows. By default it will
|
||||
write them to a temporary subdirectory within the WORK folder.
|
||||
@param [out] outlib= (MPSEARCH) Assign a different libref to the output
|
||||
library containing the matching datasets / records
|
||||
@param [in] outobs= set to a positive integer to restrict the number of
|
||||
observations
|
||||
@param filter_text= add a (valid) filter clause to further filter the results
|
||||
@param [in] filter_text= (1=1) Add a (valid) filter clause to further filter
|
||||
the results.
|
||||
|
||||
<h4> SAS Macros </h4>
|
||||
@li mf_getuniquename.sas
|
||||
@li mf_getvarlist.sas
|
||||
@li mf_getvartype.sas
|
||||
@li mf_mkdir.sas
|
||||
@@ -7762,11 +7826,12 @@ drop table &tempds;
|
||||
@author Allan Bowe
|
||||
**/
|
||||
|
||||
%macro mp_searchdata(lib=sashelp
|
||||
%macro mp_searchdata(lib=
|
||||
,ds=
|
||||
,string= /* the query will use a contains (?) operator */
|
||||
,numval= /* numeric must match exactly */
|
||||
,outloc=%sysfunc(pathname(work))/mpsearch
|
||||
,outloc=0
|
||||
,outlib=MPSEARCH
|
||||
,outobs=-1
|
||||
,filter_text=%str(1=1)
|
||||
)/*/STORE SOURCE*/;
|
||||
@@ -7783,8 +7848,12 @@ drop table &tempds;
|
||||
%if &string = %then %let type=N;
|
||||
%else %let type=C;
|
||||
|
||||
%if "&outloc"="0" %then %do;
|
||||
%let outloc=%sysfunc(pathname(work))/%mf_getuniquename();
|
||||
%end;
|
||||
|
||||
%mf_mkdir(&outloc)
|
||||
libname mpsearch "&outloc";
|
||||
libname &outlib "&outloc";
|
||||
|
||||
/* get the list of tables in the library */
|
||||
proc sql noprint;
|
||||
@@ -7796,11 +7865,6 @@ select distinct memname into: table_list separated by ' '
|
||||
%end;
|
||||
;
|
||||
/* check that we have something to check */
|
||||
proc sql
|
||||
%if &outobs>-1 %then %do;
|
||||
outobs=&outobs
|
||||
%end;
|
||||
;
|
||||
%if %length(&table_list)=0 %then %put library &lib contains no tables!;
|
||||
/* loop through each table */
|
||||
%else %do table_num=1 %to %sysfunc(countw(&table_list,%str( )));
|
||||
@@ -7811,10 +7875,10 @@ proc sql
|
||||
%end;
|
||||
%else %do;
|
||||
%let check_tm=%sysfunc(datetime());
|
||||
/* build sql statement */
|
||||
create table mpsearch.&table as select * from &lib..&table
|
||||
where %unquote(&filter_text) and
|
||||
(0
|
||||
/* prep input */
|
||||
data &outlib..&table;
|
||||
set &lib..&table;
|
||||
where %unquote(&filter_text) and ( 0
|
||||
/* loop through columns */
|
||||
%do colnum=1 %to %sysfunc(countw(&vars,%str( )));
|
||||
%let col=%scan(&vars,&colnum,%str( ));
|
||||
@@ -7828,15 +7892,20 @@ proc sql
|
||||
or ("&col"n = &numval)
|
||||
%end;
|
||||
%end;
|
||||
);
|
||||
);
|
||||
%if &outobs>-1 %then %do;
|
||||
if _n_ > &outobs then stop;
|
||||
%end;
|
||||
run;
|
||||
%put Search query for &table took
|
||||
%sysevalf(%sysfunc(datetime())-&check_tm) seconds;
|
||||
%if &sqlrc ne 0 %then %do;
|
||||
%put %str(WAR)NING: SQLRC=&sqlrc when processing &table;
|
||||
%if &syscc ne 0 %then %do;
|
||||
%put %str(ERR)ROR: SYSCC=&syscc when processing &lib..&table;
|
||||
%return;
|
||||
%end;
|
||||
%if %mf_nobs(mpsearch.&table)=0 %then %do;
|
||||
drop table mpsearch.&table;
|
||||
%if %mf_nobs(&outlib..&table)=0 %then %do;
|
||||
proc sql;
|
||||
drop table &outlib..&table;
|
||||
%end;
|
||||
%end;
|
||||
%end;
|
||||
@@ -11096,7 +11165,7 @@ data _null_;
|
||||
put '%if &action=OPEN %then %do; ';
|
||||
put ' options nobomfile; ';
|
||||
put ' data _null_;file &jref encoding=''utf-8''; ';
|
||||
put ' put ''{"START_DTTM" : "'' "%sysfunc(datetime(),datetime20.3)" ''"''; ';
|
||||
put ' put ''{"PROCESSED_DTTM" : "'' "%sysfunc(datetime(),E8601DT26.6)" ''"''; ';
|
||||
put ' run; ';
|
||||
put '%end; ';
|
||||
put '%else %if (&action=ARR or &action=OBJ) %then %do; ';
|
||||
@@ -16178,7 +16247,7 @@ data _null_;
|
||||
put '%if &action=OPEN %then %do; ';
|
||||
put ' options nobomfile; ';
|
||||
put ' data _null_;file &jref encoding=''utf-8''; ';
|
||||
put ' put ''{"START_DTTM" : "'' "%sysfunc(datetime(),datetime20.3)" ''"''; ';
|
||||
put ' put ''{"PROCESSED_DTTM" : "'' "%sysfunc(datetime(),E8601DT26.6)" ''"''; ';
|
||||
put ' run; ';
|
||||
put '%end; ';
|
||||
put '%else %if (&action=ARR or &action=OBJ) %then %do; ';
|
||||
@@ -17402,6 +17471,8 @@ options noquotelenmax;
|
||||
%local href cnt;
|
||||
%let cnt=0;
|
||||
data _null_;
|
||||
length rel href $512;
|
||||
call missing(rel,href);
|
||||
set &libref1..links;
|
||||
if rel='members' then do;
|
||||
url=cats("'","&base_uri",href,"?limit=10000'");
|
||||
@@ -17425,6 +17496,7 @@ options noquotelenmax;
|
||||
libname &libref2 JSON fileref=&fname2;
|
||||
data &outds;
|
||||
length id $36 name $128 uri $64 type $32 description $256;
|
||||
if _n_=1 then call missing (of _all_);
|
||||
set &libref2..items;
|
||||
run;
|
||||
filename &fname2 clear;
|
||||
@@ -17720,6 +17792,8 @@ data;run;
|
||||
%local joburi;
|
||||
%let joburi=0;
|
||||
data _null_;
|
||||
length name uri $512;
|
||||
call missing(name,uri);
|
||||
set &foldermembers;
|
||||
if name="&name" and uri=:'/jobDefinitions/definitions'
|
||||
then call symputx('joburi',uri);
|
||||
@@ -19509,6 +19583,7 @@ run;
|
||||
data &outds;
|
||||
format _program uri $128. state $32. stateDetails $32. timestamp datetime19.
|
||||
jobparams $32767.;
|
||||
call missing (of _all_);
|
||||
stop;
|
||||
run;
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
Run without arguments to see a list of detectable features.
|
||||
Note - this list is based on known versions of SAS rather than
|
||||
actual feature detection, as that is tricky / impossible to do
|
||||
without generating errors in most cases.
|
||||
without generating errs in most cases.
|
||||
|
||||
%put %mf_existfeature(PROCLUA);
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
@param attr full list in [documentation](
|
||||
https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000147794.htm)
|
||||
@return output returns result of the attrc value supplied, or -1 and log
|
||||
message if error.
|
||||
message if err.
|
||||
|
||||
@version 9.2
|
||||
@author Allan Bowe
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
@param attr Common values are NLOBS and NVARS, full list in [documentation](
|
||||
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212040.htm)
|
||||
@return output returns result of the attrn value supplied, or -1 and log
|
||||
message if error.
|
||||
message if err.
|
||||
|
||||
@version 9.2
|
||||
@author Allan Bowe
|
||||
|
||||
33
base/mf_isint.sas
Normal file
33
base/mf_isint.sas
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
@file
|
||||
@brief Returns 1 if the variable contains only digits 0-9, else 0
|
||||
@details Note that numerics containing any punctuation (including decimals
|
||||
or exponents) will be flagged zero.
|
||||
|
||||
If you'd like support for this, then do raise an issue (or even better, a
|
||||
pull request!)
|
||||
|
||||
Usage:
|
||||
|
||||
%put %mf_isint(1) returns 1;
|
||||
%put %mf_isint(1.1) returns 0;
|
||||
%put %mf_isint(%str(1,1)) returns 0;
|
||||
|
||||
@param [in] arg input value to check
|
||||
|
||||
@version 9.2
|
||||
**/
|
||||
|
||||
%macro mf_isint(arg
|
||||
)/*/STORE SOURCE*/;
|
||||
/* remove minus sign if exists */
|
||||
|
||||
%local val;
|
||||
%if "%substr(%str(&arg),1,1)"="-" %then %let val=%substr(%str(&arg),2);
|
||||
%else %let val=&arg;
|
||||
|
||||
/* check remaining chars */
|
||||
%if %sysfunc(findc(%str(&val),,kd)) %then %do;0%end;
|
||||
%else %do;1%end;
|
||||
|
||||
%mend mf_isint;
|
||||
@@ -51,7 +51,7 @@ Usage:
|
||||
%end;
|
||||
|
||||
/*
|
||||
Now create the directory. Complain loudly of any errors.
|
||||
Now create the directory. Complain loudly of any errs.
|
||||
*/
|
||||
|
||||
%let dname = %sysfunc(dcreate(&child, &parent));
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
@param libds library.dataset
|
||||
|
||||
@return output returns result of the attrn value supplied, or log message
|
||||
if error.
|
||||
if err.
|
||||
|
||||
|
||||
@version 9.2
|
||||
|
||||
@@ -54,6 +54,8 @@
|
||||
|
||||
/* create folders and copy content */
|
||||
data _null_;
|
||||
length msg $200;
|
||||
call missing(msg);
|
||||
set work.&tempds;
|
||||
if _n_ = 1 then dpos+sum(length(directory),2);
|
||||
filepath2="&target/"!!substr(filepath,dpos);
|
||||
@@ -63,9 +65,9 @@
|
||||
rc1=filename(fref1,filepath,'disk','recfm=n');
|
||||
rc2=filename(fref2,filepath2,'disk','recfm=n');
|
||||
if fcopy(fref1,fref2) ne 0 then do;
|
||||
sysmsg=sysmsg();
|
||||
msg=sysmsg();
|
||||
putlog "%str(ERR)OR: Unable to copy " filepath " to " filepath2;
|
||||
putlog sysmg=;
|
||||
putlog msg=;
|
||||
end;
|
||||
end;
|
||||
rc=filename(fref1);
|
||||
|
||||
@@ -1,16 +1,22 @@
|
||||
/**
|
||||
@file
|
||||
@brief Converts every value in a dataset to it's formatted value
|
||||
@brief Converts every value in a dataset to formatted value
|
||||
@details Converts every value to it's formatted value. All variables will
|
||||
become character, and will be in the same order.
|
||||
become character, and will be in the same order as the original dataset.
|
||||
|
||||
Lengths will be adjusted according to the format lengths, where applicable.
|
||||
|
||||
Usage:
|
||||
|
||||
%mp_ds2fmtds(sashelp.cars,work.cars)
|
||||
%mp_ds2fmtds(sashelp.vallopt,vw_vallopt)
|
||||
|
||||
@param [in] libds The library.dataset to be converted
|
||||
@param [out] outds The dataset to create.
|
||||
|
||||
<h4> SAS Macros </h4>
|
||||
@li mf_existds.sas
|
||||
|
||||
<h4> Related Macros <h4>
|
||||
@li mp_jsonout.sas
|
||||
|
||||
@@ -22,8 +28,9 @@
|
||||
)/*/STORE SOURCE*/;
|
||||
|
||||
/* validations */
|
||||
%if not %sysfunc(exist(&libds)) %then %do;
|
||||
%put %str(WARN)ING: &libds does not exist;
|
||||
|
||||
%if not %mf_existds(libds=&libds) %then %do;
|
||||
%put %str(WARN)ING: &libds does not exist as either a VIEW or DATASET;
|
||||
%return;
|
||||
%end;
|
||||
%if %index(&libds,.)=0 %then %let libds=WORK.&libds;
|
||||
|
||||
@@ -116,6 +116,7 @@ data _null_;
|
||||
if _n_>&maxobs then stop;
|
||||
%end;
|
||||
length _____str $32767;
|
||||
call missing(_____str);
|
||||
format _numeric_ best.;
|
||||
format _character_ ;
|
||||
%local i comma var vtype vfmt;
|
||||
|
||||
@@ -33,8 +33,8 @@
|
||||
@param [in] targetds The target dataset against which to verify the query
|
||||
@param [out] abort= (YES) If YES will call mp_abort.sas on any exceptions
|
||||
@param [out] outds= (work.mp_filtervalidate) Output dataset containing the
|
||||
error / warning message, if one exists. If this table contains any rows,
|
||||
there are problems!
|
||||
err / warning message, if one exists. If this table contains any rows,
|
||||
there are problems!
|
||||
|
||||
<h4> SAS Macros </h4>
|
||||
@li mf_getuniquefileref.sas
|
||||
|
||||
@@ -48,17 +48,17 @@
|
||||
%let vw=%mf_getuniquename(prefix=mp_getconstraints_vw_);
|
||||
data &vw /view=&vw;
|
||||
set sashelp.vcncolu;
|
||||
where TABLE_CATALOG="&lib";
|
||||
where table_catalog="&lib";
|
||||
|
||||
/* use retain approach to reset the constraint order with each constraint */
|
||||
length tmp $1000;
|
||||
retain tmp;
|
||||
drop tmp;
|
||||
if tmp ne catx('|',libref,table_name,constraint_type,constraint_name) then do;
|
||||
if tmp ne catx('|',table_catalog,table_name,constraint_name) then do;
|
||||
constraint_order=1;
|
||||
end;
|
||||
else constraint_order+1;
|
||||
tmp=catx('|',libref, table_name, constraint_type,constraint_name);
|
||||
tmp=catx('|',table_catalog, table_name,constraint_name);
|
||||
run;
|
||||
|
||||
/* must use SQL as proc datasets does not support length changes */
|
||||
|
||||
@@ -139,7 +139,7 @@ run;
|
||||
%let curds=%scan(&dsnlist,&x);
|
||||
data _null_;
|
||||
file &fref mod;
|
||||
length nm lab $1024 typ $20;
|
||||
length lab $1024 typ $20;
|
||||
set &colinfo (where=(upcase(memname)="&curds")) end=last;
|
||||
|
||||
if _n_=1 then do;
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
@param [in] libds dataset to hash
|
||||
@param [in] salt= Provide a salt (could be, for instance, the dataset name)
|
||||
@param [in] iftrue= A condition under which the macro should be executed.
|
||||
@param [out] outds= (work.mf_hashdataset) The output dataset to create. This
|
||||
will contain one column (hashkey) with one observation (a hex32.
|
||||
representation of the input hash)
|
||||
@@ -35,10 +36,14 @@
|
||||
%macro mp_hashdataset(
|
||||
libds,
|
||||
outds=,
|
||||
salt=
|
||||
salt=,
|
||||
iftrue=%str(1=1)
|
||||
)/*/STORE SOURCE*/;
|
||||
|
||||
%if not(%eval(%unquote(&iftrue))) %then %return;
|
||||
|
||||
%if %mf_getattrn(&libds,NLOBS)=0 %then %do;
|
||||
%put %str(WARN)ING: Dataset &libds is empty;, or is not a dataset;
|
||||
%put %str(WARN)ING: Dataset &libds is empty, or is not a dataset;
|
||||
%end;
|
||||
%else %if %mf_getattrn(&libds,NLOBS)<0 %then %do;
|
||||
%put %str(ERR)OR: Dataset &libds is not a dataset;
|
||||
|
||||
@@ -1,15 +1,25 @@
|
||||
/**
|
||||
@file
|
||||
@brief Initialise session with useful settings and variables
|
||||
@details Implements a set of recommended options for general SAS use. This
|
||||
macro is NOT used elsewhere within the core library (other than in tests),
|
||||
but it is used by the SASjs team when building web services for
|
||||
SAS-Powered applications elsewhere.
|
||||
@details Implements a "strict" set of SAS options for use in defensive
|
||||
programming. Highly recommended, if you want your code to run on some
|
||||
other machine.
|
||||
|
||||
If you have a good idea for an option, setting, or useful global variable -
|
||||
feel free to [raise an issue](https://github.com/sasjs/core/issues/new)!
|
||||
This macro is recommended to be compiled and invoked in the `initProgram`
|
||||
for SASjs [Jobs](https://cli.sasjs.io/sasjsconfig.html#jobConfig_initProgram
|
||||
), [Services](
|
||||
https://cli.sasjs.io/sasjsconfig.html#serviceConfig_initProgram) and [Tests]
|
||||
(https://cli.sasjs.io/sasjsconfig.html#testConfig_initProgram).
|
||||
|
||||
All global variables are prefixed with "SASJS_" (unless modfied with the
|
||||
For non SASjs projects, you could invoke in the autoexec, or in your own
|
||||
solution initialisation macro.
|
||||
|
||||
|
||||
If you have a good idea for another useful option, setting, or global
|
||||
variable - feel free to [raise an issue](
|
||||
https://github.com/sasjs/core/issues/new)!
|
||||
|
||||
All global variables are prefixed with "SASJS" (unless modified with the
|
||||
prefix parameter).
|
||||
|
||||
@param [in] prefix= (SASJS) The prefix to apply to the global macro variables
|
||||
@@ -39,16 +49,17 @@
|
||||
autocorrect /* disallow mis-spelled procedure names */
|
||||
compress=CHAR /* default is none so ensure we have something! */
|
||||
datastmtchk=ALLKEYWORDS /* protection from overwriting input datasets */
|
||||
errorcheck=STRICT /* catch errors in libname/filename statements */
|
||||
fmterr /* ensure error when a format cannot be found */
|
||||
mergenoby=ERROR /*
|
||||
missing=. /* some sites change this which causes hard to detect errors */
|
||||
%str(err)orcheck=STRICT /* catch errs in libname/filename statements */
|
||||
fmterr /* ensure err when a format cannot be found */
|
||||
mergenoby=%str(ERR)OR /* throw err when a merge has no BY variables */
|
||||
missing=. /* changing this can cause hard to detect errs */
|
||||
noquotelenmax /* avoid warnings for long strings */
|
||||
noreplace /* avoid overwriting permanent datasets */
|
||||
ps=max /* reduce log size slightly */
|
||||
validmemname=COMPATIBLE /* avoid special characters etc in table names */
|
||||
validvarname=V7 /* avoid special characters etc in variable names */
|
||||
varlenchk=ERROR /* fail hard if truncation (data loss) can result */
|
||||
varinitchk=%str(ERR)OR /* avoid data mistakes from variable name typos */
|
||||
varlenchk=%str(ERR)OR /* fail hard if truncation (data loss) can result */
|
||||
;
|
||||
|
||||
%mend mp_init;
|
||||
@@ -63,7 +63,7 @@
|
||||
%if &action=OPEN %then %do;
|
||||
options nobomfile;
|
||||
data _null_;file &jref encoding='utf-8';
|
||||
put '{"START_DTTM" : "' "%sysfunc(datetime(),datetime20.3)" '"';
|
||||
put '{"PROCESSED_DTTM" : "' "%sysfunc(datetime(),E8601DT26.6)" '"';
|
||||
run;
|
||||
%end;
|
||||
%else %if (&action=ARR or &action=OBJ) %then %do;
|
||||
|
||||
@@ -239,7 +239,7 @@ run;
|
||||
%let abortme=1;
|
||||
%end;
|
||||
|
||||
/* catch errors - mp_abort must be called outside of a logic block */
|
||||
/* catch errs - mp_abort must be called outside of a logic block */
|
||||
%mp_abort(iftrue=(&abortme=1),
|
||||
msg=%superq(msg),
|
||||
mac=&sysmacroname
|
||||
|
||||
@@ -11,22 +11,28 @@
|
||||
Usage:
|
||||
|
||||
%mp_searchdata(lib=sashelp, string=Jan)
|
||||
%mp_searchdata(lib=sashelp, numval=1)
|
||||
%mp_searchdata(lib=sashelp, ds=bird, numval=1)
|
||||
%mp_searchdata(lib=sashelp, ds=class, string=l,outobs=5)
|
||||
|
||||
|
||||
Outputs zero or more tables to an MPSEARCH library with specific records.
|
||||
|
||||
@param lib= the libref to search (should be already assigned)
|
||||
@param ds= the dataset to search (leave blank to search entire library)
|
||||
@param string= the string value to search
|
||||
@param numval= the numeric value to search (must be exact)
|
||||
@param outloc= the directory in which to create the output datasets with
|
||||
matching rows. Will default to a subfolder in the WORK library.
|
||||
@param outobs= set to a positive integer to restrict the number of
|
||||
@param [in] lib= The libref to search (should be already assigned)
|
||||
@param [in] ds= The dataset to search (leave blank to search entire library)
|
||||
@param [in] string= String value to search (case sensitive, can be partial)
|
||||
@param [in] numval= Numeric value to search (must be exact)
|
||||
@param [out] outloc= (0) Optionally specify the directory in which to
|
||||
create the the output datasets with matching rows. By default it will
|
||||
write them to a temporary subdirectory within the WORK folder.
|
||||
@param [out] outlib= (MPSEARCH) Assign a different libref to the output
|
||||
library containing the matching datasets / records
|
||||
@param [in] outobs= set to a positive integer to restrict the number of
|
||||
observations
|
||||
@param filter_text= add a (valid) filter clause to further filter the results
|
||||
@param [in] filter_text= (1=1) Add a (valid) filter clause to further filter
|
||||
the results.
|
||||
|
||||
<h4> SAS Macros </h4>
|
||||
@li mf_getuniquename.sas
|
||||
@li mf_getvarlist.sas
|
||||
@li mf_getvartype.sas
|
||||
@li mf_mkdir.sas
|
||||
@@ -36,11 +42,12 @@
|
||||
@author Allan Bowe
|
||||
**/
|
||||
|
||||
%macro mp_searchdata(lib=sashelp
|
||||
%macro mp_searchdata(lib=
|
||||
,ds=
|
||||
,string= /* the query will use a contains (?) operator */
|
||||
,numval= /* numeric must match exactly */
|
||||
,outloc=%sysfunc(pathname(work))/mpsearch
|
||||
,outloc=0
|
||||
,outlib=MPSEARCH
|
||||
,outobs=-1
|
||||
,filter_text=%str(1=1)
|
||||
)/*/STORE SOURCE*/;
|
||||
@@ -57,8 +64,12 @@
|
||||
%if &string = %then %let type=N;
|
||||
%else %let type=C;
|
||||
|
||||
%if "&outloc"="0" %then %do;
|
||||
%let outloc=%sysfunc(pathname(work))/%mf_getuniquename();
|
||||
%end;
|
||||
|
||||
%mf_mkdir(&outloc)
|
||||
libname mpsearch "&outloc";
|
||||
libname &outlib "&outloc";
|
||||
|
||||
/* get the list of tables in the library */
|
||||
proc sql noprint;
|
||||
@@ -70,11 +81,6 @@ select distinct memname into: table_list separated by ' '
|
||||
%end;
|
||||
;
|
||||
/* check that we have something to check */
|
||||
proc sql
|
||||
%if &outobs>-1 %then %do;
|
||||
outobs=&outobs
|
||||
%end;
|
||||
;
|
||||
%if %length(&table_list)=0 %then %put library &lib contains no tables!;
|
||||
/* loop through each table */
|
||||
%else %do table_num=1 %to %sysfunc(countw(&table_list,%str( )));
|
||||
@@ -85,10 +91,10 @@ proc sql
|
||||
%end;
|
||||
%else %do;
|
||||
%let check_tm=%sysfunc(datetime());
|
||||
/* build sql statement */
|
||||
create table mpsearch.&table as select * from &lib..&table
|
||||
where %unquote(&filter_text) and
|
||||
(0
|
||||
/* prep input */
|
||||
data &outlib..&table;
|
||||
set &lib..&table;
|
||||
where %unquote(&filter_text) and ( 0
|
||||
/* loop through columns */
|
||||
%do colnum=1 %to %sysfunc(countw(&vars,%str( )));
|
||||
%let col=%scan(&vars,&colnum,%str( ));
|
||||
@@ -102,15 +108,20 @@ proc sql
|
||||
or ("&col"n = &numval)
|
||||
%end;
|
||||
%end;
|
||||
);
|
||||
);
|
||||
%if &outobs>-1 %then %do;
|
||||
if _n_ > &outobs then stop;
|
||||
%end;
|
||||
run;
|
||||
%put Search query for &table took
|
||||
%sysevalf(%sysfunc(datetime())-&check_tm) seconds;
|
||||
%if &sqlrc ne 0 %then %do;
|
||||
%put %str(WAR)NING: SQLRC=&sqlrc when processing &table;
|
||||
%if &syscc ne 0 %then %do;
|
||||
%put %str(ERR)ROR: SYSCC=&syscc when processing &lib..&table;
|
||||
%return;
|
||||
%end;
|
||||
%if %mf_nobs(mpsearch.&table)=0 %then %do;
|
||||
drop table mpsearch.&table;
|
||||
%if %mf_nobs(&outlib..&table)=0 %then %do;
|
||||
proc sql;
|
||||
drop table &outlib..&table;
|
||||
%end;
|
||||
%end;
|
||||
%end;
|
||||
|
||||
@@ -95,7 +95,7 @@ data _null_;
|
||||
put '%if &action=OPEN %then %do; ';
|
||||
put ' options nobomfile; ';
|
||||
put ' data _null_;file &jref encoding=''utf-8''; ';
|
||||
put ' put ''{"START_DTTM" : "'' "%sysfunc(datetime(),datetime20.3)" ''"''; ';
|
||||
put ' put ''{"PROCESSED_DTTM" : "'' "%sysfunc(datetime(),E8601DT26.6)" ''"''; ';
|
||||
put ' run; ';
|
||||
put '%end; ';
|
||||
put '%else %if (&action=ARR or &action=OBJ) %then %do; ';
|
||||
|
||||
33
tests/crossplatform/mf_isint.test.sas
Normal file
33
tests/crossplatform/mf_isint.test.sas
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
@file
|
||||
@brief Testing mf_isint macro
|
||||
|
||||
<h4> SAS Macros </h4>
|
||||
@li mf_isint.sas
|
||||
@li mp_assert.sas
|
||||
|
||||
**/
|
||||
|
||||
%mp_assert(
|
||||
iftrue=(
|
||||
"%mf_isint(1)"="1"
|
||||
),
|
||||
desc=Checking basic mf_isint(1),
|
||||
outds=work.test_results
|
||||
)
|
||||
|
||||
%mp_assert(
|
||||
iftrue=(
|
||||
"%mf_isint(1.1)"="0"
|
||||
),
|
||||
desc=Checking basic mf_isint(1.1),
|
||||
outds=work.test_results
|
||||
)
|
||||
|
||||
%mp_assert(
|
||||
iftrue=(
|
||||
"%mf_isint(-1)"="1"
|
||||
),
|
||||
desc=Checking mf_isint(-1),
|
||||
outds=work.test_results
|
||||
)
|
||||
@@ -15,10 +15,12 @@ filename inc temp;
|
||||
data _null_;
|
||||
set work.test;
|
||||
file inc;
|
||||
line=cats('%mp_ds2fmtds(sashelp.',memname,',',memname,')');
|
||||
libds=cats('sashelp.',memname);
|
||||
if exist(libds) then line=cats('%mp_ds2fmtds(',libds,',',memname,')');
|
||||
put line;
|
||||
run;
|
||||
|
||||
options obs=50;
|
||||
%inc inc;
|
||||
|
||||
%mp_assert(
|
||||
|
||||
29
tests/crossplatform/mp_searchdata.test.sas
Normal file
29
tests/crossplatform/mp_searchdata.test.sas
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
@file
|
||||
@brief Testing mp_searchdata.sas
|
||||
|
||||
<h4> SAS Macros </h4>
|
||||
@li mp_searchdata.sas
|
||||
@li mp_assert.sas
|
||||
|
||||
|
||||
**/
|
||||
|
||||
/** Test 1 - generic useage */
|
||||
|
||||
%mp_searchdata(lib=sashelp, ds=class, string=a)
|
||||
%mp_assert(
|
||||
iftrue=(&syscc=0),
|
||||
desc=No errors in regular usage,
|
||||
outds=work.test_results
|
||||
)
|
||||
|
||||
/** Test 2 - with obs issue */
|
||||
|
||||
%mp_searchdata(lib=sashelp, ds=class, string=l,outobs=5)
|
||||
|
||||
%mp_assert(
|
||||
iftrue=("&SYSWARNINGTEXT" = ""),
|
||||
desc=Ensuring WARN status is clean,
|
||||
outds=work.test_results
|
||||
)
|
||||
@@ -33,6 +33,7 @@ run;
|
||||
%put TEST1: checking web service code;
|
||||
data work.test_results;
|
||||
length test_description $256 test_result $4 test_comments $256;
|
||||
if _n_=1 then call missing (of _all_);
|
||||
infile compare end=eof;
|
||||
input;
|
||||
if eof then do;
|
||||
|
||||
@@ -243,7 +243,7 @@ data _null_;
|
||||
put '%if &action=OPEN %then %do; ';
|
||||
put ' options nobomfile; ';
|
||||
put ' data _null_;file &jref encoding=''utf-8''; ';
|
||||
put ' put ''{"START_DTTM" : "'' "%sysfunc(datetime(),datetime20.3)" ''"''; ';
|
||||
put ' put ''{"PROCESSED_DTTM" : "'' "%sysfunc(datetime(),E8601DT26.6)" ''"''; ';
|
||||
put ' run; ';
|
||||
put '%end; ';
|
||||
put '%else %if (&action=ARR or &action=OBJ) %then %do; ';
|
||||
|
||||
@@ -100,6 +100,8 @@ options noquotelenmax;
|
||||
%local href cnt;
|
||||
%let cnt=0;
|
||||
data _null_;
|
||||
length rel href $512;
|
||||
call missing(rel,href);
|
||||
set &libref1..links;
|
||||
if rel='members' then do;
|
||||
url=cats("'","&base_uri",href,"?limit=10000'");
|
||||
@@ -123,6 +125,7 @@ options noquotelenmax;
|
||||
libname &libref2 JSON fileref=&fname2;
|
||||
data &outds;
|
||||
length id $36 name $128 uri $64 type $32 description $256;
|
||||
if _n_=1 then call missing (of _all_);
|
||||
set &libref2..items;
|
||||
run;
|
||||
filename &fname2 clear;
|
||||
|
||||
@@ -92,6 +92,8 @@ data;run;
|
||||
%local joburi;
|
||||
%let joburi=0;
|
||||
data _null_;
|
||||
length name uri $512;
|
||||
call missing(name,uri);
|
||||
set &foldermembers;
|
||||
if name="&name" and uri=:'/jobDefinitions/definitions'
|
||||
then call symputx('joburi',uri);
|
||||
|
||||
@@ -168,6 +168,7 @@ run;
|
||||
data &outds;
|
||||
format _program uri $128. state $32. stateDetails $32. timestamp datetime19.
|
||||
jobparams $32767.;
|
||||
call missing (of _all_);
|
||||
stop;
|
||||
run;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user