Lorem Ipsum
Lorem Ipsum

Reputation: 4534

SAS - Create new libraries nested inside WORK

tl;dr: Can SAS libraries be nested within one another in the Enhanced Editor Explorer?

I am working with code which generates a plethora of data sets. Although there are many individual data sets, they can be grouped into various categories. For instance, perhaps 30 of them are incoming "raw" data, another 50 are analysis "results" and the remaining 20 are "intermediate" steps.

Currently, all 100 data sets reside in the Work directory. They have been well named so that they appear next to one another in the SAS Explorer window. However, I would prefer to organize them in folders.

One way to do this is to create new directories within the temporary Work folder.

%let dirWORK = %sysfunc(pathname(Work));

options dlcreatedir;

libname raw     "&dirWORK./raw";
libname interm  "&dirWORK./intermediate";
libname results "&dirWORK./results";

As sub-directories of Work, these directories and their contents will be deleted when the session ends. This is agreeable.

Not agreeable is how the raw, iterm, and results libraries appear one level up in 'Active Libraries' instead of within the 'Contents of "Work"'. This behavior is somewhat counter-intuitive and awkward.

Upvotes: 3

Views: 1626

Answers (3)

Lorem Ipsum
Lorem Ipsum

Reputation: 4534

There appears to be no ideal solution. The best I could devise requires the use of two part names, a non-trivial amount of extra code, a custom setup, and is potentially confusing to other programmers. The easiest configuration appears to be the default behavior: simply dumping all data sets in the Work library and sifting through all of them.

For prosperity, here is the best solution I could devise to organize things.

  1. Understanding the Display Manager and Explorer Window

The Display Manager (DM) controls the various windows in the Enhanced Editor. This includes windows such as log, results, pgm, output and explorer. It is the latter which is of principle interest here.

A user may configure windows per session using the menus. There exist, however, a set of commands which may be used to automatically configure the DM for each session. When used with the AUTOEXEC.sas facility, this allows the user to 'permanently' configure the SAS editor to their preferences.

The default explorer view for the Enhanced Editor is the "Contents of 'SAS Environment'".

enter image description here

When the explorer window is selected, a tree hierarchy view can be toggled. There are several ways to do this. Here is one way:

  1. Select the explorer window
  2. View > Show Tree

enter image description here

Having to do this every time SAS is opened becomes a pain. After we clean up the library tree, we will automate this.

  1. Remove any extraneous libraries

SAS loads a variety of libraries by default. These include WORK, SASHELP, and SASUSER, as well as others that are part of non-base products. In my case, the MAPS, MAPSGFK, and MAPSSAS libraries are also loaded (see above picture). As I never have used these (and likely never will), they only serve to clutter up my library directory. To remove these, one can edit their config file.

SAS implements a whole web of config files. On a Windows system, the likely relevant sasv9.cfg file is located here:

C:\Program Files\SASHome\SASFoundation\9.4\nls\en

You will need admin rights to edit it. Disregarding the DO NOT EDIT BELOW THIS LINE warning, comment out the the following using PL/I style comments.

Un-commented:

-MAPS !SASROOT\maps
-MAPSGFK !SASROOT\mapsgfk
-MAPSSAS !SASROOT\maps

Commented:

/*    -MAPS !SASROOT\maps*/
/*    -MAPSGFK !SASROOT\mapsgfk*/
/*    -MAPSSAS !SASROOT\maps*/

Now when SAS opens, the MAPS, MAPSGFK, and MAPSSAS library will not be automatically loaded. Unfortunately, there does not seem to be a way to disable the loading of the SASUSER and SASHELP libraries. If someone knows how, please let me know!

  1. Configure the Explorer Window

The explorer window can be 'configured' using the DM statement and the AUTOEXEC.sas facility.

Navigate to the directory containing SAS.EXE. On a default Windows installation, it is located here:

C:\Program Files\SASHome\SASFoundation\9.4

Create a SAS program named AUTOEXEC.sas containing the following code and place it in the directory which contains SAS.EXE. This step requires admin privileges. I find it easiest to create the file in a non-privileged environment and simply copy the AUTOEXEC.sas program into the directory.

Include this in the AUTOEXEC.sas program:

dm 'dmsexp; tree; expand libraries; expand work;';

When SAS intializes, the AUTOEXEC.sas code is executed before any user input is accepted. The above code does the following:

dmsexp - Brings the explorer window into command focus.

tree - Toggles the tree hierarchy view.

expand libraries - Within the "Active Libraries" pane, drills down into Libraries.

expand work - Within the "Active Libraries" pane, drills down into the Work library.

Each of these is a separate command and could be issued on separate lines. Instead of this, semi-colons are used to issue them in a single line.

Now when SAS is opened, the tree menu is available and the contents of the Work library are viewed. The picture below is somewhat deceiving. The "SAS Environment" pane is not set to a convenient width as pictured. It must be manually adjusted. If anyone has a way to automate this, please let me know!

enter image description here

  1. Storing data sets in sub-folders

To ensure that data sets are deleted when the SAS session ends, sub-folders needs to be created within the temporary system folder where the Work library points to.

The following structure can now be followed:

/*Determine location of Work directory*/
%let dirWORK = %sysfunc(pathname(Work));

/*Allow LIBNAME statement to create new directories*/
options dlcreatedir;

/*Create sub-folders within the temporary Work directory
  and assign librefs*/
libname raw     "&dirWORK./raw";
libname interm  "&dirWORK./intermediate";
libname results "&dirWORK./results";

Now, all the raw data sets can be assigned to the raw library. Within SAS, they will be logically separated from Work. That is, if the data set foobar is created in the raw library, it can only be accessed via raw.foobar. A statement such as data = foobar or data = work.foobar will not work. Yet because the raw library is in fact a sub-folder of Work, it will be deleted when the SAS session is ended.

Upvotes: 0

Tom
Tom

Reputation: 51601

SAS librefs are just single words, so by definition there is no heirarchy. You could try using librefs that will place them next to each other alphabetically? Perhaps WORKIN, WORKMID, WORKOUT. Then they would sort in logical order.

You could use a file explorer to browse the directory structure you have created, but I don't think the SAS Explorer tool in SAS Display Manager can handle that.

DMS does have a file explorer tool you could try. You can start directly on your current WORK directory by running this command from the command line of any window, or via the DM statement in a program.

exproot dir="%sysfunc(pathname(work))" title="Work Directory"

Upvotes: 3

Allan Bowe
Allan Bowe

Reputation: 12691

Libraries can contain many things.. But not other libraries. Your 'Active Libraries' will always show your (available) list of libraries at the same level, regardless of where or how they were defined:

enter image description here

One option if you'd like to view your datasets like a typical file browser is to use the Explorer window. Just click View / Explorer, and navigate to your datasets that way..

eg:

%let dirWORK =C:/temp/work;
options dlcreatedir;
libname raw     "&dirWORK./raw";
libname interm  "&dirWORK./intermediate";
libname results "&dirWORK./results";

data raw.test;
set sashelp.class;
run;

enter image description here

Upvotes: 3

Related Questions