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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

8 REPLIES 8
ballardw
Super User

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.

data_null__
Jade | Level 19

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

Ksharp
Super User

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

mcrum1
Calcite | Level 5

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

Astounding
PROC Star

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.

Ksharp
Super User

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

jwillis
Quartz | Level 8

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.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 7137 views
  • 2 likes
  • 7 in conversation