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).
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.