1
0
mirror of https://github.com/sasjs/core.git synced 2025-12-10 14:04:36 +00:00

feat: new mcf_init.sas macro to handle function compilation tracking (and associated test). Further updates to support mp_stackdiffs test results so far

This commit is contained in:
munja
2022-01-24 23:29:43 +01:00
parent 81a17bc0c2
commit 6224844915
14 changed files with 205 additions and 39 deletions

View File

@@ -5,18 +5,19 @@
%put %mf_getfilesize(fpath=C:\temp\myfile.txt);
or
or, provide a libds value as follows:
data x;do x=1 to 100000;y=x;output;end;run;
%put %mf_getfilesize(libds=work.x,format=yes);
gives:
Which gives:
2mb
> 2mb
@param [in] fpath= Full path and filename. Provide this OR the libds value.
@param [in] libds= (0) Library.dataset value (assumes library is BASE engine)
@param [in] format= (NO) Set to yes to apply sizekmg. format
@param fpath= full path and filename. Provide this OR the libds value.
@param libds= library.dataset value (assumes library is BASE engine)
@param format= set to yes to apply sizekmg. format
@returns bytes
@version 9.2
@@ -26,16 +27,32 @@
%macro mf_getfilesize(fpath=,libds=0,format=NO
)/*/STORE SOURCE*/;
%if &libds ne 0 %then %do;
%let fpath=%sysfunc(pathname(%scan(&libds,1,.)))/%scan(&libds,2,.).sas7bdat;
%end;
%local rc fid fref bytes dsid lib vnum;
%local rc fid fref bytes;
%let rc=%sysfunc(filename(fref,&fpath));
%let fid=%sysfunc(fopen(&fref));
%let bytes=%sysfunc(finfo(&fid,File Size (bytes)));
%let rc=%sysfunc(fclose(&fid));
%let rc=%sysfunc(filename(fref));
%if &libds ne 0 %then %do;
%let libds=%upcase(&libds);
%if %index(&libds,.)=0 %then %let lib=WORK;
%else %let lib=%scan(&libds,1,.);
%let dsid=%sysfunc(open(
sashelp.vtable(where=(libname="&lib" and memname="%scan(&libds,-1,.)")
keep=libname memname filesize
)
));
%if (&dsid ^= 0) %then %do;
%let vnum=%sysfunc(varnum(&dsid,FILESIZE));
%let rc=%sysfunc(fetch(&dsid));
%let bytes=%sysfunc(getvarn(&dsid,&vnum));
%let rc= %sysfunc(close(&dsid));
%end;
%else %put &sysmacroname: &libds could not be opened! %sysfunc(sysmsg());
%end;
%else %do;
%let rc=%sysfunc(filename(fref,&fpath));
%let fid=%sysfunc(fopen(&fref));
%let bytes=%sysfunc(finfo(&fid,File Size (bytes)));
%let rc=%sysfunc(fclose(&fid));
%let rc=%sysfunc(filename(fref));
%end;
%if &format=NO %then %do;
&bytes

View File

@@ -12,10 +12,6 @@
%mp_assertdsobs(sashelp.class,test=ATMOST 20) %* pass if <21 obs present;
<h4> SAS Macros </h4>
@li mf_nobs.sas
@li mp_abort.sas
@param [in] inds input dataset to test for presence of observations
@param [in] desc= (Testing observations) The user provided test description
@@ -33,6 +29,11 @@
|---|---|---|
|User Provided description|PASS|Dataset &inds has XX obs|
<h4> SAS Macros </h4>
@li mf_getuniquename.sas
@li mf_nobs.sas
@li mp_abort.sas
<h4> Related Macros </h4>
@li mp_assertcolvals.sas
@li mp_assert.sas
@@ -49,9 +50,10 @@
outds=work.test_results
)/*/STORE SOURCE*/;
%local nobs;
%local nobs ds;
%let nobs=%mf_nobs(&inds);
%let test=%upcase(&test);
%let ds=%mf_getuniquename(prefix=mp_assertdsobs);
%if %substr(&test.xxxxx,1,6)=EQUALS %then %do;
%let val=%scan(&test,2,%str( ));
@@ -84,7 +86,7 @@
)
%end;
data;
data &ds;
length test_description $256 test_result $4 test_comments $256;
test_description=symget('desc');
test_result='FAIL';
@@ -110,9 +112,6 @@
%end;
run;
%local ds;
%let ds=&syslast;
proc append base=&outds data=&ds;
run;

View File

@@ -49,7 +49,7 @@
%macro mp_ds2squeeze(
libds,
outds=work.work.mp_ds2squeeze,
outds=work.mp_ds2squeeze,
mdebug=0
)/*/STORE SOURCE*/;
%local dbg source;
@@ -62,9 +62,10 @@
%let source=/source2;
%end;
%local optval ds fref;
%local optval ds fref startsize;
%let ds=%mf_getuniquename();
%let fref=%mf_getuniquefileref();
%let startsize=%mf_getfilesize(libds=&libds,format=yes);
%mp_getmaxvarlengths(&libds,outds=&ds)
@@ -113,7 +114,7 @@ options varlenchk=&optval;
filename &fref clear;
%end;
%put &sysmacroname: &libds was %mf_getfilesize(libds=&libds,format=yes);
%put &sysmacroname: &libds was &startsize;
%put &sysmacroname: &outds is %mf_getfilesize(libds=&outds,format=yes);
%mend mp_ds2squeeze;

View File

@@ -37,6 +37,7 @@
%global
SASJS_PREFIX /* the ONLY hard-coded global macro variable in SASjs */
&prefix._FUNCTIONS /* used in mcf_init() to track core function compilation */
&prefix._INIT_NUM /* initialisation time as numeric */
&prefix._INIT_DTTM /* initialisation time in E8601DT26.6 format */
&prefix.WORK /* avoid typing %sysfunc(pathname(work)) every time */

View File

@@ -194,6 +194,7 @@
@li mf_nobs.sas
@li mf_wordsinstr1butnotstr2.sas
@li mp_abort.sas
@li mp_ds2squeeze.sas
<h4> Related Macros </h4>
@@ -298,11 +299,14 @@ proc transpose data=&ds1d(where=(tgtvar_type='C'))
id TGTVAR_NM;
var OLDVAL_CHAR;
run;
%mp_ds2squeeze(&ds2d,outds=&ds2d)
%mp_ds2squeeze(&ds3d,outds=&ds3d)
data &outdel;
if 0 then set &baselibds;
set &ds2d;
set &ds3d;
drop key_hash;
if not missing(%scan(&key,1));
run;
proc sort;
by &key;
@@ -328,11 +332,14 @@ proc transpose data=&ds1a(where=(tgtvar_type='C'))
id TGTVAR_NM;
var NEWVAL_CHAR;
run;
%mp_ds2squeeze(&ds2a,outds=&ds2a)
%mp_ds2squeeze(&ds3a,outds=&ds3a)
data &outadd;
if 0 then set &baselibds;
set &ds2a;
set &ds3a;
drop key_hash;
if not missing(%scan(&key,1));
run;
proc sort;
by &key;
@@ -359,10 +366,13 @@ proc transpose data=&ds1m(where=(tgtvar_type='C'))
id TGTVAR_NM;
var NEWVAL_CHAR;
run;
%mp_ds2squeeze(&ds2m,outds=&ds2m)
%mp_ds2squeeze(&ds3m,outds=&ds3m)
data &outmod;
if 0 then set &baselibds;
set &ds2m;
set &ds3m;
if not missing(%scan(&key,1));
run;
proc sort;
by &key;
@@ -375,6 +385,7 @@ run;
* passthrough and a temporary table.
*/
data &pks;
if 0 then set &baselibds;
set &outadd &outmod &outdel;
keep &key;
run;
@@ -382,6 +393,10 @@ run;
proc sort noduprec dupout=&dups;
by &key;
run;
data _null_;
set &dups;
putlog (_all_)(=);
run;
%mp_abort(iftrue= (%mf_nobs(&dups) ne 0)
,mac=&sysmacroname
,msg=%str(duplicates (%mf_nobs(&dups)) found on &auditlibds!)
@@ -542,17 +557,16 @@ select distinct tgtvar_nm into: missvars separated by ' '
%let fref=%mf_getuniquefileref();
data _null_;
file &fref;
set &auditlibds(where=(move_type='D')) end=lastobs;
set &auditlibds(where=(move_type='M')) end=lastobs;
by key_hash;
if _n_=1 then put 'proc sql;';
if first.key_hash then put "update &outmod set ";
comma=ifc(first.key_hash=0,',',' ');
if tgtvar_type='C' then put ' ' comma TGTVAR_NM '=trim("' NEWVAL_CHAR '")';
if last.key_hash then put ' where key_hash=trim("' key_hash '");';
if lastobs then put 'alter &outmod drop key_hash';
if lastobs then put "alter table &outmod drop key_hash;";
run;
%inc &fref/source2;
%end;
%if &mdebug=0 %then %do;

44
fcmp/mcf_init.sas Normal file
View File

@@ -0,0 +1,44 @@
/**
@file
@brief Sets up the mcf_xx functions
@details
There is no (efficient) way to determine if an mcf_xx macro has already been
invoked. So, we make use of a global macro variable list to keep track.
Usage:
%mcf_init(MCF_LENGTH)
Returns:
> 1 (if already initialised) else 0
@param [in] func The function to be initialised
<h4> Related Macros </h4>
@li mcf_init.test.sas
**/
%macro mcf_init(func
)/*/STORE SOURCE*/;
%if not (%symexist(SASJS_PREFIX)) %then %do;
%global SASJS_PREFIX;
%let SASJS_PREFIX=SASJS;
%end;
%let func=%upcase(&func);
/* the / character is just a seperator */
%global &sasjs_prefix._FUNCTIONS;
%if %index(&&&sasjs_prefix._FUNCTIONS,&func/)>0 %then %do;
1
%return;
%end;
%else %do;
%let &sasjs_prefix._FUNCTIONS=&&&sasjs_prefix._FUNCTIONS &func/;
0
%end;
%mend mcf_init;

View File

@@ -40,10 +40,11 @@
Uses a 3 part format: libref.catalog.package
<h4> SAS Macros </h4>
@li mf_existfunction.sas
@li mcf_init.sas
<h4> Related Macros </h4>
<h4> Related Programs </h4>
@li mcf_length.test.sas
@li mp_init.sas
**/
@@ -54,7 +55,7 @@
,pkg=UTILS
)/*/STORE SOURCE*/;
%if %mf_existfunction(mcf_length)=1 %then %return;
%if %mcf_init(mcf_length)=1 %then %return;
%if &wrap=YES %then %do;
proc fcmp outlib=&lib..&cat..&pkg;

View File

@@ -55,7 +55,11 @@
Uses a 3 part format: libref.catalog.package
<h4> SAS Macros </h4>
@li mf_existfunction.sas
@li mcf_init.sas
<h4> Related Programs </h4>
@li mcf_stpsrv_header.test.sas
@li mp_init.sas
**/
@@ -66,7 +70,7 @@
,pkg=UTILS
)/*/STORE SOURCE*/;
%if %mf_existfunction(stpsrv_header)=1 %then %return;
%if %mcf_init(stpsrv_header)=1 %then %return;
%if &wrap=YES %then %do;
proc fcmp outlib=&lib..&cat..&pkg;

View File

@@ -40,7 +40,11 @@
Uses a 3 part format: libref.catalog.package
<h4> SAS Macros </h4>
@li mf_existfunction.sas
@li mcf_init.sas
<h4> Related Programs </h4>
@li mcf_stpsrv_header.test.sas
@li mp_init.sas
**/
@@ -51,7 +55,7 @@
,pkg=UTILS
)/*/STORE SOURCE*/;
%if %mf_existfunction(mcf_string2file)=1 %then %return;
%if %mcf_init(mcf_string2file)=1 %then %return;
%if &wrap=YES %then %do;
proc fcmp outlib=&lib..&cat..&pkg;

View File

@@ -68,7 +68,7 @@
},
{
"name": "server",
"serverUrl": "https://sas.analytium.co.uk:5001",
"serverUrl": "https://sas.analytium.co.uk:5000",
"serverType": "SASJS",
"appLoc": "/Shared Data/temp/macrocore",
"macroFolders": [

View File

@@ -0,0 +1,46 @@
/**
@file
@brief Testing mcf_init.sas macro
<h4> SAS Macros </h4>
@li mcf_init.sas
@li mp_assert.sas
**/
%mp_assert(
iftrue=(%mcf_init(test)=0),
desc=Check if new func returns 0
)
%mp_assert(
iftrue=(&syscc=0),
desc=No errs on basic invocation
)
%mp_assert(
iftrue=(%mcf_init(test)=1),
desc=Check if second invocation returns 1
)
%mp_assert(
iftrue=(&syscc=0),
desc=No errs on second invocation
)
%mp_assert(
iftrue=(%mcf_init(test2)=0),
desc=Check if new invocation returns 0
)
%mp_assert(
iftrue=(%mcf_init(test2)=1),
desc=Check if second new invocation returns 1
)
%mp_assert(
iftrue=(%mcf_init(test)=1),
desc=Check original returns 1
)
%mp_assert(
iftrue=(%mcf_init(t)=0),
desc=Check subset returns 0
)
%mp_assert(
iftrue=(&syscc=0),
desc=No errs at end
)

View File

@@ -0,0 +1,31 @@
/**
@file
@brief Testing mf_getfilesize macro
<h4> SAS Macros </h4>
@li mf_getfilesize.sas
@li mp_assert.sas
@li mp_assertscope.sas
**/
data test;
x=1;
run;
%mp_assertscope(SNAPSHOT)
%put %mf_getfilesize(libds=work.test)
%mp_assertscope(COMPARE)
%mp_assert(
iftrue=(&syscc=0),
desc=Checking syscc,
outds=work.test_results
)
%put %mf_getfilesize(libds=test)
%mp_assert(
iftrue=(&syscc=0),
desc=Checking syscc with one level name,
outds=work.test_results
)

View File

@@ -32,7 +32,9 @@ data work.orig work.deleted work.changed work.appended;
else stop;
run;
%mp_storediffs(sashelp.electric,work.orig,CUSTOMER YEAR
%mp_storediffs(sashelp.electric
,work.orig
,CUSTOMER YEAR
,delds=work.deleted
,modds=work.changed
,appds=work.appended

View File

@@ -14,6 +14,8 @@
/* set defaults */
%mp_init()
%global _debug;
%macro loglevel();
%if "&_debug"="2477" or "&_debug"="fields,log,trace" %then %do;
%put debug mode activated;