01-03-2012 12:00 AM
I have a quick SAS question regarding the “x” command.
I need to access the following directory with cmd to createa list of csv files in a particular folder.
%letPath=%nrstr(Clients\Reporting\01Data\A & B\Extracts);
x "dir *.csv >xlsFileList.txt";
Unfortunately, this failed because of the “&”sign in the directory.
Testing with cmd directly, I found that entering the following command works: cd Clients\Reporting\01Data\"A & B"\Extracts.
Logically we followed this by running the SAScommand
x ‘cd Clients\Reporting\01 Data\”A & B”\Extracts.’;
Once again, this failed. I have been stuck on this for the past hour. It would be much appreciated if someone could provide a solution.
01-03-2012 12:22 AM
I believe this is only about correct quoting. Below code worked for me:
%let path=%nrbquote(C:\temp\"this & that");
x "mkdir &path";
x "dir > &path\dirlist.txt";
01-03-2012 10:27 AM
Use double quote characters as your outside quotes and SAS will expand the macro variable references.
Use the QUOTE function to handle doubling the embedded quotes.
%let Path=%nrstr(Clients\Reporting\01Data\A & B\Extracts);
x %sysfunc(quote(dir "&drive\&path" >xlsFileList.txt));
01-03-2012 01:14 PM
Your path is not to be resolved by an explicit or implicit %eval(). Thus you don't need to macro quote the singleton ampersand character at all. The ampersand has to be hidden from the cmd.exe shell though, since it is a command separator to the interpreter (see msdn article). The following worked fine on my sas running on a windows 7:
%let drive = c:;
%let path = /temp/a & b;
x "&drive & cd ""&path"" & dir *.csv > list.txt";
01-03-2012 09:36 PM
Another way is to make a .bat file.
%let Drive=c: ; %let Path=\temp\A & B\Extracts ; data _null_; file 'c:\x.bat'; a='cd "'||"&drive&path"||'"'; put a; b='dir *.pdf > x.txt'; put b; run; options noxwait; x 'c:\x.bat'; x 'del c:\x.bat';