Hello,
I am having issues getting the filename function and infile statement to work nicely within one data step. The following code results in an error:
ERROR: No logical assign for filename PWFILE.
rc = filename("pwfile", catx("\", pathname("path"), "Password.txt"));
infile pwfile;
input password;
rc = filename("pwfile");
It is as if there is a timing issue with the fileref not being defined when infile wants to use it. I was able to work it around as follows:
password_file = catx("\", pathname("path"), "Password.txt");
infile dummy filevar=password_file;
input password;
It feels strange to use filevar to read only one file though. Is this behaviour expected? I cannot use filename statement beforehand because the code looks through the folder tree and only defines the fileref when it finds the latest folder. Thank you.
@js5 :
You are essentially correct.
The "rc=filename(....)" statement is an executable statement - i.e. it won't be honored until the data step begins data processing. After all, like all functions, it could be a result of an "IF ... THEN rc=filename(...)" statement, which means it can only be executed during actual data processin.
But the INFILE statement in your initial code is effectively a compile-time statement that must be established prior to execution.
Using the "filevar=" option tells SAS that it must be prepared to dynamically assign a physical file to a fileref (even if it is just a single unchanging assignment).
Look at FILEVAR=.
https://v8doc.sas.com/sashtml/lgref/z0146932.htm#z0166950
I know this is an old link, but it is the one I could easily find.
@js5 :
You are essentially correct.
The "rc=filename(....)" statement is an executable statement - i.e. it won't be honored until the data step begins data processing. After all, like all functions, it could be a result of an "IF ... THEN rc=filename(...)" statement, which means it can only be executed during actual data processin.
But the INFILE statement in your initial code is effectively a compile-time statement that must be established prior to execution.
Using the "filevar=" option tells SAS that it must be prepared to dynamically assign a physical file to a fileref (even if it is just a single unchanging assignment).
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.