BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
Astounding
PROC Star

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.


Reeza
Super User

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

Peter_C
Rhodochrosite | Level 12

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

Reeza
Super User

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.

SASPhile
Quartz | Level 8

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

 

Tom
Super User Tom
Super User

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

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
  • 6 replies
  • 9354 views
  • 6 likes
  • 5 in conversation