DATA Step, Macro, Functions and more

resolving macro variable inside single quotes

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

resolving macro variable inside single quotes

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.

Accepted Solutions
Solution
‎07-07-2016 12:59 PM
Super Contributor
Super Contributor
Posts: 3,174

Re: resolving macro variable inside single quotes

[ Edited ]

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


All Replies
Solution
‎07-07-2016 12:59 PM
Super Contributor
Super Contributor
Posts: 3,174

Re: resolving macro variable inside single quotes

[ Edited ]

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;

 

Occasional Contributor
Posts: 18

Re: resolving macro variable inside single quotes

It works! Thanks for your help.
Occasional Contributor
Posts: 18

Re: resolving macro variable inside single quotes

Incidentally, I tried the following and it worked as well, but it's not as elegant:

put put %unquote(%nrbquote('[save.as("CSmiley Tongueathname\filename..xlsx")]'));


Again, thanks for your help.
Super Contributor
Posts: 394

Re: resolving macro variable inside single quotes

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]
SAS Employee
Posts: 58

Re: resolving macro variable inside single quotes

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;
Regular Contributor
Posts: 241

Re: resolving macro variable inside single quotes

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

Valued Guide
Posts: 2,177

Re: resolving macro variable inside single quotes

Posted in reply to chang_y_chung_hotmail_com
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
Regular Contributor
Posts: 241

Re: resolving macro variable inside single quotes

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

Valued Guide
Posts: 2,177

Re: resolving macro variable inside single quotes

Posted in reply to chang_y_chung_hotmail_com
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
Regular Contributor
Posts: 241

Re: resolving macro variable inside single quotes

Posted in reply to chang_y_chung_hotmail_com

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;

🔒 This topic is solved and locked.

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

Discussion stats
  • 10 replies
  • 39476 views
  • 1 like
  • 6 in conversation