BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
js5
Pyrite | Level 9 js5
Pyrite | Level 9

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.

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

@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).

 

 

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
WarrenKuhfeld
Ammonite | Level 13

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.

mkeintz
PROC Star

@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).

 

 

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1150 views
  • 3 likes
  • 3 in conversation