BookmarkSubscribeRSS Feed
tomrvincent
Rhodochrosite | Level 12

If you need to save all your libnames and then be able to restore them later, here's how I do it:

 

/* save libnames  somewhere...I use the program's current path with the date and userid subdirectories*/
proc sql; create table foo as select distinct 'libname ' || libname || ' "' || trim(path) || '";' as libname format $500. from sashelp.vlibnam
where libname not in ('MAPSGFK','MAPSSAS','SASHELP','WORK');quit;

data _null_;
file "&CURRENTPATH.&YMD8/&SYSUSERID/libnames.sas";
set foo; libname = trim(libname); put libname;
run;

 

Then in some other program (or, for parallel projects, I put it in the 'insert custom code before submitted code'):

 

%include "&CURRENTPATH.&YMD8/&SYSUSERID/libnames.sas";

 

3 REPLIES 3
ballardw
Super User

The SQL is not needed and use of the CATX function makes for cleaner string creation.

Also examination of the SASHELP.VLIBNAM will show that distinct is needed because of additional variables to indicate which row to use.

data _null_; 
 set sashelp.vlibnam;
 /* add any SAS supplied libraries in the list below*/
 where libname not in ('MAPSGFK','MAPSSAS','SASHELP','WORK')
       and sysname='Filename';
 file "&CURRENTPATH.&YMD8/&SYSUSERID/libnames.sas";
 length libstr $ 500;
 libstr = catx(' ', 'libname',libname, quote(strip(path)),';');
 put libstr;
run;

Personally I would also include the SASUSER library in the exclusion list as using that SAS library potentially can get you in trouble with SAS updates and reinstall overwriting or using a different location.

 

I might also consider using an explicit path in the file statement as the &CURRENTPATH and &ymd8 variables may not be defined in all environments (Not in mine for sure).

tomrvincent
Rhodochrosite | Level 12

I couldn't use an explicit path because there could be multiple people running the same program (thus the date and userid).

 

&CURRENTPATH and &ymd8 are variables I calculate...just examples of possible directory structures.

 

There can be all sorts of other libnames to exclude.  I have a lot more...I basically added anything that gave me an error.

ballardw
Super User

@tomrvincent wrote:

I couldn't use an explicit path because there could be multiple people running the same program (thus the date and userid).

 

&CURRENTPATH and &ymd8 are variables I calculate...just examples of possible directory structures.

 

There can be all sorts of other libnames to exclude.  I have a lot more...I basically added anything that gave me an error.


Understood.

When providing an example for others it is helpful to indicate that about your macro variables &CURRENTPATH and &ymd8 were used and set by you elsewhere in code.

 

Since there are varying SAS automatic variables from different installations and modules I was not sure if those were system or user variables.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 640 views
  • 0 likes
  • 2 in conversation