I am trying to amend the macro below to accept a macro parameter as the 'location' argument for a dir command. However I cannot get it to resolve correctly due to the nested quotes issue. Using %str(%') does not work, neither do quoting functions for some reason.
The macro will work fine when the filepath has no spaces (eg C:\temp\withnospace) as the middle quotes aren't needed. However I need this macro to work for filepaths with spaces (eg 'C:\temp\with space\').
Please help!
%macro get_filenames(location)
filename pipedir pipe "dir &location. /b " lrecl=32767;
data filenames;
infile pipedir truncover;
input line $char1000.;
run;
%mend;
%get_filenames(C:\temp\) /* works */
%get_filenames('C:\temp\with space') /* doesnt work */
it works for me if i call the original macro this way
of course i had to add the semicolon at the end of the
%macro
statement.if your directory contains a comma, bad things happen. to fix, use the
%str()
macroHere's another way of achieving the same result without needing to use a PIPE.
Here's one that unscrambles the order of quoting and unquoting:
where macro variable basedir can contain spaces and so can the filenames. This combination of
%unquote
and%str(%')
is a frequently occuring macro idiom."what if I have single quote in my dir?"
Handling this situation requires a macro quoting function, such as
%bquote();
Continuing the example above, this:should do it.
To avoid infinite iterations of this kind of question, look at Ian Whitlock's paper, A Serious Look at Macro Quoting, which is available here;
There are (many) others, but this is the most widely cited. A little note: anything by Ian Whitlock is probably worthwhile. He writes clearly and his understanding of SAS issues is awesome.
here's a quick macro to pull windows-based directory listings into a sas data set.
and here's how they get called
notice there is no trailing backslash when specifying the base directory.
C:
notC:\
.We use this little macro
Sample Call
If you run in batch and don't have the
option noxwait xsync
the job will hang on the server waiting for an operator response.Make the following several changes and your code will work.
(1) End the
%macro
statement with a semi-colon;(2) Surround the macro variable resolution with doubled-up double quotes and
%unquote
;(3) Release the file handle by clearing it; and
(4) Don't single quote your input parameter. macro quote instead, if necessary.