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

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

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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

12 REPLIES 12
data_null__
Jade | Level 19

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.

Patrick
Opal | Level 21

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

mdavidson
Quartz | Level 8

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

Patrick
Opal | Level 21

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

mdavidson
Quartz | Level 8

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.

data_null__
Jade | Level 19

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.

mdavidson
Quartz | Level 8

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

data_null__
Jade | Level 19

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

Patrick
Opal | Level 21

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.

Tom
Super User Tom
Super User

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.

AdilVural
Calcite | Level 5

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) ;

jakarman
Barite | Level 11

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

---->-- ja karman --<-----

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 12 replies
  • 8171 views
  • 3 likes
  • 6 in conversation