DATA Step, Macro, Functions and more

Create Folders in SAS

Accepted Solution Solved
Reply
Regular Contributor
Posts: 237
Accepted Solution

Create Folders in SAS

I don't know if this is even possible, but I'm just going to throw this question out there.

I was wondering if SAS has the ability to create folders?  For example, suppose I automatically run code every week that generates a host of output files.  Suppose I output them in C:\SAS\Reports, but I want to have a new subfolder with the date each time the reports are generated so that this week they are output to C:\SAS\Reports\2015-02-20 and next week they are output to C:\SAS\Reports\2015-02-27, and so on.  This way, I don't have to manually create a new folder each time and move all the files from one place to another.

Is this possible?


Accepted Solutions
Solution
‎02-18-2015 04:55 PM
Respected Advisor
Posts: 3,156

Re: Create Folders in SAS

Posted in reply to djbateman

DCREATE function.

View solution in original post


All Replies
Solution
‎02-18-2015 04:55 PM
Respected Advisor
Posts: 3,156

Re: Create Folders in SAS

Posted in reply to djbateman

DCREATE function.

Super User
Posts: 7,771

Re: Create Folders in SAS

Posted in reply to djbateman

Or use call system (in a data step) or the X statement (anywhere in SAS code).

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 89

Re: Create Folders in SAS

Posted in reply to djbateman
%MACRO check_dir(dir=) ;
      OPTIONS NOXWAIT ;
     
%LOCAL rc fileref ;
     
%LET rc = %SYSFUNC(FILENAME(fileref,&dir)) ;
     
%IF %SYSFUNC(FEXIST(&fileref)) %THEN
         
%PUT NOTE: Directory &dir does already exist. ;
     
%ELSE
         
%DO ;
             
%SYSEXEC MD &dir ;
             
%PUT %SYSFUNC(SYSMSG()) Directory is created. ;
         
%END ;
     
%LET rc=%SYSFUNC(FILENAME(fileref)) ;
%mend check_dir ;

%
check_dir(dir = c:\temp\DATA );
Respected Advisor
Posts: 3,156

Re: Create Folders in SAS

At the risk of being obnoxious, me still think DCREAT is the better option. Let SAS to worry about OS. You went a great deal of using Macro, you can host your OS independent directory into Macro variable, so it even makes more sense for you to NOT use 'MD'.   What if later on your system is being migrated to Linux (or any other OS that SAS is running on)? switch it to 'mkdir'?

Just my2cents,

Haikuo

Super User
Posts: 7,771

Re: Create Folders in SAS

Since one already has to consider the underlying OS's naming syntax when using any of the methods, I don't see the possible migration as a major argument. Personally, I would use the filename pipe and data _null_ step method to get a comprehensive log in case something went wrong.

In a case where the "create a directory" function is done via macro, only one macro needs to be adapted to a new platform, but all calls need to be checked for syntax anyway (a "C:\" in a UNIX path name may have interesting consequences)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,942

Re: Create Folders in SAS

I would agree with you Hai.kuo, for the reasons you have given, and also that piping commands out to system sometimes is blocked by admin, and if it doesn't work - i.e. macro does not work correctly, you do run the risk of removing things unintentionally, or creating loads of unwanted folders etc. temporary areas.

Contributor
Posts: 26

Re: Create Folders in SAS

Posted in reply to djbateman

At the risk of appearing vain, here's a sample I submitted a few years ago - https://communities.sas.com/thread/71369

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 417 views
  • 0 likes
  • 6 in conversation