Editor's note: this topic is very popular. Thanks to @sbb, @chang_y_chung_hotmail_com, and others who contributed useful replies. We have consolidated some of these into this single reply so that future readers may benefit.
Use double-quote characters with your PUT statement, however you must also tell SAS when you want to use that same character within the data that is to be generated. You do this by "escaping" the double-quote with another double-quote (double the double-quotes).
Also, you need to check the syntax of the trailing period with macro variable resolution.
4 %let filename = x; 5 data _null_; 6 put "[save.as(""C:\pathname\&filename..xlsx"")]"; 7 run; [save.as("C:\pathname\x.xlsx")] NOTE: DATA statement used (Total process time): real time 0.01 seconds cpu time 0.00 seconds
Scott Barry
SBBWorks, Inc.
You can also use the %TSLIT macro from SAS, which will supply/escape quotes as needed. See Sample 25076: Resolve a macro variable within single quotation marks.
%let filename = x;
data _null_;
put %tslit([save.as("C:\pathname\&filename..xlsx")]);
run;
Editor's note: this topic is very popular. Thanks to @sbb, @chang_y_chung_hotmail_com, and others who contributed useful replies. We have consolidated some of these into this single reply so that future readers may benefit.
Use double-quote characters with your PUT statement, however you must also tell SAS when you want to use that same character within the data that is to be generated. You do this by "escaping" the double-quote with another double-quote (double the double-quotes).
Also, you need to check the syntax of the trailing period with macro variable resolution.
4 %let filename = x; 5 data _null_; 6 put "[save.as(""C:\pathname\&filename..xlsx"")]"; 7 run; [save.as("C:\pathname\x.xlsx")] NOTE: DATA statement used (Total process time): real time 0.01 seconds cpu time 0.00 seconds
Scott Barry
SBBWorks, Inc.
You can also use the %TSLIT macro from SAS, which will supply/escape quotes as needed. See Sample 25076: Resolve a macro variable within single quotation marks.
%let filename = x;
data _null_;
put %tslit([save.as("C:\pathname\&filename..xlsx")]);
run;
Doesn't have to be that complicated. See also this posting (and the discussion following in the thread) on
<a href="http://www.listserv.uga.edu/cgi-bin/wa?A2=ind0901B&L=sas-l&P=R33845">sas-l</a>
.
%let filename = my;
%put "[save.as('c:\pathname\&filename..xlsx')]";
%*-- on log
"[save.as('c:\pathname\my.xlsx')]"
--*;
@Peter.C: I would rather do it this way:
%macro squote(str);
%local sq;
%let sq = %str(%');
%let str = %qsysfunc(transtrn(%superq(str),&sq,&sq&sq));
%unquote(&sq.&str.&sq)
%mend squote;
%put ***%squote(%str(%'))***;
%put ***%squote(%str(%"))***;
%*-- on log
***''''***
***'"'***
--*;
Found this gem in the sas 9.3 (ts1m0) autocall library. Nice!
/*********************************************************************
* This version of the macro was provided by Gordon Keener. It will *
* work in the datastep because it unquotes the result at the end. *
* It also uses the datastep function quote to added the necessary *
* values to the input value. *
*********************************************************************/
%macro tslit(value);
%local s1 s2 v1 v2 v3;
%let s1 = %str(%'%");
%let s2 = %str(%"%');
%let v1 = %qsysfunc(translate(&value, &s1, &s2));
%let v2 = %qsysfunc(quote(&v1));
%let v3 = %qsysfunc(translate(&v2, &s2, &s1));
%unquote(&v3)
%mend;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.