Help using Base SAS procedures

Create a directory on UNIX

Accepted Solution Solved
Reply
Contributor
Posts: 53
Accepted Solution

Create a directory on UNIX

Hello --

I know how to do this in a Windows environment, but sadly I have no idea how to do the same on a UNIX environment. Can anybody assist?

%macro chk_dir(dir=) ;
     options noxwait;
     %local rc fileref ; 
     %let rc = %sysfunc(filename(fileref,&dir)) ; 
     %if %sysfunc(fexist(&fileref))  %then 
        %put NOTE: The directory "&dir" exists ; 
     %else 
       %do ; 
           %sysexec md   &dir ; 
           %put %sysfunc(sysmsg()) The directory has been created. ;
     %end ; 
     %let rc=%sysfunc(filename(fileref)) ; 
  %mend chk_dir ; 
 
  %chk_dir(dir=c:\temp) ;    %*   <==  your directory specification goes here ; 
  %chk_dir(dir=c:\temp\sascode)   


http://www.sas.com/offices/europe/uk/support/sas-hints-tips/ht1_mar04.html


Accepted Solutions
Solution
‎06-15-2012 05:27 PM
Respected Advisor
Posts: 3,777

Re: Create a directory on UNIX

I think it would work if you use the UNIX command to make a directory "mkdir".  Probably need to use forward slash instead of "back splash" :smileysilly:

Give a go.

You can also type man mkdir to see help.

Also use "which" to get full path to mkdir command so aliases won't get in the way.

View solution in original post


All Replies
Solution
‎06-15-2012 05:27 PM
Respected Advisor
Posts: 3,777

Re: Create a directory on UNIX

I think it would work if you use the UNIX command to make a directory "mkdir".  Probably need to use forward slash instead of "back splash" :smileysilly:

Give a go.

You can also type man mkdir to see help.

Also use "which" to get full path to mkdir command so aliases won't get in the way.

Respected Advisor
Posts: 3,894

Re: Create a directory on UNIX

You can also use the Base SAS function "DCREATE()" which works both under Windows and UNIX (Linux).

Contributor
Posts: 53

Re: Create a directory on UNIX

Thanks, this is good to know when I'm using base but in this case I'm using EG.  Good to know!

Respected Advisor
Posts: 3,894

Re: Create a directory on UNIX

"Base SAS" = "Base SAS Language". EG is the client. The code you're writing/generating is very often Base SAS Language.

Contributor
Posts: 53

Re: Create a directory on UNIX

Thanks, this worked great.  I'm a little embarrassed it was that simple.  Also, I'm not sure what you mean when you say "which" with regards to the full path.

Respected Advisor
Posts: 3,777

Re: Create a directory on UNIX

which - locate a command; display its pathname or alias

> which mkdir

/shared/gnu/bin/mkdir

On my system mkdir is /shared/gnu/....

I would use the fully qualified version "/shared/gnu/bin/mkdir" in my program to insure that no future aliases cause problems.

Contributor
Posts: 53

Re: Create a directory on UNIX

This is strange, this code worked briefly yesterday.  Any ideas?  I'm trying to create a directory folder called "aa".  I've attached the SAS code along with the log below.

%macro chk_dir(dir=) ;

/*     options noxwait;*/

     %local rc fileref ;

     %let rc = %sysfunc(filename(fileref,&dir)) ;

     %if %sysfunc(fexist(&fileref))  %then

        %put NOTE: The directory "&dir" exists ;

     %else

       %do ;

           %sysexec which mkdir &dir ;

           %put %sysfunc(sysmsg()) The directory has been created. ;

           %put &fileref;

     %end ;

     %let rc=%sysfunc(filename(fileref)) ;

  %mend chk_dir ;

  %chk_dir(dir=home/sas/permanent/BI Group SAS Code/DecisionSciences/aa) ;

%macro chk_dir(dir=) ;

/*     options noxwait;*/

     %local rc fileref ;

     %let rc = %sysfunc(filename(fileref,&dir)) ;

     %if %sysfunc(fexist(&fileref))  %then

        %put NOTE: The directory "&dir" exists ;

     %else

       %do ;

           %sysexec which mkdir &dir ;

           %put %sysfunc(sysmsg()) The directory has been created. ;

           %put &fileref;

     %end ;

     %let rc=%sysfunc(filename(fileref)) ;

  %mend chk_dir ;

  %chk_dir(dir=home/sas/permanent/BI Group SAS Code/DecisionSciences/aa) ;

***********LOG**************

14     GOPTIONS ACCESSIBLE;
15     %macro chk_dir(dir=) ;
16     /* options noxwait;*/
17          %local rc fileref ;
18          %let rc = %sysfunc(filename(fileref,&dir)) ;
19          %if %sysfunc(fexist(&fileref))  %then
20             %put NOTE: The directory "&dir" exists ;
21          %else
22            %do ;
23                %sysexec mkdir &dir ;
24                %put %sysfunc(sysmsg()) The directory has been created. ;
25    %put &fileref;
26          %end ;
27          %let rc=%sysfunc(filename(fileref)) ;
28       %mend chk_dir ;
29   
30       %chk_dir(dir=home/sas/permanent/BI Group SAS Code/DecisionSciences/aa) ;

MLOGIC(CHK_DIR):  Beginning execution.

MLOGIC(CHK_DIR):  Parameter DIR has value home/sas/permanent/BI Group SAS Code/DecisionSciences/aa

MLOGIC(CHK_DIR):  %LOCAL  RC FILEREF

MLOGIC(CHK_DIR):  %LET (variable name is RC)

SYMBOLGEN:  Macro variable DIR resolves to home/sas/permanent/BI Group SAS Code/DecisionSciences/aa

SYMBOLGEN:  Macro variable FILEREF resolves to #LN00035

MLOGIC(CHK_DIR):  %IF condition %sysfunc(fexist(&fileref)) is FALSE

MLOGIC(CHK_DIR):  %SYSEXEC  mkdir &dir

SYMBOLGEN:  Macro variable DIR resolves to home/sas/permanent/BI Group SAS Code/DecisionSciences/aa

MLOGIC(CHK_DIR):  %PUT %sysfunc(sysmsg()) The directory has been created.

WARNING: Physical file does not exist, /opt/biserver/Lev1/SASApp/home/sas/permanent/BI Group SAS Code/DecisionSciences/aa. The

directory has been created.

MLOGIC(CHK_DIR):  %PUT &fileref

SYMBOLGEN:  Macro variable FILEREF resolves to #LN00035

#LN00035

MLOGIC(CHK_DIR):  %LET (variable name is RC)

MLOGIC(CHK_DIR):  Ending execution.

31

Respected Advisor
Posts: 3,777

Re: Create a directory on UNIX

I think your problem is the spaces in the directory name.  They need to be escaped with \ or I think you can use quotes, but I don't know how quotes will effect the rest of your macro %SYSFUNCs and the rest.

Try it like this...

/opt/biserver/Lev1/SASApp/home/sas/permanent/BI\ Group\ SAS\ Code/DecisionSciences/aa

Respected Advisor
Posts: 3,894

Re: Create a directory on UNIX

Just 2 observations:

"home/sas/permanent/BI Group SAS Code/DecisionSciences/aa" is a relative path. If you're not 100% sure that in all environments SAS starts with the same directory then this code can fail. Should "home/..." eventually be "$HOME/..."?

There are blanks in your path name ".../BI Group SAS Code/...". What UNIX flavor allows such blanks? I would avoid blanks if you can.

Super User
Super User
Posts: 6,502

Re: Create a directory on UNIX

I have been using \ to prevent aliases from confusing my calls to unix commands. So call to \mkdir rather than just mkdir to prevent user defined aliases from being used.  I find it is required most often with commands like ls or rm as many users create aliases (or have them created for them by the startup scripts they copied from the local expert in the next cubicle) that set various options.

This does not prevent users from changing the search path and causing some other version of mkdir to be found first.  But then it also doesn't crash when the system admin moves things around so that /shared/gnu directory is no longer valid.

N/A
Posts: 1

Re: Create a directory on UNIX

I made an small update ...

%macro chk_dir(dir=) ;

/*     options noxwait; */

  %sysexec %str(pwd; ls –l);

     %local rc fileref ; 

     %let rc = %sysfunc(filename(fileref,&dir)) ; 

     %if %sysfunc(fexist(&fileref))  %then 

        %put NOTE: The directory "&dir" exists ; 

     %else 

       %do ; 

           %sysexec %str(mkdir &dir);  Smiley Wink

           %put %sysfunc(sysmsg()) The directory has been created. ;

     %end ; 

     %let rc=%sysfunc(filename(fileref)) ; 

%mend chk_dir ; 

%chk_dir(dir=/data/test) ;

Valued Guide
Posts: 3,208

Re: Create a directory on UNIX

Why doing that all that difficult:   SAS trick: get the LIBNAME statement to create folders for you - The SAS Dummy

---->-- ja karman --<-----
☑ This topic is SOLVED.

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

Discussion stats
  • 12 replies
  • 3948 views
  • 3 likes
  • 6 in conversation