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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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