<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to add single quote to a macro variable within a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703227#M215454</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let city = nyc;
data _null_;
	call symputx('z',quote("&amp;amp;city","'"));
run;
%put &amp;amp;=z;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I add that the values of macro variables almost never need to be surrounded by either single or double quotes, as this makes the coding much more difficult. I strongly urge you to avoid this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Much simpler code works in almost every situation I can possibly think of:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let z=nyc;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 02 Dec 2020 22:48:40 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-12-02T22:48:40Z</dc:date>
    <item>
      <title>How to add single quote to a macro variable within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703220#M215450</link>
      <description>&lt;P&gt;Hi all, I am trying to add a single quote to a macro variable (&amp;amp;city) within a macro (%y).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After I ran the below code, &amp;amp;z returns a value with double quote which is "nyc", my goal is to get the value with single quote which is 'nyc'.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any advices are gladly appreciated.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let city = nyc;
%macro y;
%let z= %sysfunc(quote(&amp;amp;city,"'"));
%put &amp;amp;z;
%mend;
%y;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;log as below:&lt;/P&gt;&lt;P&gt;GOPTIONS ACCESSIBLE;&lt;BR /&gt;29 %let city = nyc;&lt;BR /&gt;30 %macro y;&lt;BR /&gt;31 %let z= %sysfunc(quote(&amp;amp;city,"'"));&lt;BR /&gt;32 %put &amp;amp;z;&lt;BR /&gt;33 %mend;&lt;BR /&gt;34 %y;&lt;BR /&gt;"nyc"&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 22:23:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703220#M215450</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2020-12-02T22:23:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to add single quote to a macro variable within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703224#M215451</link>
      <description>&lt;P&gt;What is so critical about single quote?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Attaching quotes to macro variables except for very specific reasons is often a poor idea and can add a lot of complexity as may find that you need to use the masking functions like %str %nrstr or %superq to keep other code from misbehaving.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 22:43:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703224#M215451</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-12-02T22:43:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to add single quote to a macro variable within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703226#M215453</link>
      <description>&lt;P&gt;To the macro processor everything is a string. So there is no need to add quotes around string literals to show it you didn't mean to type a variable name or keyword.&amp;nbsp; So you gave the QUOTE function the three character string&lt;FONT face="courier new,courier"&gt; "'"&lt;/FONT&gt; instead of the one character string&lt;FONT face="courier new,courier"&gt; '&lt;/FONT&gt;.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let z= %sysfunc(quote(&amp;amp;city,%str(%')));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You probably also want to make sure the value of CITY doesn't have unbalanced quotes or macro triggers.&amp;nbsp; Try adding %SUPERQ().&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let z= %sysfunc(quote(%superq(city),%str(%')));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or use utility macro like&amp;nbsp;&lt;A href="https://github.com/sasutils/macros/blob/master/squote.sas" target="_self"&gt;%squote()&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 22:54:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703226#M215453</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-02T22:54:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to add single quote to a macro variable within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703227#M215454</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let city = nyc;
data _null_;
	call symputx('z',quote("&amp;amp;city","'"));
run;
%put &amp;amp;=z;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I add that the values of macro variables almost never need to be surrounded by either single or double quotes, as this makes the coding much more difficult. I strongly urge you to avoid this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Much simpler code works in almost every situation I can possibly think of:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let z=nyc;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Dec 2020 22:48:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703227#M215454</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-12-02T22:48:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to add single quote to a macro variable within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703229#M215456</link>
      <description>&lt;P&gt;The reason is this variable will be used as a filter condition via ODBC connection with MS SQL server (see below). SQL server doesn't recognized "nyc" or nyc, it only recognizes value within single quote like 'nyc'.&lt;/P&gt;&lt;P&gt;I could create another macro variable using&amp;nbsp;call symputx within data step or simply using %let, but I just wonder if there is any way to add single quote to this macro variable within a macro.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO inv(table);
PROC SQL;
  CONNECT TO odbc(id=&amp;amp;id pwd=&amp;amp;pwd dsn=inv); 
  EXECUTE (delete from inventory.&amp;amp;TABLE where city = &amp;amp;city ) by odbc;
  disconnect from odbc;
QUIT;
&amp;lt;more code here&amp;gt;

%MEND; 

%inv(table1);
%inv(table2);
%inv(table3);
%inv(table4);
&amp;lt;more tables&amp;gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Dec 2020 23:03:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703229#M215456</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2020-12-02T23:03:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to add single quote to a macro variable within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703230#M215457</link>
      <description>&lt;P&gt;Thanks. I could create a macro variable with single quote in a separate step, but I just want to explore the possibility of adding single quote for a macro variable within a macro.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 23:07:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703230#M215457</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2020-12-02T23:07:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to add single quote to a macro variable within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703232#M215459</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/89004"&gt;@LL5&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The reason is this variable will be used as a filter condition via ODBC connection with MS SQL server (see below). SQL server doesn't recognized "nyc" or nyc, it only recognizes value within single quote like 'nyc'.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Actually, that's the only reason I can think of why you might want a macro variable value surrounded by quotes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;... but I just wonder if there is any way to add single quote to this macro variable within a macro.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; has provided the answer.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 23:08:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703232#M215459</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-12-02T23:08:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to add single quote to a macro variable within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703234#M215461</link>
      <description>&lt;P&gt;awesome, this works well&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let city = nyc;
%macro y;
%let z= %sysfunc(quote(&amp;amp;city,"'"));
%let zz= %sysfunc(quote(&amp;amp;city,%str(%')));
%put &amp;amp;z &amp;amp;zz;
%mend;
%y;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;log below&amp;nbsp;&lt;/P&gt;&lt;P&gt;29 %let city = nyc;&lt;BR /&gt;30 %macro y;&lt;BR /&gt;31 %let z= %sysfunc(quote(&amp;amp;city,"'"));&lt;BR /&gt;32 %let zz= %sysfunc(quote(&amp;amp;city,%str(%')));&lt;BR /&gt;33 %put &amp;amp;z &amp;amp;zz;&lt;BR /&gt;34 %mend;&lt;BR /&gt;35 %y;&lt;BR /&gt;"nyc" 'nyc'&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 23:12:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703234#M215461</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2020-12-02T23:12:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to add single quote to a macro variable within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703236#M215462</link>
      <description>&lt;P&gt;Personally I find it cleaner and more flexible to not wrap macro variable values in quotes except where it is used:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO inv(table,city);
PROC SQL;
  CONNECT TO odbc(id=&amp;amp;id pwd=&amp;amp;pwd dsn=inv); 
  EXECUTE (delete from inventory.&amp;amp;TABLE where city = %str(%')&amp;amp;city%str(%') ) by odbc;
  disconnect from odbc;
QUIT;
&amp;lt;more code here&amp;gt;

%MEND;

%inv(Mytable, NYC); &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 23:41:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703236#M215462</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2020-12-02T23:41:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to add single quote to a macro variable within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703241#M215466</link>
      <description>&lt;P&gt;Great. This approach works really well. Thanks for the advice.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2020 00:25:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703241#M215466</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2020-12-03T00:25:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to add single quote to a macro variable within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703251#M215469</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/89004"&gt;@LL5&lt;/a&gt;&amp;nbsp; - Great, glad you like it. You are also being consistent with the way macro values in double quotes work as well.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2020 02:12:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703251#M215469</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2020-12-03T02:12:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to add single quote to a macro variable within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703354#M215488</link>
      <description>%let city = nyc;&lt;BR /&gt;%macro y;&lt;BR /&gt;%let z= %bquote(' &amp;amp;city ') ;&lt;BR /&gt;%let zz=%bquote(' &amp;amp;city ') ;&lt;BR /&gt;%put &amp;amp;z &amp;amp;zz;&lt;BR /&gt;%mend;&lt;BR /&gt;%y</description>
      <pubDate>Thu, 03 Dec 2020 12:48:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703354#M215488</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-12-03T12:48:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to add single quote to a macro variable within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703378#M215499</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13976"&gt;@SASKiwi&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/89004"&gt;@LL5&lt;/a&gt;&amp;nbsp; - Great, glad you like it. You are also being consistent with the way macro values in double quotes work as well.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Depends on how much trust you have that the macro variable values do not contain quotes already.&amp;nbsp; That is the added value that using the QUOTE function provides.&amp;nbsp; Note that if you are passing the string to some foreign system then you need to know whether that system follows SAS's rules for nested quotes or uses some other syntax. For example the Unix command shell wants you use the \ to escape embedded quotes instead of doubling them.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2020 14:27:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/703378#M215499</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-03T14:27:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to add single quote to a macro variable within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/822779#M324885</link>
      <description>Data _null_&lt;BR /&gt;Call symput ('Month',put(intnx('month',today(),-2,'B'),monname.));&lt;BR /&gt;%put &amp;amp;month1. "22&lt;BR /&gt;While resolving macro variable month1 i want "22 ( single quite and 22 ) at the end of macro variable how i can add ?</description>
      <pubDate>Tue, 12 Jul 2022 08:54:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/822779#M324885</guid>
      <dc:creator>Aniruddhaanu</dc:creator>
      <dc:date>2022-07-12T08:54:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to add single quote to a macro variable within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/822809#M324896</link>
      <description>Plz start a brand-new session, this topic is almost two years ago .&lt;BR /&gt;And you could let other sas experts know this question and get better answer .</description>
      <pubDate>Tue, 12 Jul 2022 12:03:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-single-quote-to-a-macro-variable-within-a-macro/m-p/822809#M324896</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-07-12T12:03:43Z</dc:date>
    </item>
  </channel>
</rss>

