DATA Step, Macro, Functions and more

X Windows Command - Cd to a folder directory with "&"

Reply
N/A
Posts: 1

X Windows Command - Cd to a folder directory with "&"

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

Respected Advisor
Posts: 4,173

X Windows Command - Cd to a folder directory with "&"

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;

Super User
Super User
Posts: 7,062

Re: X Windows Command - Cd to a folder directory with "&"

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.

Regular Contributor
Posts: 241

Re: X Windows Command - Cd to a folder directory with "&"

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";

Super User
Posts: 10,041

Re: X Windows Command - Cd to a folder directory with "&"

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

Ask a Question
Discussion stats
  • 4 replies
  • 1785 views
  • 2 likes
  • 5 in conversation