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

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?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

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

Check out SAS Innovate on-demand content! Watch the main stage sessions, keynotes, and over 20 technical breakout sessions!

View solution in original post

12 REPLIES 12
Reeza
Super User

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?

 

 


 

ballardw
Super User

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

FreelanceReinh
Jade | Level 19

By using subfolders of the WORK directory you can avoid conflicts between separate SAS sessions and let SAS do the clean-up afterwards automatically.

Reeza
Super User

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.


 

FreelanceReinh
Jade | Level 19

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.

ChrisHemedinger
Community Manager

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

Check out SAS Innovate on-demand content! Watch the main stage sessions, keynotes, and over 20 technical breakout sessions!
desertsp
Obsidian | Level 7

Thanks Chris. That is exactly what I needed (so were the other posts...but you pulled it all together for me).

 

PaigeMiller
Diamond | Level 26

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

--
Paige Miller
FreelanceReinh
Jade | Level 19

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

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
FreelanceReinh
Jade | Level 19

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.

desertsp
Obsidian | Level 7

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-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
  • 12 replies
  • 2501 views
  • 15 likes
  • 6 in conversation