1
0
mirror of https://github.com/sasjs/core.git synced 2026-01-05 00:20:05 +00:00

Compare commits

...

2 Commits

Author SHA1 Message Date
Allan Bowe
58784b2f28 Merge pull request #109 from sasjs/issue108
fix: rebuilding mp_searchdata into data step to avoid SQL warning.
2021-12-11 14:34:08 +00:00
munja
e125aace9b fix: rebuilding mp_searchdata into data step to avoid SQL warning. Added test and refreshed docs. Closes #108 2021-12-10 15:56:39 +00:00
3 changed files with 103 additions and 52 deletions

63
all.sas
View File

@@ -7742,22 +7742,28 @@ drop table &tempds;
Usage: Usage:
%mp_searchdata(lib=sashelp, string=Jan) %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. Outputs zero or more tables to an MPSEARCH library with specific records.
@param lib= the libref to search (should be already assigned) @param [in] lib= The libref to search (should be already assigned)
@param ds= the dataset to search (leave blank to search entire library) @param [in] ds= The dataset to search (leave blank to search entire library)
@param string= the string value to search @param [in] string= String value to search (case sensitive, can be partial)
@param numval= the numeric value to search (must be exact) @param [in] numval= Numeric value to search (must be exact)
@param outloc= the directory in which to create the output datasets with @param [out] outloc= (0) Optionally specify the directory in which to
matching rows. Will default to a subfolder in the WORK library. create the the output datasets with matching rows. By default it will
@param outobs= set to a positive integer to restrict the number of 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 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> <h4> SAS Macros </h4>
@li mf_getuniquename.sas
@li mf_getvarlist.sas @li mf_getvarlist.sas
@li mf_getvartype.sas @li mf_getvartype.sas
@li mf_mkdir.sas @li mf_mkdir.sas
@@ -7767,11 +7773,12 @@ drop table &tempds;
@author Allan Bowe @author Allan Bowe
**/ **/
%macro mp_searchdata(lib=sashelp %macro mp_searchdata(lib=
,ds= ,ds=
,string= /* the query will use a contains (?) operator */ ,string= /* the query will use a contains (?) operator */
,numval= /* numeric must match exactly */ ,numval= /* numeric must match exactly */
,outloc=%sysfunc(pathname(work))/mpsearch ,outloc=0
,outlib=MPSEARCH
,outobs=-1 ,outobs=-1
,filter_text=%str(1=1) ,filter_text=%str(1=1)
)/*/STORE SOURCE*/; )/*/STORE SOURCE*/;
@@ -7788,8 +7795,12 @@ drop table &tempds;
%if &string = %then %let type=N; %if &string = %then %let type=N;
%else %let type=C; %else %let type=C;
%if "&outloc"="0" %then %do;
%let outloc=%sysfunc(pathname(work))/%mf_getuniquename();
%end;
%mf_mkdir(&outloc) %mf_mkdir(&outloc)
libname mpsearch "&outloc"; libname &outlib "&outloc";
/* get the list of tables in the library */ /* get the list of tables in the library */
proc sql noprint; proc sql noprint;
@@ -7801,11 +7812,6 @@ select distinct memname into: table_list separated by ' '
%end; %end;
; ;
/* check that we have something to check */ /* 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!; %if %length(&table_list)=0 %then %put library &lib contains no tables!;
/* loop through each table */ /* loop through each table */
%else %do table_num=1 %to %sysfunc(countw(&table_list,%str( ))); %else %do table_num=1 %to %sysfunc(countw(&table_list,%str( )));
@@ -7816,10 +7822,10 @@ proc sql
%end; %end;
%else %do; %else %do;
%let check_tm=%sysfunc(datetime()); %let check_tm=%sysfunc(datetime());
/* build sql statement */ /* prep input */
create table mpsearch.&table as select * from &lib..&table data &outlib..&table;
where %unquote(&filter_text) and set &lib..&table;
(0 where %unquote(&filter_text) and ( 0
/* loop through columns */ /* loop through columns */
%do colnum=1 %to %sysfunc(countw(&vars,%str( ))); %do colnum=1 %to %sysfunc(countw(&vars,%str( )));
%let col=%scan(&vars,&colnum,%str( )); %let col=%scan(&vars,&colnum,%str( ));
@@ -7833,15 +7839,20 @@ proc sql
or ("&col"n = &numval) or ("&col"n = &numval)
%end; %end;
%end; %end;
); );
%if &outobs>-1 %then %do;
if _n_ > &outobs then stop;
%end;
run;
%put Search query for &table took %put Search query for &table took
%sysevalf(%sysfunc(datetime())-&check_tm) seconds; %sysevalf(%sysfunc(datetime())-&check_tm) seconds;
%if &sqlrc ne 0 %then %do; %if &syscc ne 0 %then %do;
%put %str(WAR)NING: SQLRC=&sqlrc when processing &table; %put %str(ERR)ROR: SYSCC=&syscc when processing &lib..&table;
%return; %return;
%end; %end;
%if %mf_nobs(mpsearch.&table)=0 %then %do; %if %mf_nobs(&outlib..&table)=0 %then %do;
drop table mpsearch.&table; proc sql;
drop table &outlib..&table;
%end; %end;
%end; %end;
%end; %end;

View File

@@ -11,22 +11,28 @@
Usage: Usage:
%mp_searchdata(lib=sashelp, string=Jan) %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. Outputs zero or more tables to an MPSEARCH library with specific records.
@param lib= the libref to search (should be already assigned) @param [in] lib= The libref to search (should be already assigned)
@param ds= the dataset to search (leave blank to search entire library) @param [in] ds= The dataset to search (leave blank to search entire library)
@param string= the string value to search @param [in] string= String value to search (case sensitive, can be partial)
@param numval= the numeric value to search (must be exact) @param [in] numval= Numeric value to search (must be exact)
@param outloc= the directory in which to create the output datasets with @param [out] outloc= (0) Optionally specify the directory in which to
matching rows. Will default to a subfolder in the WORK library. create the the output datasets with matching rows. By default it will
@param outobs= set to a positive integer to restrict the number of 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 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> <h4> SAS Macros </h4>
@li mf_getuniquename.sas
@li mf_getvarlist.sas @li mf_getvarlist.sas
@li mf_getvartype.sas @li mf_getvartype.sas
@li mf_mkdir.sas @li mf_mkdir.sas
@@ -36,11 +42,12 @@
@author Allan Bowe @author Allan Bowe
**/ **/
%macro mp_searchdata(lib=sashelp %macro mp_searchdata(lib=
,ds= ,ds=
,string= /* the query will use a contains (?) operator */ ,string= /* the query will use a contains (?) operator */
,numval= /* numeric must match exactly */ ,numval= /* numeric must match exactly */
,outloc=%sysfunc(pathname(work))/mpsearch ,outloc=0
,outlib=MPSEARCH
,outobs=-1 ,outobs=-1
,filter_text=%str(1=1) ,filter_text=%str(1=1)
)/*/STORE SOURCE*/; )/*/STORE SOURCE*/;
@@ -57,8 +64,12 @@
%if &string = %then %let type=N; %if &string = %then %let type=N;
%else %let type=C; %else %let type=C;
%if "&outloc"="0" %then %do;
%let outloc=%sysfunc(pathname(work))/%mf_getuniquename();
%end;
%mf_mkdir(&outloc) %mf_mkdir(&outloc)
libname mpsearch "&outloc"; libname &outlib "&outloc";
/* get the list of tables in the library */ /* get the list of tables in the library */
proc sql noprint; proc sql noprint;
@@ -70,11 +81,6 @@ select distinct memname into: table_list separated by ' '
%end; %end;
; ;
/* check that we have something to check */ /* 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!; %if %length(&table_list)=0 %then %put library &lib contains no tables!;
/* loop through each table */ /* loop through each table */
%else %do table_num=1 %to %sysfunc(countw(&table_list,%str( ))); %else %do table_num=1 %to %sysfunc(countw(&table_list,%str( )));
@@ -85,10 +91,10 @@ proc sql
%end; %end;
%else %do; %else %do;
%let check_tm=%sysfunc(datetime()); %let check_tm=%sysfunc(datetime());
/* build sql statement */ /* prep input */
create table mpsearch.&table as select * from &lib..&table data &outlib..&table;
where %unquote(&filter_text) and set &lib..&table;
(0 where %unquote(&filter_text) and ( 0
/* loop through columns */ /* loop through columns */
%do colnum=1 %to %sysfunc(countw(&vars,%str( ))); %do colnum=1 %to %sysfunc(countw(&vars,%str( )));
%let col=%scan(&vars,&colnum,%str( )); %let col=%scan(&vars,&colnum,%str( ));
@@ -102,15 +108,20 @@ proc sql
or ("&col"n = &numval) or ("&col"n = &numval)
%end; %end;
%end; %end;
); );
%if &outobs>-1 %then %do;
if _n_ > &outobs then stop;
%end;
run;
%put Search query for &table took %put Search query for &table took
%sysevalf(%sysfunc(datetime())-&check_tm) seconds; %sysevalf(%sysfunc(datetime())-&check_tm) seconds;
%if &sqlrc ne 0 %then %do; %if &syscc ne 0 %then %do;
%put %str(WAR)NING: SQLRC=&sqlrc when processing &table; %put %str(ERR)ROR: SYSCC=&syscc when processing &lib..&table;
%return; %return;
%end; %end;
%if %mf_nobs(mpsearch.&table)=0 %then %do; %if %mf_nobs(&outlib..&table)=0 %then %do;
drop table mpsearch.&table; proc sql;
drop table &outlib..&table;
%end; %end;
%end; %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
)