Are you tired of manually creating folders in SAS? Don’t worry, SAS offers various options, functions, and commands for folder creation. Let’s explore them!
These functions can be automated to perform repetitive routine tasks, avoid manual work, and save a great deal of time.
To create folders in SAS, there are a variety of system options, functions, and commands that you can use.
How to Create folder in SAS using the DLCREATEDIR System option?
DLCREATEDIR is a system option and can create folders when used with the libname statement.
Firstly, let’s understand what ‘DLCREATEDIR System option’ is. It’s a specific term used within SAS, not a standard programming phrase. Its function is to authorise the creation of a new directory when the ‘libname’ statement is used.
If you’re uncertain what a ‘libname’ statement is, it’s a command in SAS that connects your program to a SAS library. To create a folder in SAS using this option, follow these steps. Start by turning on the DLCREATEDIR system option – not enabling, just simply turning it on.
Following this, you can craft a new directory using the ‘libname’ statement. To illustrate, below is an example of a SAS command that both activates the DLCREATEDIR System option and creates a new folder.
options dlcreatedir;
libname newdir "/home/9to5sas/my_content/new";
Once you run the above statement, check the SAS log to see if the library assignment was successful or not.
SAS Log:
NOTE: Library NEWDIR was created.NOTE: Libref NEWDIR was successfully assigned as follows: Engine: V9 Physical Name: /home/9to5sas/my_content/new
To create subfolders, you can use multiple specifications in the LIBNAME statement.
options dlcreatedir;
libname newdir ("/home/9to5sas","/home/9to5sas/statistics");
Important! Make sure to turn off the DLCREATEDIR option after creating directories.
The NODLCREATEDIR
specifies not to create a directory for a SAS library named in a LIBNAME statement.
How to create folders in SAS using the DCREATE function?
DCREATE is a function that lets you create directories in your operating environment.
Syntax:
NewDirectory=DCREATE('Directory-name', 'parent-directory');
- Directory-name – It is the name of the directory to create. Do not include the path name here.
- Parent-directory is the complete pathname of the directory in which to create the new directory.
To create a new directory in the UNIX operating environment with the directory name ‘test’, follow this syntax:
NewDirectory=dcreate('test', '/home/test/abc/');
Follow this syntax to create a new directory in the Windows operating environment with the directory name ‘test’.
NewDirectory=dcreate('test', 'c:testdir');
Create folders in SAS using X md (an X Statement)
With the help of X statements, you can give UNIX operating system commands directly from SAS.
md is a command in the Unix Operating system to make directories.
data _null_;
x 'md /home/9to5sas/test';
run;
How to Delete a Directory in SAS?
This example uses FDELETE to delete an empty directory to which you have to write access. If the directory is not empty, the optional SYSMSG function returns an error message stating that SAS is unable to delete the file.
filename testdir 'physical-filename';
data _null_;rc=fdelete('testdir');
put rc=;
msg=sysmsg();
put msg=;
run;
Final Thoughts
There you have it—a comprehensive guide to creating folders in SAS using system options, functions, and commands. By leveraging these tools, you can automate the process, thereby saving a considerable amount of time and effort. If you’re looking to further streamline your SAS workflow, don’t miss our guide on SAS Data Access Functions to efficiently handle your data
Please subscribe to our mailing list for weekly updates. You can also find us on Instagram and Facebook.