BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
DrAbhijeetSafai
Lapis Lazuli | Level 10

I am trying to understand how filevar= option is used in infile statement. I am able to understand that a different file each time (varying file, filevar) will be given to read and it is read sequentially. But I am not able to understand the exact syntax. I am sharing the syntax below which I am trying. 

 

filename qtr1 "path";
filename qtr2 "path";
filename qtr3 "path";

data new;
infile temp filevar = qtr1;
input A B C D;
run;

A B C D are four columns in .dat file qtr1, qtr2 and qtr3 and they are getting read properly without filevar= option. But when I am using filevar=option, the error is shown that "Invalid physical name." 

 

I request to kindly guide me about this. Thanks in advance.

 

- Dr. Abhijeet Safai

 

 

Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Your argument to the FILEVAR option is qtr1.

FILEVAR (filename variable) defines a variable (not a value!), and this variable has to contain a valid path to a file. Since you do not set qtr1 to such a value in your code, the step fails.

 

A file reference (what you set with FILENAME) and a data step variable are two completely different things.

View solution in original post

10 REPLIES 10
Patrick
Opal | Level 21
Consult the docu for the Infile statement. It’s explained in detail with sample code.
DrAbhijeetSafai
Lapis Lazuli | Level 10

Hi @Patrick . Thanks for your response. I am looking at the documentation and at other places on internet for quite a long time. Maybe more than 3 hours. But still I am not able to find where I am making a mistake. I request you to kindly copy paste relevant example program here if possible so I can try it. I am trying many things but not able to understand how this option is working. 

 

Thank you.

 

- Dr. Abhijeet Safai

Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real
Patrick
Opal | Level 21

I can't explain this better than the docu section Reading from Multiple External Files in Sequential Order

Adopting the docu sample code to what you shared:

/* sample 1 */
data new;
  infile datalines truncover;
  input qtr1 $500.;

  infile dummy filevar = qtr1 end=done;
  do while(not done);
    input A B C D;
  end;

datalines;
<path1>
<path2>
<path3>
;
run;

/* sample 2 */
data paths;
  infile datalines truncover;
  input qtr1 $500.;
datalines;
<path1>
<path2>
<path3>
;
run;

data new;
  set paths;
  infile dummy filevar = qtr1 end=done;
  do while(not done);
    input A B C D;
  end;
run;

If the docu explanation doesn't work for you then another option is to copy/paste above sample 1 code into chatGPT. Just tried and the explanation given there is also not too bad.

Kurt_Bremser
Super User

Your argument to the FILEVAR option is qtr1.

FILEVAR (filename variable) defines a variable (not a value!), and this variable has to contain a valid path to a file. Since you do not set qtr1 to such a value in your code, the step fails.

 

A file reference (what you set with FILENAME) and a data step variable are two completely different things.

DrAbhijeetSafai
Lapis Lazuli | Level 10

Many thanks @Kurt_Bremser for pointing out the mistake that it should be a data step variable and not fileref as I was considering in the filename statement. 

 

I am sharing the correct programme below.

 

data new;

     do qtr = 1 to 3 by 1;

          nextfile = "path till qtr but not 1, 2 or 3"||put(qtr,1.)".dat";

          do until (eof);
               infile temp filevar = nextfile end=eof;
               input A B C D;

               output;

          end;

     end;
run;

 

There are 3 files at the location of path as - qtr1.dat, qtr2.dat and qtr3.dat which gets referred and are read sequentially because of the above programme. 

 

An online reference gave me idea that two do loops will be required and my colleague Sushil helped me in this.  Thanks @Kurt_Bremser  once again for finding out the exact mistake I was doing (misunderstanding about fileref and a variable in data step) which solved the problem. 

 

- Dr. Abhijeet Safai

Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real
Mazi
Pyrite | Level 9
data _null_;
	infile datalines truncover;
	input path $200.;
	do until(end);
		infile temp filevar=path end=end;
		input;
		put _infile_;
	end;
datalines;
/home/sassrv/global/templates/envsetup.sas
/home/sassrv/global/templates/ibnkstmt.sas
;
run;

Here is an example of me reading two sas programs, and printing the contents to the log.
Hope that helps

DrAbhijeetSafai
Lapis Lazuli | Level 10

Thank you @Mazi for the example.

 

- Dr. Abhijeet Safai

Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real
Tom
Super User Tom
Super User

@DrAbhijeetSafai wrote:

I am trying to understand how filevar= option is used in infile statement. I am able to understand that a different file each time (varying file, filevar) will be given to read and it is read sequentially. But I am not able to understand the exact syntax. I am sharing the syntax below which I am trying. 

 

filename qtr1 "path";
filename qtr2 "path";
filename qtr3 "path";

data new;
infile temp filevar = qtr1;
input A B C D;
run;

A B C D are four columns in .dat file qtr1, qtr2 and qtr3 and they are getting read properly without filevar= option. But when I am using filevar=option, the error is shown that "Invalid physical name." 

 

I request to kindly guide me about this. Thanks in advance.

 

- Dr. Abhijeet Safai

 

 


If you want to read three files as one then just change your FILENAME statement.

filename qtr ("path1","path2","path3");

data new;
  infile qtr ;
  input A B C D;
run;

If you want to use FILEVAR then you want something like this:

data new;
  length qtr $200;
  do qtr="path1","path2","path3":
    infile temp filevar = qtr1 end=eof;
    do while (not eof);
      input A B C D;
      output;
    end;
  end;
run;
DrAbhijeetSafai
Lapis Lazuli | Level 10

Thanks you @Tom  for sharing one more new method. 🙂

 

- Dr. Abhijeet Safai

Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real
DrAbhijeetSafai
Lapis Lazuli | Level 10

filename qtr ("path1","path2","path3");

 Just wanted to add here that the comma after "path1" and "path2" are optional and it works even if these commas are not used and only space is given. 

 

Thank you.

 

- Dr. Abhijeet Safai 

Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 10 replies
  • 1425 views
  • 6 likes
  • 5 in conversation