To input multiple files , I need the stop statement in the data step when infile statement is used with the options memvar=mname to be listed one by one in a do loop as following.
data _null_;
length mname $ 32;
do mname = 'erase.dat', 'erase2.dat';
infile 'c:\mydir\tmp' memvar=mname end=done;
do while (^done);
input x;
put _all_;
end;
end;
stop;
run;
I need not a stop statement when the options memvar=memname to be set a blank as following
data _null_;
length memname $ 1024;
memname = ' ';
infile 'c:\mydir\tmp' memvar=memname end=done;
put memname=;
do while (^done);
input x;
put _all_;
end;
run;
Why?
Most data steps stop when they read past the end of the input (dataset or text file). Your first code would never read past the end since it just keeps alternating between reading the two members.
You also don't need the extra do loop in the second step.
data _null_;
length fname $ 1024;
infile 'c:\mydir\tmp\*' filename=fname ;
input ;
if fname ne lag(fname) then _n_= fname= _infile_;
run;
PS: Does your filesystem really support filenames of 1024 bytes? I thought the Windows limit was 256.
You took the code from https://communities.sas.com/t5/SAS-Programming/infile-statement-option-memvar/m-p/637655#M189545
What's the question?
Hi @Steve1964,
When the MEMVAR= variable (here: MEMNAME) is set to a missing value, SAS iterates through the files in folder c:\mydir\tmp and stops automatically after the last file has been processed. This is recognized when the value of MEMNAME (which is RETAINed by default) changes from the non-missing last file name to missing in the final iteration of the DATA step. You can see this if you insert another PUT statement before the assignment statement for MEMNAME:
put _n_= memname=;
With non-missing values for the MEMVAR= variable, however, the rule is: A new file is opened when the value of the MEMVAR= variable changes (see INFILE statement documentation). In your example this happens in each of the two iterations of the DO loop (i.e., MNAME changes from ' ' to 'erase.dat' in the first iteration and from 'erase.dat' to 'erase2.dat' in the second). Without the STOP statement this would happen again when the DATA step started its second iteration: MNAME would change from 'erase2.dat' to 'erase.dat' and the DO loop would execute again (and again and again ...).
Most data steps stop when they read past the end of the input (dataset or text file). Your first code would never read past the end since it just keeps alternating between reading the two members.
You also don't need the extra do loop in the second step.
data _null_;
length fname $ 1024;
infile 'c:\mydir\tmp\*' filename=fname ;
input ;
if fname ne lag(fname) then _n_= fname= _infile_;
run;
PS: Does your filesystem really support filenames of 1024 bytes? I thought the Windows limit was 256.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.