The macro contained within this sample requires three arguments:
This sample opens a directory and searches all SAS data sets for a particular character string. If the string is found, a data set (supplied as the third parameter to the macro) is created. The names of all the SAS data sets in which the string was found are placed in a variable called DATASETNAME.
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.
/* To run this sample see instructions for the macro call near the bottom of this program */
%macro drive(dir,string,dsn);
%local filrf librf lib rc did memcnt cnt i varlist lname fname dsid typ;
data &dsn;
stop;
run;
/* Creates a fileref */
%let filrf=mydir;
/* Creates a libref */
%let librf=temp;
/* Assigns the libref of temp to the directory passed in */
%let lib=%sysfunc(libname(&librf,&dir));
/* Assigns the fileref of mydir to the directory passed in */
%let rc=%sysfunc(filename(filrf,&dir));
/* Opens the directory to be read */
%let did=%sysfunc(dopen(&filrf));
/* Returns the number of members in the directory passed in */
%let memcnt=%sysfunc(dnum(&did));
%let cnt=0;
%do i = 1 %to &memcnt;
%let varlist=;
/* Return the extension of the dataset found */
%let lname=%qupcase(%qscan(%sysfunc(dread(&did,&i)),2,.));
/* Return the first name of the dataset found */
%let fname=%qscan(%qsysfunc(dread(&did,&i)),1,.);
/* Check to see if file has a dataset extension of SAS7BDAT */
/* SAS7BDAT may need to be changed based on the version of */
/* the SAS data set you are looking for. */
%if %upcase(&lname) = SAS7BDAT %then %do;
%let dsid=%sysfunc(open(&librf..&fname));
/* The variable CNT will contain the number of variables that are in the */
/* dataset that is passed in. */
%let cnt=%sysfunc(attrn(&dsid,nvars));
/* Create a macro variable that contains all dataset character variables */
%do ii = 1 %to &cnt;
%let typ=%sysfunc(vartype(&dsid,&ii));
/* Looking for only character variables */
%if &typ = C %then %do;
%let varlist=&varlist %sysfunc(varname(&dsid,&ii));
%end;
%let total=%sysfunc(countw(%bquote(&varlist)));
%end;
/* Close the data set */
%let rc=%sysfunc(close(&dsid));
/* If the string was found run this step */
%if %superq(varlist) ne %then %do;
data final(keep=datasetname);
length datasetname $ 30;
set &librf..&fname;
%do j=1 %to &total;
if %scan(&varlist,&j) = "&string" then do;
datasetname="&fname";
output;
stop;
end;
%end;
run;
data &dsn;
length datasetname $ 30;
set &dsn final;
run;
%end;
%end;
%end;
/* Close the directory */
%let rc=%sysfunc(dclose(&did));
%mend drive;
/* Replace c:\test below with the directory containing the SAS data sets */
/* Second argument is the string to search for */
/* Third argument names the output data set to be created */
%drive(c:\test,string,final2)
proc print data=final2;
run;
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.
Type: | Sample |
Date Modified: | 2013-09-06 16:51:59 |
Date Created: | 2013-08-20 11:22:36 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | Aster Data nCluster on Linux x64 | ||
DB2 Universal Database on AIX | ||||
DB2 Universal Database on Linux x64 | ||||
Greenplum on Linux x64 | ||||
Netezza TwinFin 32bit blade | ||||
Netezza TwinFin 32-bit SMP Hosts | ||||
Netezza TwinFin 64-bit S-Blades | ||||
Netezza TwinFin 64-bit SMP Hosts | ||||
Teradata on Linux | ||||
z/OS | ||||
Z64 | ||||
OpenVMS VAX | ||||
Microsoft® Windows® for 64-Bit Itanium-based Systems | ||||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | ||||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | ||||
Microsoft Windows XP 64-bit Edition | ||||
Microsoft® Windows® for x64 | ||||
OS/2 | ||||
Microsoft Windows 8 Enterprise 32-bit | ||||
Microsoft Windows 8 Enterprise x64 | ||||
Microsoft Windows 8 Pro 32-bit | ||||
Microsoft Windows 8 Pro x64 | ||||
Microsoft Windows 95/98 | ||||
Microsoft Windows 2000 Advanced Server | ||||
Microsoft Windows 2000 Datacenter Server | ||||
Microsoft Windows 2000 Server | ||||
Microsoft Windows 2000 Professional | ||||
Microsoft Windows NT Workstation | ||||
Microsoft Windows Server 2003 Datacenter Edition | ||||
Microsoft Windows Server 2003 Enterprise Edition | ||||
Microsoft Windows Server 2003 Standard Edition | ||||
Microsoft Windows Server 2003 for x64 | ||||
Microsoft Windows Server 2008 | ||||
Microsoft Windows Server 2008 R2 | ||||
Microsoft Windows Server 2008 for x64 | ||||
Microsoft Windows Server 2012 Datacenter | ||||
Microsoft Windows Server 2012 Std | ||||
Microsoft Windows XP Professional | ||||
Windows 7 Enterprise 32 bit | ||||
Windows 7 Enterprise x64 | ||||
Windows 7 Home Premium 32 bit | ||||
Windows 7 Home Premium x64 | ||||
Windows 7 Professional 32 bit | ||||
Windows 7 Professional x64 | ||||
Windows 7 Ultimate 32 bit | ||||
Windows 7 Ultimate x64 | ||||
Windows Millennium Edition (Me) | ||||
Windows Vista | ||||
Windows Vista for x64 | ||||
64-bit Enabled AIX | ||||
64-bit Enabled HP-UX | ||||
64-bit Enabled Solaris | ||||
ABI+ for Intel Architecture | ||||
AIX | ||||
HP-UX | ||||
HP-UX IPF | ||||
IRIX | ||||
Linux | ||||
Linux for x64 | ||||
Linux on Itanium | ||||
OpenVMS Alpha | ||||
OpenVMS on HP Integrity | ||||
Solaris | ||||
Solaris for x64 | ||||
Tru64 UNIX |