This macro adds the word 'text' to the beginning of every file that contains the extension of txt within the c:\temp\ directory.
To run on other operating systems just change the c:\temp\ in the macro invocation to the appropriate path.
To change what is written to the beginning of the file just replace 'text' in the following line of code: if _n_ = 1 then put 'text';
This macro retrieves each file from a directory that is passed to the macro. The file is opened and written to a new file called temp.txt with the new text added to the beginning of the file. The original file is then deleted and temp.txt and renamed back to the file name that was just deleted. This technique is needed because we cannot write to the beginning of an opened file and save it with the same name.
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.
%macro drive(dir,ext);
options noxwait;
%let filrf=mydir;
/* Assigns the fileref of mydir to the directory and opens the directory */
%let rc=%sysfunc(filename(filrf,&dir));
%let did=%sysfunc(dopen(&filrf));
/* Returns the number of members in the directory */
%let memcnt=%sysfunc(dnum(&did));
/* Loops through entire directory */
%do i = 1 %to &memcnt;
/* Returns the extension from each file */
%let name=%qscan(%qsysfunc(dread(&did,&i)),-1,.);
/* Checks to see if file contains an extension */
%if %qupcase(%qsysfunc(dread(&did,&i))) ne %qupcase(&name) %then %do;
/* Checks to see if the extension matches the parameter value */
/* If condition is true add text to the file */
%if (%superq(ext) ne and %qupcase(&name) = %qupcase(&ext)) or
(%superq(ext) = and %superq(name) ne) %then %do;
data _null_;
infile "&dir%qsysfunc(dread(&did,&i))";
input;
file "&dir.temp.txt";
if _n_ = 1 then put 'text';
put _infile_;
run;
/* Delete original file */
%sysexec del "&dir%qsysfunc(dread(&did,&i))";
/* Sleep to give file time to delete */
%put %sysfunc(sleep(1));
/* Rename temp file that contains new text to original file name */
data _null_;
x %unquote(%str(%') rename "&dir.temp.txt" %qsysfunc(dread(&did,&i))%str(%'));
run;
%end;
%end;
%end;
/* Closes the directory */
%let rc=%sysfunc(dclose(&did));
%mend drive;
/* Replace c:\temp\ with the directory containing the files for text to be added */
/* 2nd parameter is the extension of the files you want to add text too */
/* Leave 2nd parameter blank if all files are to be included */
%drive(c:\temp\,txt)
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: | 2012-02-15 10:51:02 |
Date Created: | 2012-02-15 08:59:28 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | Microsoft Windows 2000 Server | 9 TS M0 | |
Microsoft Windows 2000 Datacenter Server | 9 TS M0 | |||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9 TS M0 | |||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9 TS M0 | |||
Microsoft Windows 2000 Advanced Server | 9 TS M0 | |||
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9 TS M0 | |||
Microsoft Windows 2000 Professional | 9 TS M0 | |||
Microsoft Windows NT Workstation | 9 TS M0 | |||
Microsoft Windows Server 2003 Datacenter Edition | 9 TS M0 | |||
Microsoft Windows Server 2003 Enterprise Edition | 9 TS M0 | |||
Microsoft Windows Server 2003 Standard Edition | 9 TS M0 | |||
Microsoft Windows XP Professional | 9 TS M0 |