<?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: Using a macro for a character number. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-for-a-character-number/m-p/853482#M337340</link>
    <description>&lt;P&gt;In a large percentage of cases it is better not to make quotes part of the macro variable at all. Use double quotes around a macro variable to allow it to resolve:&lt;/P&gt;
&lt;PRE&gt;%let period = 202212;

proc sql noerrorstop;
    delete from core.mytable where ym_id= "&amp;amp;period.";
quit;

&lt;/PRE&gt;
&lt;P&gt;That means for the places where &amp;amp;period may not want quotes you don't need another variable to have that available.&lt;/P&gt;
&lt;P&gt;With that said if period 202212 is supposed be December of 2022 then you may want to rethink entirely how that is stored. That really should be a date value and character values holding dates almost always require using date values for manipulation unless it is trivial. For example what if you want to represent a duration of 3 sequential months?&lt;/P&gt;
&lt;P&gt;You can't use something like&lt;/P&gt;
&lt;PRE&gt;ym_id in ("&amp;amp;period" "%eval(&amp;amp;period. +1)" "%eval(&amp;amp;period. +2")  )&lt;/PRE&gt;
&lt;P&gt;because +1 would be 202213, not the desired 202301.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 12 Jan 2023 16:04:07 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-01-12T16:04:07Z</dc:date>
    <item>
      <title>Using a macro for a character number.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-for-a-character-number/m-p/853475#M337335</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;Sorry for the confusing title -- not sure how to phrase it.&lt;/P&gt;&lt;P&gt;In the code below, I'm looking into a Netezza table (Data step won't work) and deleting current period records if they exist. The issue is, how to represent the value of 202212 as ym_id is a character/text field (don't ask me, I didn't create the database).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;If I hard-code '202212' then it works&lt;/LI&gt;&lt;LI&gt;I need to keep &amp;amp;period as 202212 since it's used throughout the program.&lt;/LI&gt;&lt;LI&gt;If I use &amp;amp;period then I get&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;ERROR: Expression using equals (=) has components that are of different data types.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/LI&gt;&lt;LI&gt;If I use &amp;amp;qperiod it resolves to '202212' but then I get the error&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;&amp;nbsp;ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant,&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;&lt;EM&gt;&lt;STRONG&gt;a missing value, (, *, +, -, ALL, ANY, BTRIM, CALCULATED, CASE, INPUT, PUT, SELECT, SOME, SUBSTRING, TRANSLATE,&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;&lt;EM&gt;&lt;STRONG&gt;USER&lt;/STRONG&gt;&lt;/EM&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Would anyone help me please with this macro issue?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/******** CODE IN QUESTION **************/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let period = 202212;&lt;/P&gt;&lt;P&gt;%let qperiod = %str(')&amp;amp;period%str(');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql noerrorstop;&lt;/P&gt;&lt;P&gt;delete from core.mytable where ym_id = &amp;amp;period;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jan 2023 15:44:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-for-a-character-number/m-p/853475#M337335</guid>
      <dc:creator>SASGeek</dc:creator>
      <dc:date>2023-01-12T15:44:53Z</dc:date>
    </item>
    <item>
      <title>Re: Using a macro for a character number.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-for-a-character-number/m-p/853478#M337337</link>
      <description>&lt;P&gt;You can't have imbalanced quotes inside %STR() See&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p0pnc7p9n4h6g5n16g6js048nhfl.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p0pnc7p9n4h6g5n16g6js048nhfl.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can have imbalanced quotes if you use the % operator inside %STR()&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let qperiod = %str(%')&amp;amp;period%str(%');
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Jan 2023 15:49:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-for-a-character-number/m-p/853478#M337337</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-01-12T15:49:16Z</dc:date>
    </item>
    <item>
      <title>Re: Using a macro for a character number.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-for-a-character-number/m-p/853481#M337339</link>
      <description>&lt;P&gt;The simple solution is to put the quotes into the macro variable to begin with:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let period = '202212';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you are building the macro variable with SAS code use the QUOTE function to add the single quotes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  call symputx('period',quote(put(today(),yymmn6.),"'");
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really really have to have the macro variable without quotes then use the %BQUOTE() function to add them.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let period=202212;
... 
where ym_id = %bquote('&amp;amp;period')
...
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;1894  %let period=202212;
1895  %put
1896  where ym_id = %bquote('&amp;amp;period')
1897  ;
where ym_id = '202212'
&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Jan 2023 15:58:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-for-a-character-number/m-p/853481#M337339</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-12T15:58:40Z</dc:date>
    </item>
    <item>
      <title>Re: Using a macro for a character number.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-for-a-character-number/m-p/853482#M337340</link>
      <description>&lt;P&gt;In a large percentage of cases it is better not to make quotes part of the macro variable at all. Use double quotes around a macro variable to allow it to resolve:&lt;/P&gt;
&lt;PRE&gt;%let period = 202212;

proc sql noerrorstop;
    delete from core.mytable where ym_id= "&amp;amp;period.";
quit;

&lt;/PRE&gt;
&lt;P&gt;That means for the places where &amp;amp;period may not want quotes you don't need another variable to have that available.&lt;/P&gt;
&lt;P&gt;With that said if period 202212 is supposed be December of 2022 then you may want to rethink entirely how that is stored. That really should be a date value and character values holding dates almost always require using date values for manipulation unless it is trivial. For example what if you want to represent a duration of 3 sequential months?&lt;/P&gt;
&lt;P&gt;You can't use something like&lt;/P&gt;
&lt;PRE&gt;ym_id in ("&amp;amp;period" "%eval(&amp;amp;period. +1)" "%eval(&amp;amp;period. +2")  )&lt;/PRE&gt;
&lt;P&gt;because +1 would be 202213, not the desired 202301.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jan 2023 16:04:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-for-a-character-number/m-p/853482#M337340</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-01-12T16:04:07Z</dc:date>
    </item>
    <item>
      <title>Re: Using a macro for a character number.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-for-a-character-number/m-p/853521#M337366</link>
      <description>&lt;P&gt;There are cases within SQL where a quoted quote causes a problem.&amp;nbsp; If all the above suggestions are still not giving you a solution, try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let qperiod = %unquote(%str(%')&amp;amp;period%str(%'));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and be sure to use &amp;amp;qperiod where needed, not &amp;amp;period,.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jan 2023 17:47:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-macro-for-a-character-number/m-p/853521#M337366</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-01-12T17:47:28Z</dc:date>
    </item>
  </channel>
</rss>

