DATA Step, Macro, Functions and more

Creating subfolders and libnames

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

Creating subfolders and libnames

Hello! I’m still learning SAS so hopefully my question is coming out clearly.  I’m trying to automatically update the libname and/or automatically create sub folders for a program if the same program is ran multiple times during the same month. I tried to make the libname a macro variable, but it gives me an error. 

This is what I have so far.

options dlcreatedir;

LIBNAME Run1 ( "C:\Users\person1\Documents\201405\run1"  

                              "C:\Users\person1\Documents\&YR_MNTH\run2"

                              "C:\Users\person1\Documents\&YR_MNTH\run3");

libname Run1 clear;

This is what I would like it to create automatically or something along the lines. I’m just not sure how to get there without the errors.

Day 1 (first time I run the program):

LIBNAME  Run1: "C:\Users\person1\Documents\&YR_MNTH\run1"   ;

Day 2 (second time I run the program):

LIBNAME  Run2: "C:\Users\person1\Documents\&YR_MNTH\run2"   ;

Day 2 (third time I run the program):

LIBNAME  Run3: "C:\Users\person1\Documents\&YR_MNTH\run3"   ;

If anyone has any advice please let me know. Thank you.


Accepted Solutions
Solution
‎06-04-2014 11:02 AM
Super User
Posts: 10,041

Re: Creating subfolders and libnames

Posted in reply to Astounding

Astounding,

I don't think that could work. use DCREATE () instead .

%let n=%sysfunc(translate(%sysfunc(datetime(),datetime.),_,:));
data _null_;
 rc=dcreate("run&n",'c:\temp\');
run;

libname x v9 "c:\temp\run&n";

Xia Keshan

View solution in original post


All Replies
Super User
Posts: 11,343

Re: Creating subfolders and libnames

If the directories do not exist before the libname statement is run then you will get an error. Libname doesn't create a folder/directory.

Look at the documentation for DCREATE for on possible way to create folders.

Respected Advisor
Posts: 3,799

Re: Creating subfolders and libnames

With LIBNAME and DLCREATEDIR you can only create one new sub-level at a time.  So create &YR_MNTH first then run1 run2.

Super User
Posts: 10,041

Re: Creating subfolders and libnames

Add a X statement before libname statement to execute a OS command if you have right to execute PIPE command.

options noxwait;
%let n=%sysevalf(%sysfunc(ranuni(0))*10000,i);
x "mkdir c:\temp\run&n";
libname x v9 "c:\temp\run&n";


Xia Keshan

Occasional Contributor
Posts: 13

Re: Creating subfolders and libnames

I don't believe I have rights for this command. I get the error "Shell escape is not valid in this SAS session".

Super User
Posts: 5,516

Re: Creating subfolders and libnames

If you don't have rights to the X command, this is worth a try instead:

%sysexec mkdir c:\temp\run&n;

No guarantees, but it might sneak through.  You don't need the quotes when switching to %sysexec.

Solution
‎06-04-2014 11:02 AM
Super User
Posts: 10,041

Re: Creating subfolders and libnames

Posted in reply to Astounding

Astounding,

I don't think that could work. use DCREATE () instead .

%let n=%sysfunc(translate(%sysfunc(datetime(),datetime.),_,:));
data _null_;
 rc=dcreate("run&n",'c:\temp\');
run;

libname x v9 "c:\temp\run&n";

Xia Keshan

Super User
Super User
Posts: 7,977

Re: Creating subfolders and libnames

Personally I would investigate version control.  What you are talking about is keeping a snapshot at certain timepoints.  TortoiseSVN for example is free and relatively simple.  You would have one copy of the run, and utilizing the version control system that would keep your snapshots.  I have worked at a company which had this type of Run 1, dated folders structure and it really is a complete nightmare when working with more than just yourself.  The other reason is why keep multiple copies of the same thing, just let a DB backend like SVN keep differences between versions, reduces the size of these things greatly.

Regular Contributor
Posts: 217

Re: Creating subfolders and libnames

Dear mcrum1,

Are you certain that you can create folders in the directory C:\Users\person1\Documents\?  If you can, all of the previous suggestions should work.  If you can manually create the new directories in the documents folder, then the problem is surmountable by any of the prior suggestions, if you are not restricted by your site administrator.  If you can manually create the new directories, please send your error logs for any of the prior suggestions.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 1640 views
  • 1 like
  • 7 in conversation