Hello
Suppose I have the following, where I want to create a 3 folders called "year2019", "state", and "California":
----------------------------------------------------------------------------------
Options dlcreatedir;
%Let Year = 2019;
Libname WANT "\\server1\UnitedStates\Health\year&YEAR.\state\California\";
---------------------------------------------------------------------------------
The above code gives me the following error: Create of library WANT failed. Error in LIBNAME statement.
However, if I keep the exact code and run it in a piecemeal fashion then it surprisingly works!
For example, in the first run I can just highlight & run "Libname WANT "\\server1\UnitedStates\Health\year&YEAR.\";
In the 2nd run, I can highlight & run "Libname WANT "\\server1\UnitedStates\Health\year&YEAR.\state\" ;
In my final run, I can highlight and run "Libname WANT "\\server1\UnitedStates\Health\year&YEAR.\state\California\";
Even though the text of the code hasn't changed, it only runs successfully if I run it folder by folder. Any way to get the option to work for the whole file path so that three folders are create immediately?
Any help is appreciated.
Options dlcreatedir;
%Let Year = 2019;
Libname WANT ("\\server1\UnitedStates\Health\year&YEAR.", "\\server1\UnitedStates\Health\year&YEAR.\state", "\\server1\UnitedStates\Health\year&YEAR.\state\California\");
This should accomplish the results you are expecting.
Reference page: https://blogs.sas.com/content/sasdummy/2013/07/02/use-dlcreatedir-to-create-folders/
Options dlcreatedir;
%Let Year = 2019;
Libname WANT ("\\server1\UnitedStates\Health\year&YEAR.", "\\server1\UnitedStates\Health\year&YEAR.\state", "\\server1\UnitedStates\Health\year&YEAR.\state\California\");
This should accomplish the results you are expecting.
Reference page: https://blogs.sas.com/content/sasdummy/2013/07/02/use-dlcreatedir-to-create-folders/
@LFern I would think about it like this.
For the California folder to be created, first State folder has to be ready and before that year&YEAR. folder has to be ready. When you are trying to create "Libname WANT "\\server1\UnitedStates\Health\year&YEAR.\state\California\" at once the preceeding folders are not ready.
I would have done it in three statements (as has already worked for you).
Libname WANT "\\server1\UnitedStates\Health\year&YEAR.\";
Libname WANT "\\server1\UnitedStates\Health\year&YEAR.\state\" ;
Libname WANT "\\server1\UnitedStates\Health\year&YEAR.\state\California\";
Having said that...there might be a shortcut, actually (which I am unaware of). One thing I have learnt is that there is more than one way to do it in SAS.
From the documentation it clearly describes what is going on:
Restriction If the path specified in the LIBNAME statement contains multiple components, SAS creates only the final component in the path. If any intermediate components of the path do not exist, SAS does not assign the specified path. For example, when the code libname mytestdir ‘c:\mysasprograms\test’
executes, and c:\mysasprograms exists, SAS creates the test directory. If c:\mysasprograms does not exist, SAS does not create the test directory.
My emphasis added on multiple components.
If you are running on Unix and can run operating system commands from SAS then make the directory yourself. The -p option on mkdir will attempt to make the intermediate directories.
%Let Year = 2019;
%let path=\\server1\UnitedStates\Health\year&YEAR.\state\California\;
data _null_;
infile "mkdir -p &path" pipe;
input;
put _infile_;
run;
Libname WANT "&path";
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.