<?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: Query a range of variables for the presence of a value in at least one variable to code another in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Query-a-range-of-variables-for-the-presence-of-a-value-in-at/m-p/610318#M18075</link>
    <description>Following up on my prior message:&lt;BR /&gt;&lt;BR /&gt;The below code produced the output I was looking for - I added "of" before each variable range.&lt;BR /&gt;&lt;BR /&gt;Data test; set mvrx19.master;&lt;BR /&gt;esp = .;&lt;BR /&gt;esp = (whichn(1, of Spanish2-Spanish4, of Spanish6-Spanish24)&amp;gt;0 );&lt;BR /&gt;run;</description>
    <pubDate>Sun, 08 Dec 2019 19:08:31 GMT</pubDate>
    <dc:creator>JackHP</dc:creator>
    <dc:date>2019-12-08T19:08:31Z</dc:date>
    <item>
      <title>Query a range of variables for the presence of a value in at least one variable to code another var.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Query-a-range-of-variables-for-the-presence-of-a-value-in-at/m-p/610146#M18038</link>
      <description>&lt;P&gt;I would like to create a variable, spanish, with a value of 1 if participant is a Spanish speaker, and a 0 of they are not.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to determine this by the presence of a numeric 1 in at least one of a range of variables: spanish2-spanish4 spanish5-spanish24 (note there is no spanish5 variable)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried the below but this sets just about every observation incorrectly the initial missing categorization. Thank you for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data = test; set = mvrx19.master;&lt;/P&gt;&lt;P&gt;Spanish = .;&lt;/P&gt;&lt;P&gt;if Spanish2-Spanish4 or Spanish6-Spanish24 = 0 then Spanish = 0;&lt;/P&gt;&lt;P&gt;else if Spanish2-Spanish4 or Spanish6-Spanish24 = 1 then Spanish = 1;&lt;/P&gt;&lt;P&gt;proc print;&lt;/P&gt;&lt;P&gt;var spanish Spanish2-Spanish4 Spanish6-Spanish24;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Example desired output:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;spanish&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt; &lt;STRONG&gt;spanish2&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt; &lt;STRONG&gt;spanish3 &amp;nbsp;&amp;nbsp;&lt;/STRONG&gt; &lt;STRONG&gt;spanish4&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt; &lt;STRONG&gt;spanish6&lt;/STRONG&gt; .....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;STRONG&gt;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Dec 2019 23:27:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Query-a-range-of-variables-for-the-presence-of-a-value-in-at/m-p/610146#M18038</guid>
      <dc:creator>JackHP</dc:creator>
      <dc:date>2019-12-06T23:27:55Z</dc:date>
    </item>
    <item>
      <title>Re: Query a range of variables for the presence of a value in at least one variable to code another</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Query-a-range-of-variables-for-the-presence-of-a-value-in-at/m-p/610148#M18039</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input       spanish2   spanish3    spanish4    spanish6;
cards;
0                  .                   1                 0
0                  .                   0                  .
.                   .                   .                   .
;

data want;
set have;
spanish=^^sum(of spanish2--spanish6,0);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or MAX&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;spanish=^^max(of spanish2--spanish6,0);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Dec 2019 23:44:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Query-a-range-of-variables-for-the-presence-of-a-value-in-at/m-p/610148#M18039</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-12-06T23:44:11Z</dc:date>
    </item>
    <item>
      <title>Re: Query a range of variables for the presence of a value in at least one variable to code another</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Query-a-range-of-variables-for-the-presence-of-a-value-in-at/m-p/610150#M18040</link>
      <description>&lt;P&gt;To find a specific value in a group of variables then the function you want is WHICHN for numeric and WHICHC for character values.&lt;/P&gt;
&lt;P&gt;The function actually returns the position number of the first variable with the value or 0 if not found.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try&lt;/P&gt;
&lt;PRE&gt;Data = test; 
   set = mvrx19.master;
   Spanish = (whichn(1,Spanish2-Spanish4,Spanish6-Spanish24)&amp;gt;0 );
run;&lt;/PRE&gt;
&lt;P&gt;The parentheses around the "whichn(1,Spanish2-Spanish4,Spanish6-Spanish24)&amp;gt;0" return a logical true, or 1, if any of the values have a 1, or false (0) if none have a one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Whichn is one of the functions that will use variable lists as you attempted.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Dec 2019 23:45:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Query-a-range-of-variables-for-the-presence-of-a-value-in-at/m-p/610150#M18040</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-12-06T23:45:59Z</dc:date>
    </item>
    <item>
      <title>Re: Query a range of variables for the presence of a value in at least one variable to code another</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Query-a-range-of-variables-for-the-presence-of-a-value-in-at/m-p/610188#M18044</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/298893"&gt;@JackHP&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another option is the &lt;A href="https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=p00iah2thp63bmn1lt20esag14lh.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#p0xgxvo2we2mqrn1kteecpv1opx0" target="_blank" rel="noopener"&gt;IN operator&lt;/A&gt; applied to an array:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set mvrx19.master;
array esp[*] spanish2-spanish4 spanish6-spanish24;
spanish=(1 in esp);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If variables &lt;FONT face="courier new,courier"&gt;spanish2&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;spanish3&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;spanish4&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;spanish6&lt;/FONT&gt;, ..., &lt;FONT face="courier new,courier"&gt;spanish24&lt;/FONT&gt; are the only variables in &lt;FONT face="courier new,courier"&gt;mvrx19.master&lt;/FONT&gt; whose names start with, say, "&lt;FONT face="courier new,courier"&gt;span&lt;/FONT&gt;", the &lt;A href="https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=p0wphcpsfgx6o7n1sjtqzizp1n39.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank" rel="noopener"&gt;variable list&lt;/A&gt; in the array definition can be written as &lt;FONT face="courier new,courier"&gt;span:&lt;/FONT&gt;. Together with other ways to shorten the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set mvrx19.master;
array e span:;
spanish=1 in e;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(assuming that there's no variable &lt;FONT face="courier new,courier"&gt;e&lt;/FONT&gt; [or &lt;FONT face="courier new,courier"&gt;esp&lt;/FONT&gt; in the first example] in dataset &lt;FONT face="courier new,courier"&gt;mvrx19.master&lt;/FONT&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that your code doesn't work correctly for several reasons:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The equals signs in the DATA and SET statements are syntax errors.&lt;/LI&gt;
&lt;LI&gt;The hyphens in your IF conditions are interpreted as minus signs, so you're looking at two differences involving only four variables. No variable lists are used there.*&lt;/LI&gt;
&lt;LI&gt;A numeric expression such as (the difference) Spanish2-Spanish4 without a comparison operator is evaluated as TRUE if the value of the expression is neither zero nor missing. The equals signs in your IF conditions only refer to the difference Spanish6-Spanish24.&lt;/LI&gt;
&lt;LI&gt;The OR operator in the first IF condition would be logically incorrect even if everything else worked as intended.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;* The same happens if you forget the &lt;A href="https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=n1t04bwpt1jkadn1a5mk35we2yif.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank" rel="noopener"&gt;OF operator&lt;/A&gt; before a &lt;A href="https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=p0wphcpsfgx6o7n1sjtqzizp1n39.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#p0j6gep47b66jwn1cuwjgnw9cmuj" target="_blank" rel="noopener"&gt;numbered range list&lt;/A&gt;&amp;nbsp;in the argument of a SAS function, e.g.&lt;/P&gt;
&lt;PRE&gt;whichn(1, &lt;FONT size="3" color="#008000"&gt;&lt;STRONG&gt;of&lt;/STRONG&gt;&lt;/FONT&gt; Spanish2-Spanish4, &lt;FONT size="3"&gt;&lt;STRONG&gt;&lt;FONT color="#008000"&gt;of&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt; Spanish6-Spanish24)&lt;/PRE&gt;</description>
      <pubDate>Sat, 07 Dec 2019 11:29:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Query-a-range-of-variables-for-the-presence-of-a-value-in-at/m-p/610188#M18044</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-12-07T11:29:11Z</dc:date>
    </item>
    <item>
      <title>Re: Query a range of variables for the presence of a value in at least one variable to code another</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Query-a-range-of-variables-for-the-presence-of-a-value-in-at/m-p/610310#M18071</link>
      <description>&lt;P&gt;Thank you for the quick response and for sharing the WhichN/WhichC function!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately, this isn't quite getting the output I'm looking for - if at least one variable in a list = 1, I would like the new variable "Spanish" to equal 1 (even if other variables in the list are missing or set to 0). Current output has Spanish = 0 for all observations, even though several observations have a value of 1 for at least some of the Spanish2-Spanish24 variables queried. Any suggestions on how to revise the code?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Sun, 08 Dec 2019 18:41:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Query-a-range-of-variables-for-the-presence-of-a-value-in-at/m-p/610310#M18071</guid>
      <dc:creator>JackHP</dc:creator>
      <dc:date>2019-12-08T18:41:38Z</dc:date>
    </item>
    <item>
      <title>Re: Query a range of variables for the presence of a value in at least one variable to code another</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Query-a-range-of-variables-for-the-presence-of-a-value-in-at/m-p/610317#M18074</link>
      <description>&lt;P&gt;Thank you for the detailed reply - this worked and output is exactly what I was looking for - thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I appreciate the feedback on the initial code as well, it's helpful to have that detailed, as well as the alternative option using the WhichN approach.&lt;/P&gt;</description>
      <pubDate>Sun, 08 Dec 2019 19:06:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Query-a-range-of-variables-for-the-presence-of-a-value-in-at/m-p/610317#M18074</guid>
      <dc:creator>JackHP</dc:creator>
      <dc:date>2019-12-08T19:06:36Z</dc:date>
    </item>
    <item>
      <title>Re: Query a range of variables for the presence of a value in at least one variable to code another</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Query-a-range-of-variables-for-the-presence-of-a-value-in-at/m-p/610318#M18075</link>
      <description>Following up on my prior message:&lt;BR /&gt;&lt;BR /&gt;The below code produced the output I was looking for - I added "of" before each variable range.&lt;BR /&gt;&lt;BR /&gt;Data test; set mvrx19.master;&lt;BR /&gt;esp = .;&lt;BR /&gt;esp = (whichn(1, of Spanish2-Spanish4, of Spanish6-Spanish24)&amp;gt;0 );&lt;BR /&gt;run;</description>
      <pubDate>Sun, 08 Dec 2019 19:08:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Query-a-range-of-variables-for-the-presence-of-a-value-in-at/m-p/610318#M18075</guid>
      <dc:creator>JackHP</dc:creator>
      <dc:date>2019-12-08T19:08:31Z</dc:date>
    </item>
  </channel>
</rss>

