SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Junyong
Pyrite | Level 9

One can %INCLUDE a local SAS file as follows.

%include "a.sas";

If the SAS file is a URL, then it would be

filename a url "https://www.sas.com/a.sas";
%include a;

So it would be troublesome if I reuse the filename "a" somewhere inside the %INCLUDEd code. Is there any way to avoid the FILENAME a to avoid the reuse collision? If impossible, then is there any way to list all the used FILENAME to avoid such collision and manage multiple SAS files smartly?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you don't have any control of what the included code does then you will just need to use a pattern that tries to minimize the risk of re-use of the name.  For example if you are using a macro then you might use &SYSINDEX automatic macro variable.  It increases every time you call a macro.  

Or you could ask SAS to just make up a name.

%let fileref=;
%let rc=%sysfunc(filename(fileref,https://raw.githubusercontent.com/sasutils/macros/master/ds2post.sas,url));
%put &=rc &=fileref;
%include &fileref ;
filename &fileref;
%let fileref=;

View solution in original post

4 REPLIES 4
Reeza
Super User

Deassign the filename when you're done with it, then you can safely use it again later.

 

filename a;
Junyong
Pyrite | Level 9

Suppose I %INCLUDE a SAS file b.sas using a as follows.

filename a "!userprofile\desktop\b.sas";
%include a;

And suppose I try to reuse the a for another SAS file a.sas inside the b.sas as follows.

filename a;
filename a "!userprofile\desktop\a.sas";

The following errors are produced when I execute the first code.

ERROR: At least one file associated with fileref A is still in use.
ERROR: Error in the FILENAME statement.
ERROR: At least one file associated with fileref A is still in use.
ERROR: Error in the FILENAME statement.

So I need to remember that the a is now occupied by the first code to prevent any collision.

ballardw
Super User

If you are using a single Fileref, such as A, it is best practice to keep it pointing to same place.

 

You can check values of assigned filerefs in your session by checking the contents of SASHELP.Vextfl. The view has the fileref the external file path and other details.

 

The SAS function FILEREF verifies if a fileref has been assigned for the current SAS session.

Data result;
   assigned = fileref('Someref');
run;

If the value returned is > 0 then not assigned, if <0 then the fileref exists but the physical file associated does not, 0 if the fileref is assigned and the file exists.

Or the macro equivalent : %sysfunc(fileref(Someref))

Tom
Super User Tom
Super User

If you don't have any control of what the included code does then you will just need to use a pattern that tries to minimize the risk of re-use of the name.  For example if you are using a macro then you might use &SYSINDEX automatic macro variable.  It increases every time you call a macro.  

Or you could ask SAS to just make up a name.

%let fileref=;
%let rc=%sysfunc(filename(fileref,https://raw.githubusercontent.com/sasutils/macros/master/ds2post.sas,url));
%put &=rc &=fileref;
%include &fileref ;
filename &fileref;
%let fileref=;

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

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
  • 4 replies
  • 1196 views
  • 0 likes
  • 4 in conversation