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

Hi everyone,

 

I´m having trouble reading the same file twice inside a datastep.

 

data WORK.OUT;
	infile datalines;
	length INP $40;
	input INP $;
	infile dummy filevar=INP end=EOF;
	do while(^EOF);
	  input VAR1 VAR2 VAR3;
	  output;
	end;
datalines;
/path/to/file/file1.txt
/path/to/file/file2.txt
/path/to/file/file2.txt
/path/to/file/file3.txt
;

Each file is read and output once even though file2.txt should have been output twice.

 

The code is just exemplary. The real code depends on a list of items in a dataset for which (depending on a parameter) different external files should be read. If the same file occurs two times in a row it is not read again.

If files alternate like below, they are read every time:

/path/to/file/file1.txt

/path/to/file/file2.txt

/path/to/file/file3.txt

/path/to/file/file2.txt

What am I missing? Do I need to reset a pointer or the infile? Or is this not intended?

Any help is appreciated!

 

/edit: formatting...

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Yes. You need to run the INFILE statement twice to "clear" it. So point it to some other file between each real file in your list.  Try using the bit bucket on Unix.

 

Something like this:

data WORK.OUT;
  infile datalines;
  length INP filevar $40;
  input INP $;
  do filevar=inp,'/dev/null';
    infile dummy filevar=filevar end=EOF;
    do while(^EOF);
      input VAR1 VAR2 VAR3;
      output;
    end;
  end;
datalines;
/path/to/file/file1.txt
/path/to/file/file2.txt
/path/to/file/file2.txt
/path/to/file/file3.txt
;

 

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

Yes. You need to run the INFILE statement twice to "clear" it. So point it to some other file between each real file in your list.  Try using the bit bucket on Unix.

 

Something like this:

data WORK.OUT;
  infile datalines;
  length INP filevar $40;
  input INP $;
  do filevar=inp,'/dev/null';
    infile dummy filevar=filevar end=EOF;
    do while(^EOF);
      input VAR1 VAR2 VAR3;
      output;
    end;
  end;
datalines;
/path/to/file/file1.txt
/path/to/file/file2.txt
/path/to/file/file2.txt
/path/to/file/file3.txt
;

 

yabwon
Onyx | Level 15

If I may, I think the TEMP filename would be a bit more OS independent.

 

All the best

Bart

 

filename f TEMP;
filename f LIST;
data _null_; /* force to create file */
  file f ;
  put    ;
  stop   ;
run;

data WORK.OUT;
	infile datalines;
	length INP $ 100.;
	input INP $ ; 

  infile dummy filevar=INP end=EOF;
  do while(^EOF);
    input VAR1 VAR2 VAR3;
    output;
  end;
  INP = pathname('f');
  infile dummy filevar=INP end=EOF;
  
datalines;
E:/SAS_WORK_5400/file1.txt
E:/SAS_WORK_5400/file2.txt
E:/SAS_WORK_5400/file2.txt
E:/SAS_WORK_5400/file3.txt
;
run;

filename f CLEAR;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

Using TEMP to allocate a name is a good idea.

It is interesting that it works using two different INFILE statements.  Note that they both have to use the same variable for the FILEVAR= option and they also have to both use the same filref (DUMMY in the example).  Otherwise the second one does not cause the first to forget the filename it was reading before.

data_null__
Jade | Level 19

From the documentation 

 

FILEVAR=variable

specifies a variable whose change in value causes the INFILE statement to close the current input file and open a new one. When the next INPUT statement executes, it reads from the new file that the FILEVAR= variable specifies. Like automatic variables, this variable is not written to the data set.

 

Why do you want to read the file twice?

 

Maze
Fluorite | Level 6

Thanks everyone for the pointers, doc is pretty clear, I don´t know why I did not see that. Smiley Mad

 

Why do you want to read the file twice?

I have a dataset with table names that include a structure tag. Based on that tag I read the according structure file to generate a DDL script (structures may repeat!).

 

 

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
  • 5 replies
  • 1420 views
  • 4 likes
  • 4 in conversation