<?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 Array or Macro for more efficient code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-Array-or-Macro-for-more-efficient-code/m-p/778916#M248011</link>
    <description>&lt;P&gt;1) No, syntax issue (see the log below). But you could test it yourself &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; See&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;'s&amp;nbsp;maxim no.4&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) How would you interpret such code? X is an array, (101,102,103) is also "kind of an array".&lt;/P&gt;
&lt;P&gt;What should such syntax: "array in array" mean?&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;"If at least one element of left array in right array then True" or&lt;/LI&gt;
&lt;LI&gt;"If all elements of left array in right array then True"?&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;You can always test each element of X against (101,102,103) in a do-loop. But since X is "bigger" I would do a loop over "10_"s.&lt;/P&gt;
&lt;P&gt;Frankly speaking the "in" operator is probably a loop under the hood too, with a "break" when success probably too.&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;PRE&gt;1    data wanted;
2      set have;
3
4      length Ind $ 10;
5      array X[*] x1--x7;
6
7      if X in (101,102,103) then Ind='Red';
ERROR: Illegal reference to the array X.
8      else if sum(of X[*]) = 0 then Ind='White';
9      else Ind='Yellow';
10
11   run;

NOTE: The SAS System stopped processing this step because of errors.
&lt;/PRE&gt;</description>
    <pubDate>Sat, 06 Nov 2021 09:12:07 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2021-11-06T09:12:07Z</dc:date>
    <item>
      <title>Using Array or Macro for more efficient code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Array-or-Macro-for-more-efficient-code/m-p/778899#M247999</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;What is the way to write this code in more efficeient way (less code )?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input id X1  X2  X3   X4  X5  X6  X7 ;
cards;
1  0 0 0 0 0 0 0
2 101 0 0 0 0 0 0
3 0 0  101 102 0 0 103
4 0 0 0 0 0 0 105
5 0 0 0 0 0 0 0
6 105 0 0 0 0 0 0 ;
Run;
/**ID1- white because all zeros
   ID2-Red because have at least one 101/102/103
   ID3-Red because have at least one 101/102/103
   ID4-Yellow because doesnt have 101/102/103 AND have at least one element that is not zero
   ID5- white because all zeros
   ID6-Yellow because doesnt have 101/102/103 AND have at least one element that is not zero
***/

Data wanted;
set have;
IF X1 in (101,102,103)  
OR
 X2 in (101,102,103)  
OR
 X3 in (101,102,103)  
OR
 X4 in (101,102,103)  
OR
 X5 in (101,102,103)  
OR
 X6 in (101,102,103)  
OR
 X7 in (101,102,103)  
then Ind='Red';
else IF  X1 not in (101,102,103)  
and 
 X2 not in (101,102,103)  
and 
 X3 not in (101,102,103)  
and 
 X4 not in (101,102,103)  
and 
 X5 not in (101,102,103)  
and 
 X6 not in (101,102,103)  
and 
 X7 not in (101,102,103)  
and 
(LENGTH (put(X1,best.)&amp;gt;=2  OR LENGTH (put(X2,best.))&amp;gt;=2 OR LENGTH (put(X3,best.))&amp;gt;=2
OR LENGTH (put(X4,best.))&amp;gt;=2 OR LENGTH (put(X5,best.))&amp;gt;=2 OR LENGTH (put(X6,best.))&amp;gt;=2
OR LENGTH (put(X7,best.))&amp;gt;=2)
then Ind='Yellow';
else IF X1=0 AND X2=0 AND X3=0 AND X4=0 AND X5=0 AND X6=0 AND X7=0   then Ind='White';
Run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 06 Nov 2021 06:36:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Array-or-Macro-for-more-efficient-code/m-p/778899#M247999</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-11-06T06:36:14Z</dc:date>
    </item>
    <item>
      <title>Re: Using Array or Macro for more efficient code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Array-or-Macro-for-more-efficient-code/m-p/778900#M248000</link>
      <description>&lt;P&gt;This?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if index(catx(' ',of X1-X6),'101') | index(catx(' ',of X1-X6),'102') | index(catx(' ',of X1-X6),'103') then IND='red';
else if sum(of X1-X6)=0 then IND='White'
else IND='Yellow';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Even less code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if prxmatch('/101|102|103/',catx(' ',of X1-X6)) then IND='red';
else if sum(of X1-X6)=0 then IND='White'
else IND='Yellow';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Even less&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IND=ifc( prxmatch('/10[123]/',catx(' ',of X1-X6)) ,'Red'
   ,ifc( ^sum(of X1-X6) , 'White', 'Yellow'));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Nov 2021 07:07:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Array-or-Macro-for-more-efficient-code/m-p/778900#M248000</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-11-06T07:07:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using Array or Macro for more efficient code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Array-or-Macro-for-more-efficient-code/m-p/778901#M248001</link>
      <description>May you please  show using array?</description>
      <pubDate>Sat, 06 Nov 2021 07:18:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Array-or-Macro-for-more-efficient-code/m-p/778901#M248001</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-11-06T07:18:38Z</dc:date>
    </item>
    <item>
      <title>Re: Using Array or Macro for more efficient code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Array-or-Macro-for-more-efficient-code/m-p/778904#M248003</link>
      <description>&lt;P&gt;1. You've been shown how to use arrays countless times&lt;/P&gt;
&lt;P&gt;2. There's no need for arrays here as the variable names can be referenced with the hyphen shortcut: VAR1-VAR6&lt;/P&gt;</description>
      <pubDate>Sat, 06 Nov 2021 07:23:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Array-or-Macro-for-more-efficient-code/m-p/778904#M248003</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-11-06T07:23:47Z</dc:date>
    </item>
    <item>
      <title>Re: Using Array or Macro for more efficient code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Array-or-Macro-for-more-efficient-code/m-p/778907#M248006</link>
      <description>&lt;P&gt;One more, not as compact as the one&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;offered but works on numeric values only,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data wanted;
  set have;

  length Ind $ 10;
  array X[*] x1--x7;

  if 101 in X or 102 in X or 103 in X then Ind='Red';
  else if 101 not in X and 102 not in X and 103 not in X
          and max(of X[*]) &amp;gt; 0 then Ind='Yellow';
  else if sum(of X[*]) = 0 then Ind='White';

run;

proc print data = wanted;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but even shorter when combined with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;'s second one:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data wanted;
  set have;

  length Ind $ 10;
  array X[*] x1--x7;

  if 101 in X or 102 in X or 103 in X then Ind='Red';  
  else if sum(of X[*]) = 0 then Ind='White';
  else Ind='Yellow';

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sat, 06 Nov 2021 07:50:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Array-or-Macro-for-more-efficient-code/m-p/778907#M248006</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-11-06T07:50:44Z</dc:date>
    </item>
    <item>
      <title>Re: Using Array or Macro for more efficient code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Array-or-Macro-for-more-efficient-code/m-p/778909#M248008</link>
      <description>Great and thank you.&lt;BR /&gt;A question related to first code.&lt;BR /&gt;You wrote&lt;BR /&gt;If 101 in x or 102 in x or 103 in x&lt;BR /&gt;Could you wrote&lt;BR /&gt;If x in (101,102,103)??</description>
      <pubDate>Sat, 06 Nov 2021 08:11:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Array-or-Macro-for-more-efficient-code/m-p/778909#M248008</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-11-06T08:11:56Z</dc:date>
    </item>
    <item>
      <title>Re: Using Array or Macro for more efficient code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Array-or-Macro-for-more-efficient-code/m-p/778916#M248011</link>
      <description>&lt;P&gt;1) No, syntax issue (see the log below). But you could test it yourself &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; See&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;'s&amp;nbsp;maxim no.4&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) How would you interpret such code? X is an array, (101,102,103) is also "kind of an array".&lt;/P&gt;
&lt;P&gt;What should such syntax: "array in array" mean?&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;"If at least one element of left array in right array then True" or&lt;/LI&gt;
&lt;LI&gt;"If all elements of left array in right array then True"?&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;You can always test each element of X against (101,102,103) in a do-loop. But since X is "bigger" I would do a loop over "10_"s.&lt;/P&gt;
&lt;P&gt;Frankly speaking the "in" operator is probably a loop under the hood too, with a "break" when success probably too.&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;PRE&gt;1    data wanted;
2      set have;
3
4      length Ind $ 10;
5      array X[*] x1--x7;
6
7      if X in (101,102,103) then Ind='Red';
ERROR: Illegal reference to the array X.
8      else if sum(of X[*]) = 0 then Ind='White';
9      else Ind='Yellow';
10
11   run;

NOTE: The SAS System stopped processing this step because of errors.
&lt;/PRE&gt;</description>
      <pubDate>Sat, 06 Nov 2021 09:12:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Array-or-Macro-for-more-efficient-code/m-p/778916#M248011</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-11-06T09:12:07Z</dc:date>
    </item>
  </channel>
</rss>

