<?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 I use an array to check for several values in multiple variables? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/800984#M315193</link>
    <description>&lt;P&gt;Maybe:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data combinedmasternewvars;
   set project.combinedmaster;
   Array cd(30) $ D1-D30;
   Present=0;
   do i=1 to 30;
       If cd (i) in ("A04" "A05" "A06") then do;
            present=1;
            leave;
       end;
   end;
Run;&lt;/PRE&gt;
&lt;P&gt;Set the "not found" before checking the values.&lt;/P&gt;
&lt;P&gt;The above with the LEAVE instruction tells SAS to exit the loop set by the Do i= ....; Because the Leave instruction is set to execute conditionally when one of the values is found, it stops searching for the other matches. If none of the variables matches then the not found is the value as it has not been overwritten.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note that the code I posted is in a text box opened on the forum using the &amp;lt;/&amp;gt; icon so formatting of text is maintained a bit nicer. You could also post in a code box which is opened with the little "running man" icon. The boxes help separate question from code and make pasted text much easier to read (especially if code is indented), or in the case of examples of a text file keep the contents. The message windows on this forum will reformat text and in some cases may result in non-printable characters that mean code copied and pasted from the forum will not execute as expected or at all even when it appears correct.&lt;/P&gt;</description>
    <pubDate>Wed, 09 Mar 2022 03:44:36 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2022-03-09T03:44:36Z</dc:date>
    <item>
      <title>How do I use an array to check for several values in multiple variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/800972#M315183</link>
      <description>&lt;P&gt;I have 30 variables that I'm trying to check through to see if they each match one of several results. If any of those specific results are in even one of those 30 variables, I want a new indicator variable to be equal to 1, and equal to 0 otherwise. I don't care if it's the first variable checked or the last but I seem to only be able to get it to check the last variable for the result options I want. It doesn't give errors, but only shows a few with the new variable equal to 1 and they seem to all be ones where the result is in the D30 variable so I'm guessing I'm missing something that makes it check all of the variables and mark the new variable to 1 no matter which variable it is, but I can't figure out how to fix that. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this:&lt;/P&gt;&lt;P&gt;data combinedmasternewvars;&lt;BR /&gt;set project.combinedmaster;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Array cd(30) $ D1-D30;&lt;BR /&gt;do i=1 to 30;&lt;BR /&gt;If cd (i) in ("A04" "A05" "A06") then present=1;&lt;BR /&gt;Else present=0;&lt;BR /&gt;end;&lt;BR /&gt;Run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2022 02:26:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/800972#M315183</guid>
      <dc:creator>Geoghegan</dc:creator>
      <dc:date>2022-03-09T02:26:41Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use an array to check for several values in multiple variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/800973#M315184</link>
      <description>Is your dataset arranged well with D1, D2, D3 … D30 columns in sequence, or is jumbled like D2, D1, D30, D5, because then D1-D30 will look at just D1 and D30. To arrange in sequence, you can precede with a data step retaining the right sequence like data a; retain D1 D2 D3; set combinedmaster; run;</description>
      <pubDate>Wed, 09 Mar 2022 02:40:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/800973#M315184</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2022-03-09T02:40:33Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use an array to check for several values in multiple variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/800974#M315185</link>
      <description>&lt;P&gt;The ELSE always resets it to 0, even after it's been found once. Remove the ELSE and you should have the answer you expect.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data combinedmasternewvars;
set project.combinedmaster;

Array cd(30) $ D1-D30;
present=0;
do i=1 to 30;
If cd (i) in ("A04" "A05" "A06") then present=1;
end;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/302752"&gt;@Geoghegan&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have 30 variables that I'm trying to check through to see if they each match one of several results. If any of those specific results are in even one of those 30 variables, I want a new indicator variable to be equal to 1, and equal to 0 otherwise. I don't care if it's the first variable checked or the last but I seem to only be able to get it to check the last variable for the result options I want. It doesn't give errors, but only shows a few with the new variable equal to 1 and they seem to all be ones where the result is in the D30 variable so I'm guessing I'm missing something that makes it check all of the variables and mark the new variable to 1 no matter which variable it is, but I can't figure out how to fix that. Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have this:&lt;/P&gt;
&lt;P&gt;data combinedmasternewvars;&lt;BR /&gt;set project.combinedmaster;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Array cd(30) $ D1-D30;&lt;BR /&gt;do i=1 to 30;&lt;BR /&gt;If cd (i) in ("A04" "A05" "A06") then present=1;&lt;BR /&gt;Else present=0;&lt;BR /&gt;end;&lt;BR /&gt;Run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2022 03:59:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/800974#M315185</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-03-09T03:59:02Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use an array to check for several values in multiple variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/800977#M315187</link>
      <description>I think D1 is earlier in the dataset and then D2-30 are in order later in&lt;BR /&gt;the dataset. Would that make it just look at D30?&lt;BR /&gt;</description>
      <pubDate>Wed, 09 Mar 2022 02:52:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/800977#M315187</guid>
      <dc:creator>Geoghegan</dc:creator>
      <dc:date>2022-03-09T02:52:45Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use an array to check for several values in multiple variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/800984#M315193</link>
      <description>&lt;P&gt;Maybe:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data combinedmasternewvars;
   set project.combinedmaster;
   Array cd(30) $ D1-D30;
   Present=0;
   do i=1 to 30;
       If cd (i) in ("A04" "A05" "A06") then do;
            present=1;
            leave;
       end;
   end;
Run;&lt;/PRE&gt;
&lt;P&gt;Set the "not found" before checking the values.&lt;/P&gt;
&lt;P&gt;The above with the LEAVE instruction tells SAS to exit the loop set by the Do i= ....; Because the Leave instruction is set to execute conditionally when one of the values is found, it stops searching for the other matches. If none of the variables matches then the not found is the value as it has not been overwritten.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note that the code I posted is in a text box opened on the forum using the &amp;lt;/&amp;gt; icon so formatting of text is maintained a bit nicer. You could also post in a code box which is opened with the little "running man" icon. The boxes help separate question from code and make pasted text much easier to read (especially if code is indented), or in the case of examples of a text file keep the contents. The message windows on this forum will reformat text and in some cases may result in non-printable characters that mean code copied and pasted from the forum will not execute as expected or at all even when it appears correct.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2022 03:44:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/800984#M315193</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-03-09T03:44:36Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use an array to check for several values in multiple variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/800989#M315198</link>
      <description>I think that would be ok. If array did not include all 30 variables, the do loop would complain that array subscript is out of range.</description>
      <pubDate>Wed, 09 Mar 2022 04:22:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/800989#M315198</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2022-03-09T04:22:18Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use an array to check for several values in multiple variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/800990#M315199</link>
      <description>&lt;P&gt;or this way.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data combinedmasternewvars;
  set project.combinedmaster;
  array cd {*} $ d1-d30;
  present=0;
  do i=1 to dim(cd) until(present=1);
    if cd[i] in ("A04" "A05" "A06") then
      present=1;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2022 04:35:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/800990#M315199</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-03-09T04:35:49Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use an array to check for several values in multiple variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/800993#M315200</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/302752"&gt;@Geoghegan&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I think D1 is earlier in the dataset and then D2-30 are in order later in&lt;BR /&gt;the dataset. Would that make it just look at D30?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The order of the variables in the dataset does not matter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is the order of the variables in your ARRAY statement that matters and D30 is the last variable in the array statement.&amp;nbsp; Because you include the ELSE clause only the result of last element&amp;nbsp; in the array matters.&amp;nbsp; Remove the ELSE clause.&amp;nbsp; If you want the new variable coded as 1 or 0 instead of 1 or missing then set it to 0 before the DO loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2022 04:53:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/800993#M315200</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-03-09T04:53:40Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use an array to check for several values in multiple variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/801012#M315210</link>
      <description>&lt;P&gt;Already mentioned: The else-part is causing the problem.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data combinedmasternewvars;
  set project.combinedmaster;

  array cd[30] D1-D30;
  present = 0;
  do i=1 to 30;
    if cd[i] in ("A04" "A05" "A06") then do;
      present=1;
      leave;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Mar 2022 06:54:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/801012#M315210</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-03-09T06:54:53Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use an array to check for several values in multiple variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/801089#M315252</link>
      <description>&lt;P&gt;Thank you everyone! a few of those variations worked and that makes sense why what I did was messing it up.&lt;/P&gt;&lt;P&gt;I appreciate it!&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2022 15:15:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-an-array-to-check-for-several-values-in-multiple/m-p/801089#M315252</guid>
      <dc:creator>Geoghegan</dc:creator>
      <dc:date>2022-03-09T15:15:41Z</dc:date>
    </item>
  </channel>
</rss>

