<?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 Array do loop through a subset of condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246825#M309396</link>
    <description>&lt;P&gt;I have the following code which loop through all possible combination of 3 factors&lt;BR /&gt;data have;&lt;BR /&gt;input a1 a2 a3 a4 win_or_loss ;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 1 1 1 1&lt;BR /&gt;1 1 1 1 -1 2&lt;BR /&gt;1 1 0 0 1 3&lt;BR /&gt;2 1 0 0 1 4&lt;BR /&gt;2 0 2 1 -1 5&lt;BR /&gt;2 0 2 1 1 6&lt;BR /&gt;2 0 3 1 -1 7&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data want&amp;nbsp;(keep=a_: b_: c_: win_or_loss );&lt;BR /&gt;SET have;&lt;BR /&gt;array a{*} a1 a2 a3 a4;&lt;BR /&gt;array b{*} a1 a2 a3 a4;&lt;BR /&gt;array c{*} a1 a2 a3 a4;&lt;BR /&gt; &lt;BR /&gt;do i=1 to dim(a);&lt;BR /&gt;do j=i+1 to dim(b); &lt;BR /&gt;do k=j+1 to dim(c); &lt;BR /&gt;&amp;nbsp; &amp;nbsp; a_name=vname(a{i}); &lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;b_name=vname(b{j});&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;c_name=vname(c{k});&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;a_value=a{i}; &lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;b_value=b{j};&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;c_value=c{k};&lt;BR /&gt;output ww;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;end; &lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;I want is to limit the first Do loop within a subset of value say (a1= 1 , a1=2 a2=1 , a2=0, a3=2) &lt;/STRONG&gt;&lt;BR /&gt;(with the setup like that, I know there will be some duplicate say (a1=1 a1=1 a2=2) but it is ok)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want(keep=a_: b_: c_: win_or_loss ID);&lt;BR /&gt; SET have;&lt;/P&gt;
&lt;P&gt;array a{*} a1 a2 a3 a4;&lt;BR /&gt;array b{*}a1 a2 a3 a4;&lt;BR /&gt;array c{*}a1 a2 a3 a4;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;do (a1= 1 , a1=2 a2=1 , a2=0, a3=2) ; *of course instead of a1=1,2,3.., a2=1,2,3... a3=1,2,3..;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt; do j=1 to dim(b); *This run full; &lt;BR /&gt; do k=j+1 to dim(c); *this run full;&lt;BR /&gt; a_name=vname(a{i}); &lt;BR /&gt; b_name=vname(b{j});&lt;BR /&gt; c_name=vname(c{k});&lt;/P&gt;
&lt;P&gt;a_value=a{i}; &lt;BR /&gt; b_value=b{j};&lt;BR /&gt; c_value=c{k};&lt;/P&gt;
&lt;P&gt;output ww;&lt;BR /&gt; end;&lt;BR /&gt;end;&lt;BR /&gt;end; &lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I appreciate it if you could help me out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
    <pubDate>Fri, 29 Jan 2016 03:23:16 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2016-01-29T03:23:16Z</dc:date>
    <item>
      <title>Array do loop through a subset of condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246825#M309396</link>
      <description>&lt;P&gt;I have the following code which loop through all possible combination of 3 factors&lt;BR /&gt;data have;&lt;BR /&gt;input a1 a2 a3 a4 win_or_loss ;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 1 1 1 1&lt;BR /&gt;1 1 1 1 -1 2&lt;BR /&gt;1 1 0 0 1 3&lt;BR /&gt;2 1 0 0 1 4&lt;BR /&gt;2 0 2 1 -1 5&lt;BR /&gt;2 0 2 1 1 6&lt;BR /&gt;2 0 3 1 -1 7&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data want&amp;nbsp;(keep=a_: b_: c_: win_or_loss );&lt;BR /&gt;SET have;&lt;BR /&gt;array a{*} a1 a2 a3 a4;&lt;BR /&gt;array b{*} a1 a2 a3 a4;&lt;BR /&gt;array c{*} a1 a2 a3 a4;&lt;BR /&gt; &lt;BR /&gt;do i=1 to dim(a);&lt;BR /&gt;do j=i+1 to dim(b); &lt;BR /&gt;do k=j+1 to dim(c); &lt;BR /&gt;&amp;nbsp; &amp;nbsp; a_name=vname(a{i}); &lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;b_name=vname(b{j});&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;c_name=vname(c{k});&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;a_value=a{i}; &lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;b_value=b{j};&lt;BR /&gt; &lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;c_value=c{k};&lt;BR /&gt;output ww;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;end; &lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;I want is to limit the first Do loop within a subset of value say (a1= 1 , a1=2 a2=1 , a2=0, a3=2) &lt;/STRONG&gt;&lt;BR /&gt;(with the setup like that, I know there will be some duplicate say (a1=1 a1=1 a2=2) but it is ok)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want(keep=a_: b_: c_: win_or_loss ID);&lt;BR /&gt; SET have;&lt;/P&gt;
&lt;P&gt;array a{*} a1 a2 a3 a4;&lt;BR /&gt;array b{*}a1 a2 a3 a4;&lt;BR /&gt;array c{*}a1 a2 a3 a4;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;do (a1= 1 , a1=2 a2=1 , a2=0, a3=2) ; *of course instead of a1=1,2,3.., a2=1,2,3... a3=1,2,3..;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt; do j=1 to dim(b); *This run full; &lt;BR /&gt; do k=j+1 to dim(c); *this run full;&lt;BR /&gt; a_name=vname(a{i}); &lt;BR /&gt; b_name=vname(b{j});&lt;BR /&gt; c_name=vname(c{k});&lt;/P&gt;
&lt;P&gt;a_value=a{i}; &lt;BR /&gt; b_value=b{j};&lt;BR /&gt; c_value=c{k};&lt;/P&gt;
&lt;P&gt;output ww;&lt;BR /&gt; end;&lt;BR /&gt;end;&lt;BR /&gt;end; &lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I appreciate it if you could help me out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 03:23:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246825#M309396</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2016-01-29T03:23:16Z</dc:date>
    </item>
    <item>
      <title>Re: Array do loop through a subset of condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246826#M309397</link>
      <description>&lt;P&gt;Ouch. Does it have to be arrays?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe proc freq/tabulate are better options? If reporting only, then tabulate definitely, but freq gets you pretty close, just needs a transpose.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have noprint;
table a1*a2*a3*a4*win_or_loss/list out=summary;
run;

proc transpose data=summary out=want1 prefix=Score;
by a1 a2 a3 a4;
var count;
id win_or_loss;
run;

/*report only*/
proc tabulate data=have;
class a1-a4 win_or_loss;
table a1*a2*a3*a4, win_or_loss*N="Count"/misstext='0';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Jan 2016 03:25:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246826#M309397</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-29T03:25:15Z</dc:date>
    </item>
    <item>
      <title>Re: Array do loop through a subset of condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246827#M309398</link>
      <description>&lt;P&gt;I really need it to be array. The other method I try before doesnt work because of the size of the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 03:27:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246827#M309398</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2016-01-29T03:27:29Z</dc:date>
    </item>
    <item>
      <title>Re: Array do loop through a subset of condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246828#M309399</link>
      <description>&lt;P&gt;&lt;img id="womansad" class="emoticon emoticon-womansad" src="https://communities.sas.com/i/smilies/16x16_woman-sad.png" alt="Woman Sad" title="Woman Sad" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's too bad. Since it's counts they're&amp;nbsp;cumulative, so you could split it up and process...Someone will probably have a hash solution that's better, but I'm not good at that. Hope you find something that works better.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 03:29:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246828#M309399</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-29T03:29:23Z</dc:date>
    </item>
    <item>
      <title>Re: Array do loop through a subset of condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246831#M309400</link>
      <description>&lt;P&gt;A few things stand out about the program. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you need 3 arrays? &amp;nbsp;They contain the same elements, so one array should be plenty.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The way you have coded the DO loops, you are not getting every combination. &amp;nbsp;You are getting more than that,. &amp;nbsp;WIth 4 elements in the array, it would be sufficient to code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do i=1 to 2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;do j=i+1 to 3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; do k=j+1 to 4;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is just to illustrate ... you don't have to hard-code the stopping values of the loops. &amp;nbsp;You could always make them depend on dim(a).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The easiest way to control which combinations you want in all combination problems (at least in my opinion) is to turn the OUTPUT statement into an IF/THEN/OUTPUT statement. &amp;nbsp;Run through all the combinations, but output only the ones that you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not sure I hit on everything you need here, but I hope we're moving in the right direction.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 03:48:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246831#M309400</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-01-29T03:48:58Z</dc:date>
    </item>
    <item>
      <title>Re: Array do loop through a subset of condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246832#M309401</link>
      <description>&lt;P&gt;I agree that I need to declare 1 array as below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data ww1 (keep=a_: b_: c_: win_or_loss );&lt;BR /&gt;SET have;&lt;BR /&gt;array a{*} a1 a2 a3 a4;&lt;BR /&gt; &lt;BR /&gt;do i=1 to dim(a);&lt;BR /&gt;do j=i+1 to dim(a); &lt;BR /&gt;do k=j+1 to dim(a); &lt;BR /&gt; a_name=vname(a{i}); &lt;BR /&gt; b_name=vname(a{j});&lt;BR /&gt; c_name=vname(a{k});&lt;BR /&gt; a_value=a{i}; &lt;BR /&gt; b_value=a{j};&lt;BR /&gt; c_value=a{k};&lt;BR /&gt;output ww1;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I believe that I have exact all combination.&lt;/P&gt;
&lt;P&gt;if you add an ID=_n_&lt;/P&gt;
&lt;P&gt;the code below show no duplicate.&lt;BR /&gt;proc sort data=want&amp;nbsp;nodupkey;&lt;BR /&gt;by a_name b_name c_name a_value b_value c_value id;run;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 04:06:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246832#M309401</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2016-01-29T04:06:24Z</dc:date>
    </item>
    <item>
      <title>Re: Array do loop through a subset of condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246833#M309402</link>
      <description>&lt;P&gt;I attempted a hash solution, my first one!!!&lt;/P&gt;
&lt;P&gt;Test it...thoroughly &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Reference used:&amp;nbsp;&lt;A href="http://www.sascommunity.org/wiki/Data_Aggregation_Using_the_SAS_Hash_Object" target="_blank"&gt;http://www.sascommunity.org/wiki/Data_Aggregation_Using_the_SAS_Hash_Object&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input a1 a2 a3 a4 win_or_loss ;
cards;
1 1 1 1 1 
1 1 1 1 -1
1 1 0 0 1
2 1 0 0 1
2 0 2 1 -1
2 0 2 1 1
2 0 3 1 -1
;
run;

data _null_;
dcl hash H (ordered: "A");
h.defineKey("a1", "a2", "a3", "a4", "win_or_loss");
h.defineData("a1", "a2", "a3", "a4", "win_or_loss", "count");
h.defineDone();

dcl hash U();
u.definekey("a1", "a2", "a3", "a4", "win_or_loss");
u.definedone();

do until(end);
   set have end=end;
   if h.find() ne 0 then call missing(count);
   count= sum(count, 1);
   
   if u.check() ne 0 then do;
      u.add();
    end;
    h.replace();

     end;
     
     h.output(dataset:"hash_agg");
     stop;
    
run;

data hash_agg;
set hash_agg;
result=ifc(win_or_loss=1, "Win", "Loss");
run;

proc transpose data=hash_agg out=want(drop=_name_);
by a1 a2 a3 a4;
id result;
var count;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Jan 2016 04:09:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246833#M309402</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-29T04:09:34Z</dc:date>
    </item>
    <item>
      <title>Re: Array do loop through a subset of condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246839#M309403</link>
      <description>&lt;P&gt;It you look at the log, it will tell you how many observations were duplicates. &amp;nbsp;The NODUPKEY option removes them.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 04:48:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246839#M309403</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-01-29T04:48:38Z</dc:date>
    </item>
    <item>
      <title>Re: Array do loop through a subset of condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246841#M309404</link>
      <description>&lt;P&gt;Thank you so much.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 05:10:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-do-loop-through-a-subset-of-condition/m-p/246841#M309404</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2016-01-29T05:10:16Z</dc:date>
    </item>
  </channel>
</rss>

