I have a macro for which I would like to use an alternative "work" library, to avoid any potential for collisions with datasets not associated with the macro. There could even be multiple instances of the macro running simultaneously within the same SAS session, or in separate sessions.
Currently this is the solution I'm considering:
libname macwork 'c:\temp\randomletters';
The downside is I need to manage the directory c:\temp\randomletters'. I know this can be done in SAS by creating a random string, then creating the directory, then mounting the library, and (hopefully) deleting the directory afterwards, however this seems like reinventing the wheel. The normal WORK library already handles all of this: &address always has an element of randomness:
%let address = %sysfunc(pathname(WORK));
%put &address;
Is it possible to establish additional "work" libraries in the same SAS session which would have unique names?
I think this is a portable approach that works in all of the suggestions here.
options dlcreatedir;
libname work1 "%sysfunc(getoption(WORK))/%sysfunc(uuidgen())" access=temp;
libname work2 "%sysfunc(getoption(WORK))/%sysfunc(uuidgen())" access=temp;
libname work3 "%sysfunc(getoption(WORK))/%sysfunc(uuidgen())" access=temp;
libname work4 "%sysfunc(getoption(WORK))/%sysfunc(uuidgen())" access=temp;
Chris
Not sure about work libraries, but if you assign a library with the name user that becomes the default. You still have the issue of managing the space and ensuring it's purged after.
@desertsp wrote:
I have a macro for which I would like to use an alternative "work" library, to avoid any potential for collisions with datasets not associated with the macro. There could even be multiple instances of the macro running simultaneously (i.e. in multiple SAS sessions) on the same computer.
Currently this is the solution I'm considering:
libname macwork 'c:\temp\randomletters';
The downside is I need to manage the directory c:\temp\randomletters'. I know this can be done in SAS by creating a random string, then creating the directory, then mounting the library, and (hopefully) deleting the directory afterwards, however this seems like reinventing the wheel. The normal WORK library already handles all of this: &address always has an element of randomness:
%let address = %sysfunc(pathname(WORK)); %put &address;
Is it possible to establish additional "work" libraries in the same SAS session which would have unique names?
@desertsp wrote:
I have a macro for which I would like to use an alternative "work" library, to avoid any potential for collisions with datasets not associated with the macro. There could even be multiple instances of the macro running simultaneously within the same SAS session, or in separate sessions.
A serious concern but the best solution is to control which library anything gets written to. The only data sets I have in my WORK library are ones that for most purposes exist for one or two steps and I don't care about them. If you have multiple instances then the only way you are going to control that is to specify a library. A routine and repetitive task is what computers are designed for.
If you use the system option DLCREATEDIR then you don't need to explicitly create the directory in your libname statement as that option allows a libname statement to create the needed directory.
If you name all of these "temporary" libraries with a consistent name it would not be hard to write a clean up program to clean them up and %include the code to call that cleanup as needed, or use an autocall macro.
By using subfolders of the WORK directory you can avoid conflicts between separate SAS sessions and let SAS do the clean-up afterwards automatically.
Only issue is how to generate unique names that don't collide as well.
@FreelanceReinh wrote:
By using subfolders of the WORK directory you can avoid conflicts between separate SAS sessions and let SAS do the clean-up afterwards automatically.
This should be manageable by the random letter approach @desertsp already mentioned (perhaps using the UUIDGEN function). A datetime stamp in the folder name might work as well, unless the time between two DATETIME() calls becomes too short.
I think this is a portable approach that works in all of the suggestions here.
options dlcreatedir;
libname work1 "%sysfunc(getoption(WORK))/%sysfunc(uuidgen())" access=temp;
libname work2 "%sysfunc(getoption(WORK))/%sysfunc(uuidgen())" access=temp;
libname work3 "%sysfunc(getoption(WORK))/%sysfunc(uuidgen())" access=temp;
libname work4 "%sysfunc(getoption(WORK))/%sysfunc(uuidgen())" access=temp;
Chris
Thanks Chris. That is exactly what I needed (so were the other posts...but you pulled it all together for me).
@ChrisHemedinger wrote:
I think this is a portable approach that works in all of the suggestions here.
options dlcreatedir; libname work1 "%sysfunc(getoption(WORK))/%sysfunc(uuidgen())" access=temp; libname work2 "%sysfunc(getoption(WORK))/%sysfunc(uuidgen())" access=temp; libname work3 "%sysfunc(getoption(WORK))/%sysfunc(uuidgen())" access=temp; libname work4 "%sysfunc(getoption(WORK))/%sysfunc(uuidgen())" access=temp;
Chris
Then as I understand things, you would still have to clean up this folder, otherwise the data sets in work1 (and work2 ...) remain on the hard disk.
@PaigeMiller wrote:
Then as I understand things, you would still have to clean up this folder, otherwise the data sets in work1 (and work2 ...) remain on the hard disk.
Not on my system (unless I set the NOWORKTERM option), SAS 9.4 under Windows 7.
On my system (Windows 10), the files remain on the hard disk, taking up space, until I close SAS. I guess that's why I was considering deleting them when no longer needed, in case I have a very long program with large datasets, it seems like deleting them when no longer needed is a prudent idea.
Ah okay, you meant those work files pertaining to a particular macro call should be deleted when the program has finished using them. Good point. Sorry, I was thinking only of the clean-up at the end of the SAS session.
I think this might be what I'm after. Thanks for the suggestion! Searching the forum pulls up the following page:
https://communities.sas.com/t5/SAS-Procedures/create-sub-folder-in-work/td-p/214934
It looks like I can use the above process to establish a uniquely named directory inside WORK, and then point my own libname at it.
BTW - Unfortunately I probably wasn't clear in my first post. I don't want the macro to literally write its datasets to WORK. I do want to specify a library, but I want the pathway for that library to be dynamic and unique, and ideally for the SAS system to perform garbage cleanup just as it does for WORK.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.