<?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 Macro in Do Loop Where Assignments are Sets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-Macro-in-Do-Loop-Where-Assignments-are-Sets/m-p/433615#M107521</link>
    <description>&lt;P&gt;I see three serious issues and one minor:&lt;/P&gt;
&lt;P&gt;Proc SQL;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; Create table work.rst_&amp;amp;k. as&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Select&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.field&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color="#ff0000"&gt;&amp;lt;missing comma&lt;/FONT&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum(a.abc) as W,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum(a.bcd) as Z,&amp;nbsp; &lt;FONT color="#ff0000"&gt;&amp;lt; This comma should not be here as 'from' is now treated as a variable to select&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;From&amp;nbsp; work.data1 a, work.data2&amp;nbsp;b, &lt;FONT color="#ff0000"&gt;&amp;lt; This comma should not be here as where is going to be treated as dataset&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Where&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.field1 = b.field1 and&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.field2 = b.field2&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.field3 in set_&amp;amp;k.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Group by a.year&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Order by a.year&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; %End; &lt;FONT color="#3366ff"&gt;&amp;lt; this is&amp;nbsp;a minor issue, I don't see a real reason not to use&amp;nbsp; " quit; %end;" to generate complete&amp;nbsp;Proc SQL calls inside the loop.&amp;nbsp;&lt;/FONT&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Obviously we do not know how big your two data sets are that you use in your from statement but the number of full Cartesian joins you may do can get very expensive in time for repeated actions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this were my data I don't think I would need a macro at all as long as none of your Set_x lists over lap. I would do something to assign a LIST value with one pass through the data. Depending on how you currently have your list information that could be used to make a format which could be used with a : put(a.field3,mylistformat) as ListId&lt;/P&gt;
&lt;P&gt;Something like (note: no macro)&lt;/P&gt;
&lt;PRE&gt;proc format library=work;
value mylistid
123, 234, 345 = '1'
456, 567, 678 = '2'
789, 890, 012 = '3'&lt;BR /&gt;other = '0' /* or what might make sense*/
;
run;
Proc SQL;
   Create table work.rst_lists as
      Select
         a.field,
         sum(a.abc) as W,
         sum(a.bcd) as Z,
         put(a.field3,mylistid.) as ListId
      From  work.data1 a, work.data2 b,

      Where
         a.field1 = b.field1 and
         a.field2 = b.field2
         
      Group by a.year
      Order by a.year
   ;
 quit;
&lt;/PRE&gt;
&lt;P&gt;You might add Listid to the order&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 02 Feb 2018 17:35:48 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2018-02-02T17:35:48Z</dc:date>
    <item>
      <title>Using Macro in Do Loop Where Assignments are Sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Macro-in-Do-Loop-Where-Assignments-are-Sets/m-p/433603#M107513</link>
      <description>&lt;P&gt;Hello.&amp;nbsp; This is the first time I'm posting a question and am a rank beginner with SAS 9.4, so I hope this doesn't sound stupid.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to use a macro where the variable assignments are sets of numbers in a do loop.&amp;nbsp; So, for example, I'd have&lt;/P&gt;&lt;P&gt;%let set_1 = (123, 234, 345);&lt;/P&gt;&lt;P&gt;%let set_2 = (456, 567, 678);&lt;/P&gt;&lt;P&gt;%let set_3 = (789, 890, 012);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then a proc sql statement like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro loop(Start,End);&lt;BR /&gt;&amp;nbsp; %DO k=&amp;amp;Start. %TO &amp;amp;End.;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; Proc SQL;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; Create table work.rst_&amp;amp;k. as&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Select&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.field&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum(a.abc) as W,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum(a.bcd) as Z,&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;From&amp;nbsp; work.data1 a, work.data2&amp;nbsp;b,&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Where&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.field1 = b.field1 and&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.field2 = b.field2&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.field3 in set_&amp;amp;k.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Group by a.year&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Order by a.year&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; %End;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; quit;&lt;BR /&gt;%mend;&lt;BR /&gt;%loop (Start=1, End=3)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The set_x are much larger than displayed here, and there are far more sets.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The way I did it originally was to do a separate SQL step for each set_x, and that worked, but I was hoping I could streamline the code a bit.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The error I'm getting with the Do Loop is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;WARNING: Apparent symbolic reference Set_ not resolved.&lt;BR /&gt;NOTE: Line generated by the invoked macro "LOOP".&lt;BR /&gt;3 &amp;amp;set_&amp;amp;k.&amp;nbsp;&lt;/P&gt;&lt;P&gt;-&lt;/P&gt;&lt;P&gt;22&lt;/P&gt;&lt;P&gt;76&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: (, SELECT.&lt;/P&gt;&lt;P&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for any help you are willing to provide and please advise if I put this request in the wrong place.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 16:54:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Macro-in-Do-Loop-Where-Assignments-are-Sets/m-p/433603#M107513</guid>
      <dc:creator>SOF1_CWoPA</dc:creator>
      <dc:date>2018-02-02T16:54:33Z</dc:date>
    </item>
    <item>
      <title>Re: Using Macro in Do Loop Where Assignments are Sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Macro-in-Do-Loop-Where-Assignments-are-Sets/m-p/433615#M107521</link>
      <description>&lt;P&gt;I see three serious issues and one minor:&lt;/P&gt;
&lt;P&gt;Proc SQL;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; Create table work.rst_&amp;amp;k. as&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Select&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.field&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color="#ff0000"&gt;&amp;lt;missing comma&lt;/FONT&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum(a.abc) as W,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum(a.bcd) as Z,&amp;nbsp; &lt;FONT color="#ff0000"&gt;&amp;lt; This comma should not be here as 'from' is now treated as a variable to select&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;From&amp;nbsp; work.data1 a, work.data2&amp;nbsp;b, &lt;FONT color="#ff0000"&gt;&amp;lt; This comma should not be here as where is going to be treated as dataset&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Where&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.field1 = b.field1 and&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.field2 = b.field2&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.field3 in set_&amp;amp;k.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Group by a.year&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Order by a.year&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; %End; &lt;FONT color="#3366ff"&gt;&amp;lt; this is&amp;nbsp;a minor issue, I don't see a real reason not to use&amp;nbsp; " quit; %end;" to generate complete&amp;nbsp;Proc SQL calls inside the loop.&amp;nbsp;&lt;/FONT&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Obviously we do not know how big your two data sets are that you use in your from statement but the number of full Cartesian joins you may do can get very expensive in time for repeated actions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this were my data I don't think I would need a macro at all as long as none of your Set_x lists over lap. I would do something to assign a LIST value with one pass through the data. Depending on how you currently have your list information that could be used to make a format which could be used with a : put(a.field3,mylistformat) as ListId&lt;/P&gt;
&lt;P&gt;Something like (note: no macro)&lt;/P&gt;
&lt;PRE&gt;proc format library=work;
value mylistid
123, 234, 345 = '1'
456, 567, 678 = '2'
789, 890, 012 = '3'&lt;BR /&gt;other = '0' /* or what might make sense*/
;
run;
Proc SQL;
   Create table work.rst_lists as
      Select
         a.field,
         sum(a.abc) as W,
         sum(a.bcd) as Z,
         put(a.field3,mylistid.) as ListId
      From  work.data1 a, work.data2 b,

      Where
         a.field1 = b.field1 and
         a.field2 = b.field2
         
      Group by a.year
      Order by a.year
   ;
 quit;
&lt;/PRE&gt;
&lt;P&gt;You might add Listid to the order&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 17:35:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Macro-in-Do-Loop-Where-Assignments-are-Sets/m-p/433615#M107521</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-02-02T17:35:48Z</dc:date>
    </item>
    <item>
      <title>Re: Using Macro in Do Loop Where Assignments are Sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Macro-in-Do-Loop-Where-Assignments-are-Sets/m-p/433623#M107524</link>
      <description>&lt;P&gt;Many thanks for the reply and recommendations!!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Will this generate a separate data set in the work library for each of '1', '2', ...?&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 17:43:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Macro-in-Do-Loop-Where-Assignments-are-Sets/m-p/433623#M107524</guid>
      <dc:creator>SOF1_CWoPA</dc:creator>
      <dc:date>2018-02-02T17:43:27Z</dc:date>
    </item>
    <item>
      <title>Re: Using Macro in Do Loop Where Assignments are Sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Macro-in-Do-Loop-Where-Assignments-are-Sets/m-p/433627#M107527</link>
      <description>&lt;P&gt;To indirectly address macro variables, use a double ampersand:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Where
            a.field1 = b.field1 and
            a.field2 = b.field2

            a.field3 in &amp;amp;&amp;amp;set_&amp;amp;k.

 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Feb 2018 17:46:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Macro-in-Do-Loop-Where-Assignments-are-Sets/m-p/433627#M107527</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-02-02T17:46:23Z</dc:date>
    </item>
    <item>
      <title>Re: Using Macro in Do Loop Where Assignments are Sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Macro-in-Do-Loop-Where-Assignments-are-Sets/m-p/433635#M107531</link>
      <description>&lt;P&gt;That's perfect!!&amp;nbsp; Thank you so much!!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have a great weekend everyone.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 18:04:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Macro-in-Do-Loop-Where-Assignments-are-Sets/m-p/433635#M107531</guid>
      <dc:creator>SOF1_CWoPA</dc:creator>
      <dc:date>2018-02-02T18:04:17Z</dc:date>
    </item>
  </channel>
</rss>

