BookmarkSubscribeRSS Feed
danielh
Calcite | Level 5

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

4 REPLIES 4
Patrick
Opal | Level 21

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;

Tom
Super User Tom
Super User

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.

chang_y_chung_hotmail_com
Obsidian | Level 7

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

Ksharp
Super User

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 6588 views
  • 2 likes
  • 5 in conversation