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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

20 REPLIES 20
Doc_Duke
Rhodochrosite | Level 12

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

Haikuo
Onyx | Level 15

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

vomer
Obsidian | Level 7

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?

Haikuo
Onyx | Level 15

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

vomer
Obsidian | Level 7

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?

Haikuo
Onyx | Level 15

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!

polingjw
Quartz | Level 8

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;

Haikuo
Onyx | Level 15

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

Tom
Super User Tom
Super User

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 ?

vomer
Obsidian | Level 7

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.

Tom
Super User Tom
Super User

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;

vomer
Obsidian | Level 7

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!!

art297
Opal | Level 21

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

data _null_;

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

            put(today(), monyy7.)),

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

run;

vomer
Obsidian | Level 7

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!!

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 20 replies
  • 14542 views
  • 9 likes
  • 7 in conversation