Date: Tue, 17 Mar 1998 08:13:59 -0500
Reply-To: RHOADSM1 <RHOADSM1@WESTAT.COM>
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: RHOADSM1 <RHOADSM1@WESTAT.COM>
Subject: Re: Loop Macro
Content-Type: text/plain; charset=US-ASCII
Dave Boylan asks about a macro to read in a number of files that do
not have sequential names (see original posting at end):
The solution most in keeping with the spirit of the original macro
would be to add a NAMES= parameter to the READALL macro. For example,
%readall (names = Tom Dick Foo Bar)
Then use the %SCAN macro function to break the NAMES into words, and pass
each one successively to %ReadIt, which would have to be modified to
accept a filename rather than a month as its argument. If the extension
is not always TXT, you could make this part of what you pass to %readall
as well.
However, the macro is not really necessary in this situation. In
Windows at least, you can simply reference all the files in a single
FILENAME statement, and SAS will read them all in the same step. For
instance,
filename rev_recs ("Tom.txt", "Dick.txt", "Foo.txt", "Bar.txt");
One other tip: if you are going to use PROC APPEND, you don't
actually need to code a special case for the first data set. APPEND will
automatically create the BASE= data set if it doesn't already exist, so
you can use the same code for month 01 as for the remaining months.
Hope this helps!
Mike Rhoads
Westat
RhoadsM1@Westat.com
<<Dave Boylan asks>>
I've now mastered the macro technique to loop through a bunch of text
files (with incremental names) and read them into a SAS data set (e.g.,
"file01.txt", "file02.txt",..., "file09.txt"). Now, how would I modify
the macro below (or write a new macro) to read in a number of files that
do not have sequential names (or no naming convention at all; e.g.
"Tom.txt", "Dick.txt", "Foo.txt", "Bar.txt") Any help is appreciated.
Thanks!
*============================================
%macro readall ;
%do month = 1 %to 12 ;
%if (&month le 9) %then %do;
%let month=0&month;
%end;
%ReadIt(Mo=&month,Yr=&year);
%end; *end month loop;
%mend readall;
%macro ReadIt(Mo=);
filename rev_recs "f:\png\PNG&Mo.97.BIN";
Data dmonth;
infile rev_recs lrecl=462 recfm=f firstobs=2;* obs=1000;
Input
@1 yrmo s370fpd3.
@4 CustNo $Ebcdic9.
@14 servCd $Ebcdic1.;
run;
%if (&Mo eq 01) %then %do;
data work.wpeall;
set dmonth;
%end;
%else %do;
Proc Append base=wpeall data=dmonth;
%end;
run;
%mend ReadIt;
%readall;
*============================================
<<end original posting>>