You ever go through your folders and manually create sub-folders?
Sometimes it gets a bit tedious. And it’s not fun if you’re looking at a bunch of directories.
The LIBNAME
Statement
We’re taught pretty early to use the LIBNAME
statement to establish paths to directories where we need to pull data, save data, etc.
For example, let’s say I’m on one of the servers, and I use a LIBNAME
statement to establish a connection with my assigned directory.
Note: I am using SAS Studio. Your directory will be different on the server.
libname projdir '/home/u40912106/sasuser.v94/your-project-directory/michaelmaguire';
Now we go to file explorer, right click and add all of our folders, right?
Enter DLCREATEDIR
Instead of manually creating folders, you can do it within your program.
OPTIONS DLCREATEDIR
is an option that allows you to create folders if they don’t exist already.
From the documentation:
[DLCREATEDIR] Specifies to create a directory for the SAS library that is named in a LIBNAME statement if the directory does not exist.
Using DLCREATEDIR
Let’s say I want to create three subfolders within my personal directory:
- `mydata`: to store any copies/child datasets that I want to preserve
- `myfigs`: to store any figures that I generate
- `mytbls`: to store any tables made for articles.
I recommend you specify DLCREATEDIR
at the beginning of your program.
options dlcreatedir;
# Our regular libname
libname projdir '/home/u40912106/sasuser.v94/your-project-directory/michaelmaguire';
# Create `mydata` directory within our `projdir` folder
libname mydata '/home/u40912106/sasuser.v94/your-project-directory/michaelmaguire/data/';
# Create `myfigs` directory within our `projdir` folder
libname myfigs '/home/u40912106/sasuser.v94/your-project-directory/michaelmaguire/figures/';
# Create `mytbls` directory within our `projdir` folder
libname mytbls '/home/u40912106/sasuser.v94/your-project-directory/michaelmaguire/tables/';
Then we can see the log:
69 options dlcreatedir;
70
71 libname projdir '/home/u40912106/sasuser.v94/your-project-directory/michaelmaguire';
NOTE: Libref PROJDIR was successfully assigned as follows:
Engine: V9
Physical Name: /home/u40912106/sasuser.v94/your-project-directory/michaelmaguire
72
73 libname mydata '/home/u40912106/sasuser.v94/your-project-directory/michaelmaguire/data/';
NOTE: Libref MYDATA was successfully assigned as follows:
Engine: V9
Physical Name: /home/u40912106/sasuser.v94/your-project-directory/michaelmaguire/data
74 libname myfigs '/home/u40912106/sasuser.v94/your-project-directory/michaelmaguire/figures/';
NOTE: Libref MYFIGS was successfully assigned as follows:
Engine: V9
Physical Name: /home/u40912106/sasuser.v94/your-project-directory/michaelmaguire/figures
75 libname mytbls '/home/u40912106/sasuser.v94/your-project-directory/michaelmaguire/tables/';
NOTE: Libref MYTBLS was successfully assigned as follows:
Engine: V9
Physical Name: /home/u40912106/sasuser.v94/your-project-directory/michaelmaguire/tables
After this, you can save files to these directories like you would with a LIBNAME
statement.
Let’s say I would save a copy of the sashelp.class
dataset to my personal directory.
Do not copy raw data sets on the server to your personal directory.
data mydata.class;
set sashelp.class;
run;
And the log:
69 data mydata.class;
70 set sashelp.class;
71 run;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set MYDATA.CLASS has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
user cpu time 0.01 seconds
system cpu time 0.00 seconds
memory 828.37k
OS Memory 24484.00k
Timestamp 05/17/2022 02:30:42 PM
Step Count 29 Switch Count 2
Page Faults 0
Page Reclaims 169
Page Swaps 0
Voluntary Context Switches 25
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 272