DATA Step, Macro, Functions and more

Getting a macro variable to resolve within an x command

Accepted Solution Solved
Reply
Super User
Posts: 17,748
Accepted Solution

Getting a macro variable to resolve within an x command

using an X command to copy a file:

x 'copy

     "C:\temp\sample.xls"

     "C:\temp\sample2.xls"';

But now I'd like sample2 to be a macro variable, but it won't resolve inside the single quotes (I'm assuming). How would I go about getting this to work? I'm on SAS 9.2

%let my_var=test;

x 'copy

     "C:\temp\sample.xls"

     "C:\temp\&test..xls"';

Thanks!


Accepted Solutions
Solution
‎01-17-2013 01:04 PM
Super User
Posts: 5,071

Re: Getting a macro variable to resolve within an x command

Reeza,

The entire string after X is in single quotes ... and I know that you know that macro variables don't resolve within single quotes.  Reverse them:  double quotes on the outside and single quotes on the inside.

Alternatively, you could replace X with %SYSEXEC and you wouldn't need the outer quotes.

Good luck.


View solution in original post


All Replies
Solution
‎01-17-2013 01:04 PM
Super User
Posts: 5,071

Re: Getting a macro variable to resolve within an x command

Reeza,

The entire string after X is in single quotes ... and I know that you know that macro variables don't resolve within single quotes.  Reverse them:  double quotes on the outside and single quotes on the inside.

Alternatively, you could replace X with %SYSEXEC and you wouldn't need the outer quotes.

Good luck.


Super User
Posts: 17,748

Re: Getting a macro variable to resolve within an x command

I tried reversing the quotes and it doesn't work .

%sysexec works perfectly, Thanks Astounding

%sysexec copy "file1.xls" "&file_name..xls";

Message was edited by: Reeza

Valued Guide
Posts: 2,174

Re: Getting a macro variable to resolve within an x command

Reeza

I have a very little to add but it seems a bit significant..

Although Tom has provided a most reliable way of ensuring a string is properly quoted even if you are unsure of what it contains (by using the quote() function) there is a simpler way that is also very generalised - when seeking to deliver quote marks within a string that is quoted(with the same kind of quotes) repeat the embedded quote - for example

x "copy

     ""C:\temp\sample.xls""

     ""C:\temp\&test..xls"" ";

Super User
Posts: 17,748

Re: Getting a macro variable to resolve within an x command

I had tried that as well Peter, but it didn't work on the system I was on. I'll retry again tomorrow to verify.

Super Contributor
Posts: 647

Re: Getting a macro variable to resolve within an x command

I tried to used the same command by extending in a do loop. But it did  not work as expected.  

OPTIONS NOXWAIT NOXSYNC;
%macro zxc;
%do i= 1 %to 68;
%SYSEXEC copy "D:\temp\&&memname&i." "c:\temp\&&memname&i.";
%end;
%mend zxc;
%zxc;

 

In The macro arrray &&memname&i, only the counter "i" is getting resolved.

SYMBOLGEN: Macro variable I resolves to 1
SYMBOLGEN: Macro variable I resolves to 1
SYMBOLGEN: Macro variable I resolves to 2
SYMBOLGEN: Macro variable I resolves to 2

 

Super User
Super User
Posts: 6,495

Re: Getting a macro variable to resolve within an x command

To allow you to specify filenames with embedded spaces DOS wants you to enclose your filenames in dquotes.

To allow your macro variables to expand SAS wants you to enclose the full command in dquotes.

I have found that the QUOTE function helps with this.

%let my_var=My Goofy Filename;

x %sysfunc(quote(copy "C:\temp\sample.xls" "C:\temp\&test..xls")) ;

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 2927 views
  • 4 likes
  • 5 in conversation