BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Steve1964
Obsidian | Level 7

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? 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

3 REPLIES 3
FreelanceReinh
Jade | Level 19

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 ...).

Tom
Super User Tom
Super User

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 638 views
  • 1 like
  • 4 in conversation