Is there a way to have tables created inside a macro be independent of tables outside of the macro?
So that tables outside of the macro that have the same name will not be overwritten?
Similar to how you can identify a macrovariable as %LOCAL.
Tables of the same name must be written to a different SAS library or the new table will overwrite the existing table. The tables written by SAS code generated from a macro are in no way associated with the macro itself.
Tables of the same name must be written to a different SAS library or the new table will overwrite the existing table. The tables written by SAS code generated from a macro are in no way associated with the macro itself.
While the advice you have gotten so far is all true, you can work around this with minimal change to your macros. Define a folder using the LIBNAME statement, such as:
libname my_temp 'path to some folder';
Then modify the macros slightly, adding a line at the beginning and a line at the end:
%macro mymac1;
options user=my_temp;
all of the macro language logic goes here, unchanged.
options user=work;
%mend mymac1;
When running the macro, all single-level data set names will be written to the MY_TEMP folder. Once the macro completes, SAS reverts to saving single level data set names in the WORK library.
When done, MY_TEMP does not get cleaned out. You would have to tend to that yourself.
Also note, if your macro reads from (rather than writes to) single-level names, it will attempt to read from MY_TEMP. If you want to read from a file in the WORK library, you would have to refer to it as work.data-set-name.
@Astounding wrote:
While the advice you have gotten so far is all true, you can work around this with minimal change to your macros. Define a folder using the LIBNAME statement, such as:
libname my_temp 'path to some folder';
Then modify the macros slightly, adding a line at the beginning and a line at the end:
%macro mymac1; options user=my_temp; all of the macro language logic goes here, unchanged. options user=work; %mend mymac1;
When running the macro, all single-level data set names will be written to the MY_TEMP folder. Once the macro completes, SAS reverts to saving single level data set names in the WORK library.
When done, MY_TEMP does not get cleaned out. You would have to tend to that yourself.
Also note, if your macro reads from (rather than writes to) single-level names, it will attempt to read from MY_TEMP. If you want to read from a file in the WORK library, you would have to refer to it as work.data-set-name.
This is a great tip, @Astounding . But how would you handle it if the macro has internal data sets, but the macro is also supposed to create output data sets in the WORK library?
If most of the one-level data set names should be discarded, but a few need to be saved in the WORK library, you can hard-code the two-part name:
data work.dsn5;
You can assign a macro variable name value like WORK.TMP<timestamp> to be used as temporary dataset name, with significant probability that is unique, and use it across the macro program.
@Astounding wrote:
When done, MY_TEMP does not get cleaned out. You would have to tend to that yourself.
@mcook: If you want to have SAS do the clean-up automatically (when closing the session), use a subfolder of the WORK library as the location of library MY_TEMP. See, e.g., this 2018 post by Chris Hemedinger himself for an elegant way to do this (including uniqueness of the subfolder names and automatic subfolder creation thanks to system option DLCREATEDIR).
Edit: An additional advantage of using a subfolder of the WORK library is that no name conflicts can occur even if two SAS sessions using your macro run in parallel on the same computer.
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.