Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_searchcols.sas
Go to the documentation of this file.
1/**
2 @file mp_searchcols.sas
3 @brief Searches all columns in a library
4 @details
5 Scans a set of libraries and creates a dataset containing all source tables
6 containing one or more of a particular set of columns
7
8 Usage:
9
10 %mp_searchcols(libs=sashelp work, cols=name sex age)
11
12 @param [in] libs= (SASHELP)
13 Space separated list of libraries to search for columns
14 @param [in] cols=
15 Space separated list of column names to search for (not case sensitive)
16 @param [out] outds= (mp_searchcols)
17 The table to create with the results. Will have one line per table match.
18 @param [in] match= (ANY) The match type. Valid values:
19 @li ANY - The table contains at least one of the columns
20 @li WILD - The table contains a column with a name that partially matches
21
22 @version 9.2
23 @author Allan Bowe
24**/
25
26%macro mp_searchcols(libs=sashelp
27 ,cols=
28 ,outds=mp_searchcols
29 ,match=ANY
30)/*/STORE SOURCE*/;
31
32%put &sysmacroname process began at %sysfunc(datetime(),datetime19.);
33
34/* get the list of tables in the library */
35proc sql;
36create table _data_ as
37 select distinct upcase(libname) as libname
38 , upcase(memname) as memname
39 , upcase(name) as name
40 from dictionary.columns
41%if %sysevalf(%superq(libs)=,boolean)=0 %then %do;
42 where upcase(libname) in ("IMPOSSIBLE",
43 %local x;
44 %do x=1 %to %sysfunc(countw(&libs));
45 "%upcase(%scan(&libs,&x))"
46 %end;
47 )
48%end;
49 order by 1,2,3;
50
51%local tempds;
52%let tempds=&syslast;
53data &outds;
54 set &tempds;
55 length cols matchcols $32767;
56 cols=upcase(symget('cols'));
57 colcount=countw(cols);
58 by libname memname name;
59 if _n_=1 then do;
60 putlog "Searching libs: &libs";
61 putlog "Searching cols: " cols;
62 end;
63 if first.memname then do;
64 sumcols=0;
65 retain matchcols;
66 matchcols='';
67 end;
68%if &match=ANY %then %do;
69 if findw(cols,name,,'spit') then do;
70 sumcols+1;
71 matchcols=cats(matchcols)!!' '!!cats(name);
72 end;
73%end;
74%else %if &match=WILD %then %do;
75 if _n_=1 then do;
76 retain wcount;
77 wcount=countw(cols);
78 drop wcount;
79 end;
80 do i=1 to wcount;
81 length curword $32;
82 curword=scan(cols,i,' ');
83 drop curword;
84 if index(name,cats(curword)) then do;
85 sumcols+1;
86 matchcols=cats(matchcols)!!' '!!cats(curword);
87 end;
88 end;
89%end;
90
91 if last.memname then do;
92 if sumcols>0 then output;
93 if sumcols=colcount then putlog "Full Match: " libname memname;
94 end;
95 keep libname memname sumcols matchcols;
96run;
97
98proc sort; by descending sumcols memname libname; run;
99
100proc sql;
101drop table &tempds;
102%put &sysmacroname process finished at %sysfunc(datetime(),datetime19.);
103
104%mend mp_searchcols;