<?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: Utilize macro variable within WHERE clause in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Utilize-macro-variable-within-WHERE-clause/m-p/126595#M25785</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It's because your values are recognized as code (and thus expects a name variable) instead of as the string their string representation. Macro variables make no distinction of variable types, it's all (or at least mostly) just text parsing.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You simply need to change&lt;/P&gt;&lt;P&gt;where state_ab = &amp;amp;st;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;to&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;where state_ab = "&amp;amp;st";&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Make sure you use double quotes as sigle quotes prevent dereferencing macro variables (treat the ampersand as text and not macro code components)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also, not that it is a necessary fix but you could've used&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%do i=1 %to 7;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; block&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;to avoid having to handle the increment yourself&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Vincent&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 30 Aug 2013 19:01:45 GMT</pubDate>
    <dc:creator>Vince28_Statcan</dc:creator>
    <dc:date>2013-08-30T19:01:45Z</dc:date>
    <item>
      <title>Utilize macro variable within WHERE clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Utilize-macro-variable-within-WHERE-clause/m-p/126594#M25784</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Howdy, new to SAS (using a couple weeks) and am currently trying to create 1 state table per state and have the state abbreviation be used in the WHERE clause to only insert records for that state. I have tried MANY incarnations of this code so far and believe I'm close. Seems I need to quote my variable and like I said I've tried doing that many ways. Currently receiving the error on all iterations, with the abbreviation changing:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where TABLE1.STATE_AB = DE;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;ERROR: The following columns were not found in the contributing tables: DE.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%LET stateab= DE PA MI MN MA WI NY; &lt;/P&gt;&lt;P&gt;%createstatetables&lt;/P&gt;&lt;P&gt;%MACRO createstatetables; &lt;/P&gt;&lt;P&gt;%LET i =1 ; &lt;/P&gt;&lt;P&gt;%DO %UNTIL(&amp;amp;i = &lt;span class="lia-unicode-emoji" title=":smiling_face_with_sunglasses:"&gt;😎&lt;/span&gt; ; &lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&amp;nbsp; proc sql feedback;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&amp;nbsp; %LET st = %SCAN(&amp;amp;stateab,&amp;amp;i);&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&amp;nbsp; create table &amp;amp;st as&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&amp;nbsp; select *&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&amp;nbsp; from testdata.table1 where state_ab = &amp;amp;st;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&amp;nbsp; RUN; &lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&amp;nbsp; %LET i=%EVAL(&amp;amp;i+1); &lt;/P&gt;&lt;P&gt;%END; &lt;/P&gt;&lt;P&gt;%MEND createstatetables;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thank you in advance for any insight&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 30 Aug 2013 18:47:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Utilize-macro-variable-within-WHERE-clause/m-p/126594#M25784</guid>
      <dc:creator>80sMetalForever</dc:creator>
      <dc:date>2013-08-30T18:47:08Z</dc:date>
    </item>
    <item>
      <title>Re: Utilize macro variable within WHERE clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Utilize-macro-variable-within-WHERE-clause/m-p/126595#M25785</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It's because your values are recognized as code (and thus expects a name variable) instead of as the string their string representation. Macro variables make no distinction of variable types, it's all (or at least mostly) just text parsing.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You simply need to change&lt;/P&gt;&lt;P&gt;where state_ab = &amp;amp;st;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;to&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;where state_ab = "&amp;amp;st";&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Make sure you use double quotes as sigle quotes prevent dereferencing macro variables (treat the ampersand as text and not macro code components)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also, not that it is a necessary fix but you could've used&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%do i=1 %to 7;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; block&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;to avoid having to handle the increment yourself&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Vincent&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 30 Aug 2013 19:01:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Utilize-macro-variable-within-WHERE-clause/m-p/126595#M25785</guid>
      <dc:creator>Vince28_Statcan</dc:creator>
      <dc:date>2013-08-30T19:01:45Z</dc:date>
    </item>
    <item>
      <title>Re: Utilize macro variable within WHERE clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Utilize-macro-variable-within-WHERE-clause/m-p/126596#M25786</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You'd still need to know the number of values in the array. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Preferably something along the lines of:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%do %while (&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;%SCAN(&amp;amp;stateab,&amp;amp;i) ne %str());&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Probably a bit over your head, but generally this isn't recommended. SAS has BY group processing that is super effective and usually negates the need to separate by variables. &lt;/P&gt;&lt;P&gt;Here's a good but technical read on the different methods and at the bottom is a link to a generic sql solution.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A class="active_link" href="http://www.sascommunity.org/wiki/Split_Data_into_Subsets" title="http://www.sascommunity.org/wiki/Split_Data_into_Subsets"&gt;http://www.sascommunity.org/wiki/Split_Data_into_Subsets&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 30 Aug 2013 20:37:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Utilize-macro-variable-within-WHERE-clause/m-p/126596#M25786</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2013-08-30T20:37:17Z</dc:date>
    </item>
    <item>
      <title>Re: Utilize macro variable within WHERE clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Utilize-macro-variable-within-WHERE-clause/m-p/126597#M25787</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you, I didn't try that as I was certain it wouldn't work LOL. It worked correctly and I've updated the loop to your more 'elegant' version.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for the link Reeza, I will check it out.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 03 Sep 2013 13:01:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Utilize-macro-variable-within-WHERE-clause/m-p/126597#M25787</guid>
      <dc:creator>80sMetalForever</dc:creator>
      <dc:date>2013-09-03T13:01:20Z</dc:date>
    </item>
    <item>
      <title>Re: Utilize macro variable within WHERE clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Utilize-macro-variable-within-WHERE-clause/m-p/126598#M25788</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I was wondering about the term&lt;/P&gt;&lt;P&gt;single quote &lt;STRONG&gt;" prevent dereferencing"..............&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;did u mean single quote "prevent referencing"?????&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Thanks&lt;/STRONG&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 03 Sep 2013 15:06:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Utilize-macro-variable-within-WHERE-clause/m-p/126598#M25788</guid>
      <dc:creator>robertrao</dc:creator>
      <dc:date>2013-09-03T15:06:33Z</dc:date>
    </item>
    <item>
      <title>Re: Utilize macro variable within WHERE clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Utilize-macro-variable-within-WHERE-clause/m-p/126599#M25789</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Try the experiment.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let myvar=Macro Variable value;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%put '&amp;amp;myvar';&lt;/P&gt;&lt;P&gt;%put "&amp;amp;myvar";&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 03 Sep 2013 15:28:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Utilize-macro-variable-within-WHERE-clause/m-p/126599#M25789</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2013-09-03T15:28:34Z</dc:date>
    </item>
    <item>
      <title>Re: Utilize macro variable within WHERE clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Utilize-macro-variable-within-WHERE-clause/m-p/126600#M25790</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;Thanks so much...&lt;/P&gt;&lt;P&gt;Importantly, a couple of days ago i read your response to one of the questions on macro on how to use braces....&lt;/P&gt;&lt;P&gt;I could not find it now .could you help me locate the question and the responses???/&lt;/P&gt;&lt;P&gt;It uses the QUOTE function!!!!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 03 Sep 2013 17:19:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Utilize-macro-variable-within-WHERE-clause/m-p/126600#M25790</guid>
      <dc:creator>robertrao</dc:creator>
      <dc:date>2013-09-03T17:19:08Z</dc:date>
    </item>
  </channel>
</rss>

