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!
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;
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
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
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?
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
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?
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!
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;
awesome! I did not know the existence of dcreate(). Thanks a lot!
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 ?
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.
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;
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!!
Just change the '-' to ' - '. e.g.,
data _null_;
newdir=dcreate(catx(' - ', month(today()),
put(today(), monyy7.)),
'C:\AB&C\Reports & Tests\');
run;
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!!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.