Dynamically switch input files in a datastep.
Often files are organised into
individual files per week, month, year, or goegraphic location. Rather
than using multiple datasteps to read from multiple versions, it is
better to use a single datastep and switch to a new input file during
processing. This is possible using the FILEVAR and END options on the
INFILE statement.
FILEVAR allows you to specify a variable whose change in value
signifies that the current input file is closed, and a new one opened.
END allows you to specify a variable which will be set to TRUE when the end of the file is met.
The combination of these two variables allows us to detect when we get
to the end of the current input file, and switch to a new one. A few
things to be careful of here ;
- Make sure the variable used by FILEVAR is of sufficient length to hold the full filename.
- Appoint an initial filename for the first value.
- Make sure the END variable is tested upon every iteration.
An example is shown below ;
%Macro PayW(StartWk,EndWk);
filename infilly "LIVE.ABINITIO.FEED.YRWK&StartWk" disp=shr;
%let
Firstwk=%eval(&startwk+200000);
Data TestCall(Drop=infilly
weekptr) /
View=TestCall;
Length WEEK £ 6 Area 8 DEPT £ 3 infilly £ 80;
Retain WeekPtr
&startwk week
"&Firstwk"
infilly
"LIVE.ABINITIO.FEED.YRWK&startWk";
Infile infilly
filevar=infilly
End=EOF;
Input @1 Area 8.
@9 Dept £3.
@15 Sales pd5.;
If Area="XOM" Then Output;
If EOF And WeekPtr
< &EndWk Then
Do;
WeekPtr +
1;
If WeekPtr-(int(weekptr/100)*100)=53 Then WeekPtr + 48;
SubStr(Infilly,21,4)=Put(weekptr,z4.);
Week=Put(weekptr+200000,6.);
End;
Run;
Filename
infilly;
%Mend;
%PayW(&Stw,&ndW);
Here the macro defined has start and end week input parameters.
The initial filename is
assigned, and a macro variable defined which gives us the full date
(only part of the date is in the filename in this case).
The filename used on the FILEVAR option is called infilly. The initial value of this is set in a retain statement.
The END option is used against variable name EOF.
After we've used a subsetting
IF to reduce the output, we're testing for the end of file (when EOF
will be TRUE), and subsequently updating the pointers and part of the
filename which holds the week.