<?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 how to use a looping to create a subset to feed in a macro? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-a-looping-to-create-a-subset-to-feed-in-a-macro/m-p/917239#M361313</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have a dataset which has 3 level of 'Subject' and 5 levels of 'test'. I would like to create a looping code to create summary tables by utilize&amp;nbsp; an exisiting macro 'get_sum'. How can I do that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am thinking of sth like below:&lt;/P&gt;
&lt;P&gt;do i=1 to 3;&lt;/P&gt;
&lt;P&gt;do j=1 to 5;&lt;/P&gt;
&lt;P&gt;%get_sum(indata=have; subset=subject eq i and test eq j);&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;I am new to sas and not sure how to handle looping situation. Could anyone guide me on this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 21 Feb 2024 20:39:47 GMT</pubDate>
    <dc:creator>stataq</dc:creator>
    <dc:date>2024-02-21T20:39:47Z</dc:date>
    <item>
      <title>how to use a looping to create a subset to feed in a macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-a-looping-to-create-a-subset-to-feed-in-a-macro/m-p/917239#M361313</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have a dataset which has 3 level of 'Subject' and 5 levels of 'test'. I would like to create a looping code to create summary tables by utilize&amp;nbsp; an exisiting macro 'get_sum'. How can I do that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am thinking of sth like below:&lt;/P&gt;
&lt;P&gt;do i=1 to 3;&lt;/P&gt;
&lt;P&gt;do j=1 to 5;&lt;/P&gt;
&lt;P&gt;%get_sum(indata=have; subset=subject eq i and test eq j);&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;I am new to sas and not sure how to handle looping situation. Could anyone guide me on this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2024 20:39:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-a-looping-to-create-a-subset-to-feed-in-a-macro/m-p/917239#M361313</guid>
      <dc:creator>stataq</dc:creator>
      <dc:date>2024-02-21T20:39:47Z</dc:date>
    </item>
    <item>
      <title>Re: how to use a looping to create a subset to feed in a macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-a-looping-to-create-a-subset-to-feed-in-a-macro/m-p/917255#M361317</link>
      <description>&lt;P&gt;How about providing an example of the data you have, or at least a workable dummy, and what you expect as a result for that.&lt;/P&gt;
&lt;P&gt;As a minimum you should include the code for the "get_sum" macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Quite often macro code is not needed at all and when appropriate looping may not be needed either.&lt;/P&gt;
&lt;P&gt;Typically in SAS a report based on levels such as "as subject identifier" would use BY group processing to use each level.&lt;/P&gt;
&lt;P&gt;Depending on exactly what a "summary table" needs to look like this might be better done by Proc Tabulate or Report than some not-yet-defined macro.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2024 21:29:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-a-looping-to-create-a-subset-to-feed-in-a-macro/m-p/917255#M361317</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-02-21T21:29:30Z</dc:date>
    </item>
    <item>
      <title>Re: how to use a looping to create a subset to feed in a macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-a-looping-to-create-a-subset-to-feed-in-a-macro/m-p/917328#M361352</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/448857"&gt;@stataq&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have a dataset which has 3 level of 'Subject' and 5 levels of 'test'. I would like to create a looping code to create summary tables by utilize&amp;nbsp; an exisiting macro 'get_sum'.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Don't split the data. Use a BY statement in PROC MEANS or PROC SUMMARY to get the sums.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2024 10:34:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-a-looping-to-create-a-subset-to-feed-in-a-macro/m-p/917328#M361352</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-02-22T10:34:22Z</dc:date>
    </item>
    <item>
      <title>Re: how to use a looping to create a subset to feed in a macro?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-a-looping-to-create-a-subset-to-feed-in-a-macro/m-p/917330#M361354</link>
      <description>&lt;P&gt;You didn't provide the data and macro code, that's why I'm doing some assumptions:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* assuming this is your data */
data have;
  do subject=1 to 3;
    do test=1 to 5;
      do value=1 to sum(subject+test);
        output;
      end;
    end;
  end;
run;

/* and assuming this (looking at name) is your macro */
%macro get_sum(indata=have, subset=1, result=want);
proc sql;
create table &amp;amp;result. as
select 
 subject, test, sum(value) as sumof_value
from 
  &amp;amp;indata.
where
  (&amp;amp;subset.)
;
quit;
%mend get_sum;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Having those I guess you want to do something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  do i=1 to 3;
    do j=1 to 5;
      call execute(
        '%nrstr(%get_sum(indata=have,' !!
         catx(" ",'subset=subject eq ', i, 'and test eq ', j, ',') !!
         'result=want_'!!catx("_",i,j) !!
         '))'
      );
    end;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But! As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;already mentioned (and I 100% agree) it would be much wiser to use "by group processing".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In case SQL it would be something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select 
 subject, test, sum(value) as sumof_value
from 
  have

  GROUP BY
   subject, test
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;with GROUP BY clause.&amp;nbsp; And for statistical procedures something like BY clause, or CLASS clause (if there is one).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2024 10:52:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-a-looping-to-create-a-subset-to-feed-in-a-macro/m-p/917330#M361354</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-02-22T10:52:08Z</dc:date>
    </item>
  </channel>
</rss>

