Use SAS to find a specific string in any character variable in a SAS data library.
The macro code on the Full Code tab has the basic function of the grep command in UNIX operating environments, but works on a SAS data library in Windows operating environments.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
The %GREP macro takes two arguments, the libref for the SAS data library and the search string. If the search string contains special macro characters, use the %NRSTR function to mask them.
The output is in the SAS Log shown on the Output tab.
options nomprint nomlogic nosymbolgen nonotes;
/* Create test data sets */
libname test 'c:\';
data test.one;
x=1;
y='hi';
z='***find me***';
run;
data test.two;
x=5;
a='***find me***';
run;
data test.three;
x=5;
run;
/* Find data sets names and store in a macro variable */
%macro grep(librf,string); /* parameters are unquoted, libref name, search string */
%let librf = %upcase(&librf);
proc sql noprint;
select left(put(count(*),8.)) into :numds
from dictionary.tables
where libname="&librf";
select memname into :ds1 - :ds&numds
from dictionary.tables
where libname="&librf";
%do i=1 %to &numds;
proc sql noprint;
select left(put(count(*),8.)) into :numvars
from dictionary.columns
where libname="&librf" and memname="&&ds&i" and type='char';
/* Create list of variable names and store in a macro variable */
%if &numvars > 0 %then %do;
select name into :var1 - :var&numvars
from dictionary.columns
where libname="&librf" and memname="&&ds&i" and type='char';
quit;
data _null_;
set &librf..&&ds&i;
%do j=1 %to &numvars;
if &&var&j = "&string" then
put "String &string found in dataset &librf..&&ds&i for variable &&var&j";
%end;
run;
%end;
%end;
%mend;
%grep(test,***find me***);
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
1 2 options nomprint nomlogic nosymbolgen nonotes; 3 /* create test data sets */ 4 5 libname test 'c:\'; 6 data test.one; 7 x=1; 8 y='hi'; 9 z='***find me***'; 10 run; 11 12 data test.two; 13 x=5; 14 a='***find me***'; 15 run; 16 17 data test.three; 18 x=5; 19 run; 20 21 /* find data sets names and store in a macro variable */ 22 23 %macro grep(librf,string); /* parameters are unquoted, libref name, search string */ 24 %let librf = %upcase(&librf); 25 proc sql noprint; 26 select left(put(count(*),8.)) into :numds 27 from dictionary.tables 28 where libname="&librf"; 29 30 select memname into :ds1 - :ds&numds 31 from dictionary.tables 32 where libname="&librf"; 33 34 %do i=1 %to &numds; 35 proc sql noprint; 36 select left(put(count(*),8.)) into :numvars 37 from dictionary.columns 38 where libname="&librf" and memname="&&ds&i" and type='char'; 39 40 /* create list of variable names and store in a macro variable */ 41 42 %if &numvars > 0 %then %do; 43 select name into :var1 - :var&numvars 44 from dictionary.columns 45 where libname="&librf" and memname="&&ds&i" and type='char'; 46 quit; 47 48 data _null_; 49 set test.&&ds&i; 50 %do j=1 %to &numvars; 51 if &&var&j = "&string" then 52 put "String &string found in dataset &librf..&&ds&i for variable &&var&j" 52 ! ; 53 %end; 54 run; 55 %end; 56 %end; 57 %mend; 58 %grep(test,***find me***); String ***find me*** found in dataset TEST.ONE for variable z String ***find me*** found in dataset TEST.TWO for variable a
Type: | Sample |
Topic: | Common Programming Tasks ==> Reusing Code Common Programming Tasks ==> Working with Character Data SAS Reference ==> Macro SAS Reference ==> SQL |
Date Modified: | 2012-02-15 10:58:03 |
Date Created: | 2008-08-27 12:00:53 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | N/A | z/OS | 9.2 TS2M3 | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.2 TS2M3 | |||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.2 TS2M3 | |||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.2 TS2M3 | |||
Microsoft Windows XP 64-bit Edition | 9.2 TS2M3 | |||
Microsoft® Windows® for x64 | 9.2 TS2M3 | |||
Microsoft Windows Server 2003 Datacenter Edition | 9.2 TS2M3 | |||
Microsoft Windows Server 2003 Enterprise Edition | 9.2 TS2M3 | |||
Microsoft Windows Server 2003 Standard Edition | 9.2 TS2M3 | |||
Microsoft Windows Server 2003 for x64 | 9.2 TS2M3 | |||
Microsoft Windows Server 2008 | 9.2 TS2M3 | |||
Microsoft Windows Server 2008 for x64 | 9.2 TS2M3 | |||
Microsoft Windows XP Professional | 9.2 TS2M3 | |||
Windows 7 Enterprise 32 bit | 9.2 TS2M3 | |||
Windows 7 Enterprise x64 | 9.2 TS2M3 | |||
Windows 7 Home Premium 32 bit | 9.2 TS2M3 | |||
Windows 7 Home Premium x64 | 9.2 TS2M3 | |||
Windows 7 Professional 32 bit | 9.2 TS2M3 | |||
Windows 7 Professional x64 | 9.2 TS2M3 | |||
Windows 7 Ultimate 32 bit | 9.2 TS2M3 | |||
Windows 7 Ultimate x64 | 9.2 TS2M3 | |||
Windows Vista | 9.2 TS2M3 | |||
Windows Vista for x64 | 9.2 TS2M3 | |||
64-bit Enabled AIX | 9.2 TS2M3 | |||
64-bit Enabled HP-UX | 9.2 TS2M3 | |||
64-bit Enabled Solaris | 9.2 TS2M3 | |||
HP-UX | 9.2 TS2M3 | |||
HP-UX IPF | 9.2 TS2M3 | |||
Linux | 9.2 TS2M3 | |||
Linux for x64 | 9.2 TS2M3 | |||
Linux on Itanium | 9.2 TS2M3 | |||
OpenVMS on HP Integrity | 9.2 TS2M3 | |||
Solaris for x64 | 9.2 TS2M3 | |||
Tru64 UNIX | 9.2 TS2M3 |