DATA Step, Macro, Functions and more

establishing additional "work" libraries?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

establishing additional "work" libraries?

[ Edited ]

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?

 

 


Accepted Solutions
Solution
a week ago
Community Manager
Posts: 3,442

Re: establishing additional "work" libraries?

Posted in reply to FreelanceReinhard

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

View solution in original post


All Replies
Super User
Posts: 23,778

Re: establishing additional "work" libraries?

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?

 

 


 

Super User
Posts: 13,583

Re: establishing additional "work" libraries?


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

Trusted Advisor
Posts: 1,259

Re: establishing additional "work" libraries?

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

Super User
Posts: 23,778

Re: establishing additional "work" libraries?

Posted in reply to FreelanceReinhard

Only issue is how to generate unique names that don't collide as well.

 


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


 

Trusted Advisor
Posts: 1,259

Re: establishing additional "work" libraries?

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.

Solution
a week ago
Community Manager
Posts: 3,442

Re: establishing additional "work" libraries?

Posted in reply to FreelanceReinhard

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

Occasional Contributor
Posts: 18

Re: establishing additional "work" libraries?

Posted in reply to ChrisHemedinger

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

 

Respected Advisor
Posts: 3,066

Re: establishing additional "work" libraries?

Posted in reply to ChrisHemedinger

@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
Trusted Advisor
Posts: 1,259

Re: establishing additional "work" libraries?

Posted in reply to PaigeMiller

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

Respected Advisor
Posts: 3,066

Re: establishing additional "work" libraries?

Posted in reply to FreelanceReinhard

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
Trusted Advisor
Posts: 1,259

Re: establishing additional "work" libraries?

Posted in reply to PaigeMiller

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.

Occasional Contributor
Posts: 18

Re: establishing additional "work" libraries?

Posted in reply to FreelanceReinhard

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.

 

 

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 12 replies
  • 160 views
  • 13 likes
  • 6 in conversation