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.
%letDrive=M:;
%letPath=%nrstr(Clients\Reporting\01Data\A & B\Extracts);
x "&drive.";
x "cd&path.";
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.
Thanks
Daniel
I believe this is only about correct quoting. Below code worked for me:
options noxwait;
%let path=%nrbquote(C:\temp\"this & that");
data _null_;
x "mkdir &path";
x "dir > &path\dirlist.txt";
run;
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 Drive=M:;
%let Path=%nrstr(Clients\Reporting\01Data\A & B\Extracts);
x %sysfunc(quote(dir "&drive\&path" >xlsFileList.txt));
Simplified command.
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";
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';
Ksharp
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.