- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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=;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Deassign the filename when you're done with it, then you can safely use it again later.
filename a;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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))
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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=;