Date: Tue, 17 Mar 1998 10:43:44 +0200
Reply-To: Martin Trollope <martint@HOLLARD.CO.ZA>
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: Martin Trollope <martint@HOLLARD.CO.ZA>
Subject: Re: Loop Macro
Content-Type: text/plain; charset="us-ascii"
Hi Dave,
There are an enormous number of potential solutions to this one, but the following would be my choice:
Step One:
Read a list of the files from a cards statement and put the name of the files into sequentially numbered macro variables e.g. &file1 = tom.txt, &file2 = dick.txt, etc.
At the end of the list, put the file count into a macro variable e.g. &nfiles = 4.
N.B. All the 'compress' and 'trim' functions serve to rid the resulting macro variable of leading and/or trailing spaces, which can cause irritating and hard to find errors to occur.
data _null_;
infile cards eof=end;
input dsname $20.;
call symput(compress('file'!!put(_n_,8.)),trim(dsname));
return;
end:
call symput('nfiles',compress(put(_n_,8.)));
return;
cards;
tom.txt
dick.txt
foo.txt
bar.txt
;
run;
Step Two:
Process the files one after the other by looping from 1 to the number of files.
&&file&fnum is resolved twice. First resolution gives &file1. Second resolution gives tom.txt .
%macro readall ;
%do fnum = 1 %to &nfiles ;
%ReadIt(file = &&file&fnum);
%end;
%mend readall;
%macro ReadIt(file=);
filename inf "r:\somewhere\&file";
other code to suit your needs;
%mend readit;
Martin Trollope
----------
From: Boylan, Dave[SMTP:dboylan@UTILICORP.COM]
Reply To: Boylan, Dave
Sent: 17 March 1998 07:53
To: SAS-L@AKH-WIEN.AC.AT
Subject: Loop Macro
SAS-L People,
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;
*============================================
--------------------------------------
David Boylan
Corporate Forecasting
UtiliCorp United, Inc.
816.467.3062
--------------------------------------