<?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: How do you use an Array to test each value and output a new variable with the result? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-use-an-Array-to-test-each-value-and-output-a-new/m-p/852381#M336949</link>
    <description>&lt;P&gt;That could work.&lt;/P&gt;
&lt;P&gt;It will create 5 additional variables each of which has a constant value for all observations.&lt;/P&gt;
&lt;P&gt;If you do not need those extra variables then define the array a temporary.&lt;/P&gt;
&lt;P&gt;Also note you don't have to use comma as the delimiters in the list of initial values.&amp;nbsp; Spaces work just as well and are a lot easier to type.&amp;nbsp; And much much easier to use if you want to pass the values in macro variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  array testval[5] $ _temporary_  ("X" "A" "C" "D" "E");
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 05 Jan 2023 20:00:58 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-01-05T20:00:58Z</dc:date>
    <item>
      <title>How do you use an Array to test each value and output a new variable with the result?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-use-an-Array-to-test-each-value-and-output-a-new/m-p/852356#M336936</link>
      <description>&lt;P&gt;I have this sample data set:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data WORK.CheckSet;&lt;BR /&gt;input Col1 $ Col2 $ Col3 $ Col4 $ Col5 $;&lt;BR /&gt;datalines;&lt;BR /&gt;X A B C D &lt;BR /&gt;Y D C F G &lt;BR /&gt;Z F B H E &lt;BR /&gt;;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;What I'm trying to do is test each column variable against an equal amount of test variables testvar1..5 So I also have:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let testVar1 = A;&lt;BR /&gt;%let testVar2 = B;&lt;BR /&gt;%let testVar3 = C;&lt;BR /&gt;%let testVar4 = D;&lt;BR /&gt;%let testVar5 = E;&lt;/PRE&gt;
&lt;P&gt;I wanted to practice using an array so this is where I'm getting stuck:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;BR /&gt;data WORK.ResultSet;&lt;BR /&gt;set CheckSet;&lt;BR /&gt;Array Result[*] Col1-Col5;&lt;BR /&gt;/*&lt;BR /&gt;&amp;lt;paudocode starts here&amp;gt;&lt;BR /&gt;Res[1] = if &amp;amp;testvar1. = Col1 then 1 else 0 &lt;BR /&gt;Res[2] = if &amp;amp;testvar2. = Col2 then 1 else 0 &lt;BR /&gt;Res[3] = if &amp;amp;testvar3. = Col3 then 1 else 0 &lt;BR /&gt;Res[4] = if &amp;amp;testvar4. = Col4 then 1 else 0 &lt;BR /&gt;Res[5] = if &amp;amp;testvar5. = Col5 then 1 else 0 &lt;BR /&gt;&amp;lt;psudocode ends here&amp;gt;&lt;BR /&gt;*/&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;I'm just not sure how to tell SAS to generate 5 new variables as it loops through the array...&amp;nbsp;&lt;BR /&gt;my desired result is:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASAlex101_0-1672946024756.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/79082i85B0B4BB69E83E2D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASAlex101_0-1672946024756.png" alt="SASAlex101_0-1672946024756.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jan 2023 19:14:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-use-an-Array-to-test-each-value-and-output-a-new/m-p/852356#M336936</guid>
      <dc:creator>SASAlex101</dc:creator>
      <dc:date>2023-01-05T19:14:31Z</dc:date>
    </item>
    <item>
      <title>Re: How do you use an Array to test each value and output a new variable with the result?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-use-an-Array-to-test-each-value-and-output-a-new/m-p/852360#M336937</link>
      <description>&lt;P&gt;You need TWO arrays.&amp;nbsp; One of the OLD variables and one for the NEW variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data CheckSet;
  input (Col1-Col5) ($);
datalines;
X A B C D 
Y D E F G 
Z F B H A 
;

%let testval = A ;

data want;
  set CheckSet;
  array old col1-col5 ;
  array new res1-res5 ;
  do index=1 to dim(old);
    new[index] = old[index] = "&amp;amp;testval" ;
  end;
  drop index ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    Col1    Col2    Col3    Col4    Col5    res1    res2    res3    res4    res5

 1      X       A       B       C       D        0       1       0       0       0
 2      Y       D       E       F       G        0       0       0       0       0
 3      Z       F       B       H       A        0       0       0       0       1
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jan 2023 19:12:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-use-an-Array-to-test-each-value-and-output-a-new/m-p/852360#M336937</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-05T19:12:05Z</dc:date>
    </item>
    <item>
      <title>Re: How do you use an Array to test each value and output a new variable with the result?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-use-an-Array-to-test-each-value-and-output-a-new/m-p/852365#M336940</link>
      <description>Nice solution but you beat me to the punch as I was editing my question... I see the [index] is key here. &lt;BR /&gt;Can your solution be adapted for the macro variables 1 to 5 above? or would it be easier to make a third array 1x5 with the test variables in it to loop through?</description>
      <pubDate>Thu, 05 Jan 2023 19:18:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-use-an-Array-to-test-each-value-and-output-a-new/m-p/852365#M336940</guid>
      <dc:creator>SASAlex101</dc:creator>
      <dc:date>2023-01-05T19:18:22Z</dc:date>
    </item>
    <item>
      <title>Re: How do you use an Array to test each value and output a new variable with the result?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-use-an-Array-to-test-each-value-and-output-a-new/m-p/852367#M336942</link>
      <description>&lt;P&gt;Explain how the macro variables are used (explain in words, show us the output, and we'll help create the code)&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jan 2023 19:23:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-use-an-Array-to-test-each-value-and-output-a-new/m-p/852367#M336942</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-01-05T19:23:09Z</dc:date>
    </item>
    <item>
      <title>Re: How do you use an Array to test each value and output a new variable with the result?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-use-an-Array-to-test-each-value-and-output-a-new/m-p/852368#M336943</link>
      <description>Tom! I think this may work: &lt;BR /&gt;data want;&lt;BR /&gt;  set CheckSet;&lt;BR /&gt;  array old col1-col5 ;&lt;BR /&gt;  array new res1-res5 ;&lt;BR /&gt;  array testval[5] $ ("X","A","C","D","E");&lt;BR /&gt;  do index=1 to dim(old);&lt;BR /&gt;    new[index] = old[index] = testval[index] ;&lt;BR /&gt;  end;&lt;BR /&gt;  drop index ;&lt;BR /&gt;run;</description>
      <pubDate>Thu, 05 Jan 2023 19:23:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-use-an-Array-to-test-each-value-and-output-a-new/m-p/852368#M336943</guid>
      <dc:creator>SASAlex101</dc:creator>
      <dc:date>2023-01-05T19:23:51Z</dc:date>
    </item>
    <item>
      <title>Re: How do you use an Array to test each value and output a new variable with the result?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-use-an-Array-to-test-each-value-and-output-a-new/m-p/852381#M336949</link>
      <description>&lt;P&gt;That could work.&lt;/P&gt;
&lt;P&gt;It will create 5 additional variables each of which has a constant value for all observations.&lt;/P&gt;
&lt;P&gt;If you do not need those extra variables then define the array a temporary.&lt;/P&gt;
&lt;P&gt;Also note you don't have to use comma as the delimiters in the list of initial values.&amp;nbsp; Spaces work just as well and are a lot easier to type.&amp;nbsp; And much much easier to use if you want to pass the values in macro variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  array testval[5] $ _temporary_  ("X" "A" "C" "D" "E");
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jan 2023 20:00:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-use-an-Array-to-test-each-value-and-output-a-new/m-p/852381#M336949</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-05T20:00:58Z</dc:date>
    </item>
  </channel>
</rss>

