Help using Base SAS procedures

Automatic Directory Creation - Windows

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 117
Accepted Solution

Automatic Directory Creation - Windows

I run some sas queries on a monthly basis and they export data to specific folders. Problem is I have to manually make these folders before I run the code so SAS wont error out.

Is there any way to automate this?

My folders have to be named in this fashion:

number of the Month - Name of the Month Year (12 - Dec 2011). I would also like to create a subfolder called Excel.

Also is there any way the month and number is populated automatically...so when I run this in January it changes to (1 - Jan 2012)?

Please provide the codes needed if possible...I am very new to SAS. Thanks!


Accepted Solutions
Solution
‎02-02-2012 05:09 PM
Super User
Super User
Posts: 6,502

Re: Automatic Directory Creation - Windows

This will get todays date. Get a date in the previous month.  Build the directory names into variables. Then call the DCREATE() function to make the directories.

I put the parent directory into a variable so that I can use it two places without having to type it twice.

The PUT statement will let you see the directory names that it generated and the result of the DCREATE function.

data _null_;

  rootdir='U:\Software\';

  today=today();

  lastmonth=intnx('month',today,-1);

  dir1=catx(' ',put(month(lastmonth),z2.),'-',put(lastmonth,monname3.),year(lastmonth));

  dir2=catx(' ',put(month(today),z2.),'-',put(today,monname3.),year(today));

newdir1=dcreate(dir1,rootdir);

  newdir2=dcreate(dir2,rootdir);

  put (rootdir dir1 newdir1 dir2 newdir2) (=/);

run;

View solution in original post


All Replies
Trusted Advisor
Posts: 2,113

Automatic Directory Creation - Windows

You could use the "X" command to shell out to Windows to create it.

For instance, if you had a macro variable with the name of the directory, it might look something like

%LET yrmo=12-Dec2011;

x "mkdir c:\mypath\&yrmo";

x "mkdir c:\mypath\&yrmo\excel";

Doc Muhlbaier

Duke

Respected Advisor
Posts: 3,124

Re: Automatic Directory Creation - Windows

Assume you have access to C:\, and you want to use current date to name the folder:

data _null_;

mc=catx('-',month(today()),put(today(),monyy7.));

call symput('d',cats('md c:\',mc,'\excel\'));

run;

x "&d";

Please note: TODAY() helps to extract today's date. It also seems to me that dos command "md" does not support blank, so you will end up with no blank between mmm and yyyy. You can also add:

options noxwait;

at the beginning of the code to avoid manually get out of dos window.

Kindly Regards,

Haikuo

Frequent Contributor
Posts: 117

Re: Automatic Directory Creation - Windows

I am not looking for the folder name to be the current date. I just want it to be the month number (jan is 1, Feb is 2 etc.), month abbreviation and Year (01 - Jan - 2012)

Also, I want to make these on a network drive not the local drive... how would I go about that?

Respected Advisor
Posts: 3,124

Re: Automatic Directory Creation - Windows

If you have tried my code above, it does exactly what you want. MONTH() to extract month info from TODAY().

It does not make a difference whether you are making folders locally or on network drive, as long as you have write access and it has been mapped correctly.

Regards,

Haikuo

Frequent Contributor
Posts: 117

Re: Automatic Directory Creation - Windows

Thanks Haikuo. I have one last question:

My directory is named in this fashion:

C:\AB&C\Reports & Tests\Folder

Your query seems to get stuck becuse of the "&" in my directory name...I unfortunately cannot rename it...how can I made the code work with this path?

Respected Advisor
Posts: 3,124

Re: Automatic Directory Creation - Windows

Try this one:

data _null_;

mc=catx('-',month(today()),put(today(),monyy7.));

call symput('d',cats('md %nrbquote("c:\AB%nrbquote(&)C\Reports & Tests\")',mc,'\excel\'));

run;

x "&d";

Regards,

Haikuo

Edit: I am really a newbie in term of Macro language. So my code above would inevitably  falls clumsy.  Macro experts please recue!

Regular Contributor
Posts: 171

Re: Automatic Directory Creation - Windows

There is no need to use macro variables at all.

data _null_;

     newdir=dcreate(catx('-', month(today()), put(today(), monyy7.)), 'C:\AB&C\Reports & Tests\');

run;

Respected Advisor
Posts: 3,124

Re: Automatic Directory Creation - Windows

awesome! I did not know the existence of  dcreate(). Thanks a lot!

Super User
Super User
Posts: 6,502

Re: Automatic Directory Creation - Windows

Make sure to put the month with leading zero. Otherwise the directories will not sort properly in most browsing tools like Windows Explorer.

data _null_;

     newdir=dcreate(catx('-', put(month(today()),z2.), put(today(), monyy7.)), 'C:\AB&C\Reports & Tests\');

run;

What I have been doing lately for date stamps in files or directory is using the YYMMDD format.  That sorts properly and avoids confusion over which part is month and which is day.

For you report structure perhaps you want 2012 - 01 - January ?  Or perhaps introduce a year folder?  2012\01 - January ?

Frequent Contributor
Posts: 117

Re: Automatic Directory Creation - Windows

This worked but I just have 1 small issue. When I run this I don't get a space between the month and the year... so it looks like : 02 - FEB2012

How should I modify it so it looks like : 02 - FEB 2012 (note space after month)

Also, is there any way to modify this code so it creates the previous month's folder?

Thanks.

Super User
Super User
Posts: 6,502

Re: Automatic Directory Creation - Windows

Pull out the month and year separately.  Use the MONNAME3 format to get first three characters of the month name. 

data _null_;

  newdir=dcreate(

catx('-'

, put(month(today()),z2.)

, put(month(today()), monname3.)||''||put(year(today()),4.)

)

, 'C:\AB&C\Reports & Tests\');

run;

Frequent Contributor
Posts: 117

Automatic Directory Creation - Windows

Tried the code and it created the space but now the month name is wrong... so it outputs : 02 - Jan - 2012

Another Question : How do I make this code create a folder for the previous month. So if I run this in Feburary it creates a folder named : 01 - Jan 2012

Many Thanks!!

PROC Star
Posts: 7,363

Re: Automatic Directory Creation - Windows

Just change the '-' to ' - '.  e.g.,

data _null_;

     newdir=dcreate(catx(' - ', month(today()),

            put(today(), monyy7.)),

            'C:\AB&C\Reports & Tests\');

run;

Frequent Contributor
Posts: 117

Re: Automatic Directory Creation - Windows

Tried the code and it created the space but now the month name is wrong... so it outputs : 02 - Jan - 2012

Code Used:

data _null_;

  newdir=dcreate(

catx(' - '

, put(month(today()),z2.)

, put(month(today()), monname3.)||''||put(year(today()),4.)

)

, 'U:\Software\');

run;

Another Question : How do I make this code create a folder for the previous month. So if I run this in Feburary it creates a folder named : 01 - Jan 2012

Many Thanks!!

☑ This topic is SOLVED.

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

Discussion stats
  • 20 replies
  • 5791 views
  • 7 likes
  • 7 in conversation