I'm using a macro I found online to create Windows directories on my local disk. It's failing if the directory name contains hyphens.
Can explain what is going on?
Here's the problematic code (source)
%MACRO makeDirectory(DIR=);
%LET COUNT=0;
%LET DLM=\;
%IF %SUBSTR(&DIR,%LENGTH(&DIR),1) NE &DLM %THEN %DO;
%LET DIR=&DIR&DLM;
%END;
%DO J=1 %TO %LENGTH(&DIR);
%IF %SUBSTR(&DIR,&J,1)=&DLM %THEN %DO;
%LET COUNT=%EVAL(&COUNT+1);
%END;
%END;
%LET DRIVE=%SUBSTR(&DIR,1,3);
%LET LEVEL=&DRIVE;
%DO I=2 %TO &COUNT;
%LET WORD=%SCAN(&DIR,&I,&DLM);
%LET LNEW=&LEVEL&WORD&DLM;
data _null_;
rc=filename('newdir',"&lnew");
c=dopen('newdir');
if c=0 then new=dcreate("&word","&level");
run;
%LET LEVEL=&LNEW;
%END;
%MEND;
/* this will fail */
%makeDirectory(Dir=C:\TEMP\WITH-HYPHENS);
Here's the error from the log:
SYMBOLGEN: Macro variable DIR resolves to C:\TEMP\WITH-HYPHEN\
SYMBOLGEN: Macro variable J resolves to 13
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric
operand is required. The condition was: %SUBSTR(&DIR,&J,1)=&DLM
SYMBOLGEN: Macro variable DLM resolves to \
ERROR: The macro MAKEDIRECTORY will stop executing.
Thanks!
%IF %QSUBSTR(&DIR,%LENGTH(&DIR),1) NE &DLM %THEN %DO;
But yes, I agree with @Reeza, this is a lot of work to write such a macro when you can use a built-in function to do the same thing.
Any reason not to use the DCREATE() function directly?
http://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=p1aj29pf4cxnirn15q5hmf0tv438.htm&...
data _null_;
rc=dcreate("With-Hyphens", "C:\_localdata\temp\");
put rc;
run;
Or try the method here:
https://blogs.sas.com/content/sasdummy/2013/07/02/use-dlcreatedir-to-create-folders/
Thank you for replying.
I'm not using DCREATE() directly because it requires that the parent directory already exists. The macro I found is iteratively creates parent directories as needed. I -think- the error is occurring in the "J loop", so I don't believe there's any problem with the actual creation of each directory (maybe I should have simplified my macro before posting!).
I did consider OPTIONS DLCREATEDIR however there's the risk that that option is disabled. If not for that consideration, the approach would be ideal.
%IF %QSUBSTR(&DIR,%LENGTH(&DIR),1) NE &DLM %THEN %DO;
But yes, I agree with @Reeza, this is a lot of work to write such a macro when you can use a built-in function to do the same thing.
That does the trick, thanks!
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.