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

fix: rebuilding mp_searchdata into data step to avoid SQL warning. Added test and refreshed docs. Closes #108

This commit is contained in:
munja
2021-12-10 15:56:39 +00:00
parent b02a9e3478
commit e125aace9b
3 changed files with 103 additions and 52 deletions

63
all.sas
View File

@@ -7742,22 +7742,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
@@ -7767,11 +7773,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*/;
@@ -7788,8 +7795,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;
@@ -7801,11 +7812,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( )));
@@ -7816,10 +7822,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( ));
@@ -7833,15 +7839,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;

View File

@@ -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;

View 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
)