BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mcook
Quartz | Level 8

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.

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
SASJedi
SAS Super FREQ

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. 

Check out my Jedi SAS Tricks for SAS Users

View solution in original post

7 REPLIES 7
SASJedi
SAS Super FREQ

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. 

Check out my Jedi SAS Tricks for SAS Users
Reeza
Super User
Unfortunately not. What I do, is use _ in front of my table names and temp tables within my macros. Then I drop all temp tables at the end of each macro execution explicitly (proc datasets) to avoid any issues with tables existing in a new run and save the output to a unique name or append it to a data set as required.
Astounding
PROC Star

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.

PaigeMiller
Diamond | Level 26

@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?

--
Paige Miller
Astounding
PROC Star

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;
Shmuel
Garnet | Level 18

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.

FreelanceReinh
Jade | Level 19

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1299 views
  • 9 likes
  • 7 in conversation