BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
LFern
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
tsap
Pyrite | Level 9
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/

View solution in original post

5 REPLIES 5
tsap
Pyrite | Level 9
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/

koyelghosh
Lapis Lazuli | Level 10

@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.

 

Reeza
Super User
There is a shortcut...I can't recall what now, but there's a way to have it create the folders above as well....DCREATE can also be used within a macro loop to automate this. I think there's a few of those floating around if you search. If you have XCMD enabled, using mkdir is another option.
ballardw
Super User

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.

Tom
Super User Tom
Super User

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";

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 5 replies
  • 2971 views
  • 4 likes
  • 6 in conversation