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
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.
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.
You can also use the Base SAS function "DCREATE()" which works both under Windows and UNIX (Linux).
Thanks, this is good to know when I'm using base but in this case I'm using EG. Good to know!
"Base SAS" = "Base SAS Language". EG is the client. The code you're writing/generating is very often Base SAS Language.
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.
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.
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
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
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.
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.
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);
%put %sysfunc(sysmsg()) The directory has been created. ;
%end ;
%let rc=%sysfunc(filename(fileref)) ;
%mend chk_dir ;
%chk_dir(dir=/data/test) ;
Why doing that all that difficult: SAS trick: get the LIBNAME statement to create folders for you - The SAS Dummy
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.