BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RyanD
Fluorite | Level 6
My macro variable &filename won't resolve inside the following line of code:

put '[save.as("C:\pathname\&filename.xlsx")]';

I've tried different variations of single and double quotes to get this to work but so far I've not been successful. What other ways are there to force SAS to resolve the macro variable inside single quotes?

Thanks.
1 ACCEPTED SOLUTION

Accepted Solutions
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10

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;

 

View solution in original post

10 REPLIES 10
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10

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;

 

RyanD
Fluorite | Level 6
It works! Thanks for your help.
RyanD
Fluorite | Level 6
Incidentally, I tried the following and it worked as well, but it's not as elegant:

put put %unquote(%nrbquote('[save.as("C:pathname\filename..xlsx")]'));


Again, thanks for your help.
Tim_SAS
Barite | Level 11
Try doing it in 2 steps:

[pre]
%let filename=example;
data _null_;
x = "C:\pathname\&filename..xlsx";
x2 = '[save.as("' || x || '")]';
put x2;
run;
[/pre]
PatrickG
SAS Employee
Yeah, you must either use double quotes as Scott said or do as Tim suggested. the SAS Macro Facility will not look inside of matching single quotes -- but you can sometimes get around this if you use %STR inside a %UNQUOTE function e.g.

%let name=Fred;

data _null_;
put %unquote(%str(%'NAME: &name%'));
run;
chang_y_chung_hotmail_com
Obsidian | Level 7

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
Rhodochrosite | Level 12
and another way to have a macro variable (appear to) resolve within single quotes

libname here '.' ;
%let here = %sysfunc( pathname( here )) ;
libname here ;
* that was just to put something interesting into a macro variable 🙂 ! ;

%let there = "'&here'" ;
%let shere = %sysfunc( dequote( &there )) ;
%put (here) = (&here) (there) = (&there) (shere) = (&shere) ;

assignment to "there" is single quoted &here, but as it is within double quotes it gets resolved.
assignment to "shere" removes the double quotes .

Combining that is straightforward and could be turned into a simple macro function
%macro squote( string ) /des='single quote this string' ;
%sysfunc( dequote( "'%superq(string)'" ))
%mend squote ;
%put (single test) %squote(single test) ;
demonstrated on this SASlog[pre]29 %put (single test) %squote(single test);
(single test) 'single test'[/pre]ok
peterC
chang_y_chung_hotmail_com
Obsidian | Level 7

@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
   ***''''***
   ***'"'***
   --*;

Peter_C
Rhodochrosite | Level 12
That's better!
handling internal single quotes is important.
It's a pity there isn't a single-quote version of the quote() function.
TRANSTRN() is new to me so thank you for pointing that out, too.
peterC
chang_y_chung_hotmail_com
Obsidian | Level 7

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 10 replies
  • 87632 views
  • 1 like
  • 6 in conversation