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
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.
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
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.
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.
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
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
Thank you @Mazi for the example.
- Dr. Abhijeet Safai
@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;
Thanks you @Tom for sharing one more new method. 🙂
- Dr. Abhijeet Safai
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
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.