Macro quoting help

Accepted Solution Solved
Reply
Super Contributor
Posts: 418
Accepted Solution

Macro quoting help

Hello everyone. I have never fully understood macro quoting within SAS and am having problems with a relatively simple issue. If anyone could help I would greatly appreciate it!

Basically I want to replicate the following code with Macros for the input zip file, and the output file location. Note that as I understand it I 100% need the double quotes around the directories in order to allow for the spaces in the directory names. If this is not true and someone can show me another way to get this to work, I would much appreciate it!

data _null_;

unzipcmd='"c:\program files\winzip\Winzip.exe" -min -e ';

zipfile=' "I:\project new\zipfile.zip" ';

whereto=' "i:\project new" ';

cmd=unzipcm||' '|| zipfile|| ' ' || whereto;

putlog cmd;

call system( cmd);

run;

My question is, how do I take a defined macro variable, (like below) and get it to have the double quotes around it, and then the single quotes around that so that I can call a directory with spaces dynamically? Aka how do I get the code below to work?

%let zipfile_macro=I:\project new\zipfile.zip;

data _null_;

unzipcmd='"c:\program files\winzip\Winzip.exe" -min -e ';

zipfile=' "&zipfile_macro." ';

whereto=' "i:\project new" ';

cmd=unzipcm||' '|| zipfile|| ' ' || whereto;

putlog cmd;

call system( cmd);

run;

Thanks! and let me know if you have any questions!


Accepted Solutions
Solution
‎06-06-2013 01:32 PM
Super User
Posts: 5,069

Re: Macro quoting help

Here's another approach that might work.  Switch from X to %SYSEXEC.  In most cases, it operates the same way but doesn't require the outer quotes.  So you could try:

%sysexec "c:\Program Files\7-zip\7z.exe" x "&macro1." -O"&macro2.";


View solution in original post


All Replies
Super User
Posts: 5,069

Re: Macro quoting help

It looks like you have a blank between the single and double quotes, so I'm leaving those in place.  To avoid macro quoting entirely, use double quotes in the DATA step:

zipfile = " ""&zipfile_macro."" ";

Alternatively, use macro quoting before the DATA step.  Add a second %LET statement just after the first one, to add quotes to the value of &ZIPFILE_MACRO:

%let zipfile_macro = %unquote(%str(%' "&zipfile_macro." %'));

Then in the DATA step switch to:

zipfile = &zipfile_macro.;

Good luck.

Super Contributor
Posts: 418

Re: Macro quoting help

Hello Astounding. I actualy still cannot get this to work within the system (using either method). I had to switch to WIndows ZIp 7, and I am able to get the program to work with the following code

x '"c:\Program Files\7-zip\7z.exe" x "I:\Projects\data download\SMT\R200810\SMT_COL.zip" -O"I:\Projects\data download\SMT\R200810"';


Note there are a single quote at the beginning and end of string.... Now what I would like to do is replace the second directory call, and the third directory output to be marco variables. I have tried using let statements and all forms of macro quoting. Can anyone get the following code to work?

%let macro1=I:\Projects\data download\SMT\R200810\SMT_COL.zip;

%let macro2=I:\Projects\data download\SMT\R200810;

x '"c:\Program Files\7-zip\7z.exe" x "&macro1." -O"&macro2."';

I have tried the method of double quoting within the data step and then using call symput, and the method of %unquote as you show above, yet it does not seem to work.

Furthermore, created one macro string of the entire proces and then tried calling that with an x COmmand (as show below) but to no avail.

%let macrovar= '"c:\Program Files\7-zip\7z.exe" x "I:\Projects\data download\SMT\R200810\SMT_COL.zip" -O"I:\Projects\data download\SMT\R200810"';

x &macrovar.;

Solution
‎06-06-2013 01:32 PM
Super User
Posts: 5,069

Re: Macro quoting help

Here's another approach that might work.  Switch from X to %SYSEXEC.  In most cases, it operates the same way but doesn't require the outer quotes.  So you could try:

%sysexec "c:\Program Files\7-zip\7z.exe" x "&macro1." -O"&macro2.";


Super Contributor
Posts: 418

Re: Macro quoting help

THat was the trick. Using the %sysexec option seems to be working in the loop I designed. Thank you very much astounding!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 242 views
  • 0 likes
  • 2 in conversation