<?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: Defining array without numbered variable names in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668688#M200487</link>
    <description>&lt;P&gt;You may want to run the following code and then read the log.&lt;/P&gt;
&lt;P&gt;SAS will treat any numeric value as a "boolean" when used with "If Variablename " constructs. It is a good idea to know how they are treated.&lt;/P&gt;
&lt;PRE&gt;data example;
   do x = ., -1, -.5, 0, 1 , 3 , 3.1416;
      if x then result ="SAS considers this value as true";
      else result = 'Treated as False';
      put x= result;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;Depending on the actual needed use later in a process sometimes you can save creating two variables by using one to hold both a count(treated as 1 if greater than 0) instead of a count plus an "any in a range".&lt;/P&gt;</description>
    <pubDate>Sun, 12 Jul 2020 20:37:29 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-07-12T20:37:29Z</dc:date>
    <item>
      <title>Defining array without numbered variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668649#M200464</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a fairly long list of variable (all 0/1) that all have the prefix "_any." These also all happen to be in consecutive columns. I am trying to use an array to define a new variable "outcome" as having value "1" if any of the variables with prefix "_any" have "1" as their value. I have tried the following code:&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/SPAN&gt; want;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;set&lt;/SPAN&gt; have;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;array&lt;/SPAN&gt; any(*) any_:;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;do&lt;/SPAN&gt; i=&lt;SPAN class="s3"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN class="s2"&gt;to&lt;/SPAN&gt; dim(any);&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;if&lt;/SPAN&gt; any(i) in: (&lt;SPAN class="s4"&gt;"1"&lt;/SPAN&gt;) &lt;SPAN class="s2"&gt;then&lt;/SPAN&gt; outcome=&lt;SPAN class="s3"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt;;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;else&lt;/SPAN&gt; outcome=&lt;SPAN class="s3"&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/SPAN&gt;;&lt;/P&gt;&lt;P class="p2"&gt;end&lt;SPAN class="s5"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN class="s5"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p3"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p3"&gt;&lt;SPAN class="s5"&gt;However, this is not returning the correct answer (the count for "outcome" is far less than I know the true value should be). Does anyone have any suggestions regarding what I should change? Thank you!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jul 2020 15:25:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668649#M200464</guid>
      <dc:creator>jgreenberg321</dc:creator>
      <dc:date>2020-07-12T15:25:29Z</dc:date>
    </item>
    <item>
      <title>Re: Defining array without numbered variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668654#M200467</link>
      <description>&lt;P&gt;The code you show will only ever have the result of 0 or 1 and will only have the "outcome" of the last variable in the array.&lt;/P&gt;
&lt;P&gt;If you intend to accumulate a value when the conditions are met something like&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   array any(*) any_:;
   do i=1 to dim(any);
      if any(i) in: ("1") then outcome=sum(outcome,1);
   end;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What your code was doing was comparing the first value and setting 1 or 0.&lt;/P&gt;
&lt;P&gt;Then the next variable was compared and setting 1 or 0 (not accumulating from the previous loop)&lt;/P&gt;
&lt;P&gt;Repeat until running out of variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are looking for a "run" count, longest sequence then the above code would use the "else outcome=0". otherwise that zero assignment is not needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jul 2020 15:44:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668654#M200467</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-07-12T15:44:13Z</dc:date>
    </item>
    <item>
      <title>Re: Defining array without numbered variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668655#M200468</link>
      <description>&lt;P&gt;Are the variables prefixed with ANY_ or _ANY?&lt;/P&gt;
&lt;P&gt;Anyway if you just want to know if any of the 'anys' is "1" then use the WHICHC() function. No need for an array or a do loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  outcome= 0 ne whichc("1", of any_:);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jul 2020 15:58:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668655#M200468</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-12T15:58:19Z</dc:date>
    </item>
    <item>
      <title>Re: Defining array without numbered variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668656#M200469</link>
      <description>&lt;P&gt;Thank you very much! This successfully created the counter variable. I didn't realize that in my previous code the array would not look for the variable "1" in any of the array variables. I realize I can always do this after the fact using the summed variable, but what would be the correct way to request the array set outcome=1 if "1" is identified in any of the array variables?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As an aside, I tried the "whichc" method, but that did not seem to work (returns "0" for everyone).&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jul 2020 16:08:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668656#M200469</guid>
      <dc:creator>jgreenberg321</dc:creator>
      <dc:date>2020-07-12T16:08:13Z</dc:date>
    </item>
    <item>
      <title>Re: Defining array without numbered variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668658#M200471</link>
      <description>&lt;P&gt;WHICHC() returns a the index into the list of variables where the value was first found.&amp;nbsp; So it always returns a number. It returns zero when the string is not found.&amp;nbsp; So whatever string you where searching for was not in the variables you listed.&lt;/P&gt;
&lt;P&gt;If your ANY_: variables are numeric then if you use them with WHICHC() they will first be converted to stings using the BEST12. format.&amp;nbsp; So 1 would become the digit 1 with eleven spaces in front of it so it would not match a string like '1'.&lt;/P&gt;
&lt;P&gt;For numeric variables use WHICHN() and search for a numeric value instead of character string.&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jul 2020 16:32:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668658#M200471</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-12T16:32:46Z</dc:date>
    </item>
    <item>
      <title>Re: Defining array without numbered variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668659#M200472</link>
      <description>&lt;P&gt;Ah I see. So using "whichn" would make more sense, given that my variables are numeric. I switched to that and now have an answer that seems much more reasonable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to understand how the full statement you provided is getting to that result. I see that whichn is supposed to return the index of the first matching value, but this statement (fortunately) just returns 0/1.&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jul 2020 16:32:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668659#M200472</guid>
      <dc:creator>jgreenberg321</dc:creator>
      <dc:date>2020-07-12T16:32:35Z</dc:date>
    </item>
    <item>
      <title>Re: Defining array without numbered variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668660#M200473</link>
      <description>&lt;P&gt;&amp;nbsp;The code is testing if the result of the function call is zero or not.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;0 ne ....&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;SAS evaluates boolean expression with 0 for FALSE and 1 for TRUE.&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jul 2020 16:34:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668660#M200473</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-12T16:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: Defining array without numbered variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668662#M200475</link>
      <description>&lt;P&gt;I see. So it returns a value of "1" for outcome when the whichn expression is not equal to 0. Very clever. Since I ultimately wanted to have outcome=1 if the "any_" statements are "1" or if another variable &amp;gt;=1, it looks like I can do the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;outcome=&lt;SPAN class="s1"&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/SPAN&gt; ne (whichn(&lt;SPAN class="s2"&gt;"1"&lt;/SPAN&gt;, of any_:) or var2 &amp;gt;=&lt;SPAN class="s1"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt;);&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jul 2020 16:47:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668662#M200475</guid>
      <dc:creator>jgreenberg321</dc:creator>
      <dc:date>2020-07-12T16:47:55Z</dc:date>
    </item>
    <item>
      <title>Re: Defining array without numbered variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668668#M200481</link>
      <description>&lt;P&gt;When using complex expressions it helps to add ( ) for both the SAS parser and the human readers.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;outcome=(0 ne whichn(1, of any_:)) or (var2 &amp;gt;=1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since SAS will treat any non-zero, non-missing value as TRUE you can shorten that to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;outcome=whichn(1, of any_:) or (var2 &amp;gt;=1);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 Jul 2020 17:27:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668668#M200481</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-12T17:27:09Z</dc:date>
    </item>
    <item>
      <title>Re: Defining array without numbered variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668671#M200484</link>
      <description>Makes sense. Thanks!&lt;BR /&gt;</description>
      <pubDate>Sun, 12 Jul 2020 18:31:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668671#M200484</guid>
      <dc:creator>jgreenberg321</dc:creator>
      <dc:date>2020-07-12T18:31:00Z</dc:date>
    </item>
    <item>
      <title>Re: Defining array without numbered variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668688#M200487</link>
      <description>&lt;P&gt;You may want to run the following code and then read the log.&lt;/P&gt;
&lt;P&gt;SAS will treat any numeric value as a "boolean" when used with "If Variablename " constructs. It is a good idea to know how they are treated.&lt;/P&gt;
&lt;PRE&gt;data example;
   do x = ., -1, -.5, 0, 1 , 3 , 3.1416;
      if x then result ="SAS considers this value as true";
      else result = 'Treated as False';
      put x= result;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;Depending on the actual needed use later in a process sometimes you can save creating two variables by using one to hold both a count(treated as 1 if greater than 0) instead of a count plus an "any in a range".&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jul 2020 20:37:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668688#M200487</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-07-12T20:37:29Z</dc:date>
    </item>
    <item>
      <title>Re: Defining array without numbered variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668720#M200500</link>
      <description>&lt;P&gt;Here is a reference that illustrates several options to refer to variables and datasets in a short cut list:&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jul 2020 00:08:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668720#M200500</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-13T00:08:44Z</dc:date>
    </item>
    <item>
      <title>Re: Defining array without numbered variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668748#M200516</link>
      <description>&lt;P&gt;I think your initial code was almost there, you just have to stop looking when you find a valid value:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array any(*) any_:;
  do i=1 to dim(any) until(outcome=1);
    outcome=any(i) in: ("1");
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I also changed the the code in the loop a bit to make it shorter, but the important thing is the UNTIL in the DO loop header.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jul 2020 07:04:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668748#M200516</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-07-13T07:04:04Z</dc:date>
    </item>
    <item>
      <title>Re: Defining array without numbered variable names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668945#M200591</link>
      <description>That makes sense. Thank you!&lt;BR /&gt;</description>
      <pubDate>Mon, 13 Jul 2020 19:43:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Defining-array-without-numbered-variable-names/m-p/668945#M200591</guid>
      <dc:creator>jgreenberg321</dc:creator>
      <dc:date>2020-07-13T19:43:00Z</dc:date>
    </item>
  </channel>
</rss>

