<?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: If-then statement: Multiple variables into one in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545075#M150757</link>
    <description>&lt;P&gt;You'd have to expand your question with some more detail, but some starting points for you:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Arrays to loop instead of multiple statements&lt;/P&gt;
&lt;P&gt;2. arrays to hold diagnosis as well&lt;/P&gt;
&lt;P&gt;3. Or using WHICHC()/WHICHN() to search an array at once.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/267481"&gt;@Dinurik&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi SAS users,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have groups of 20 variables (for 20 repeated measurements) and now I need to create one new variable that takes a value of 1 if at least one of the 20 variables satisfy the condition. I know that I can do it with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if (cd41&amp;lt;= 200 and cd41 ne . ) or (cd42 &amp;lt;=200 and cd42 ne .) or ... or&amp;nbsp; (cd420 &amp;lt;= 200 and cd420 ne .) then imf = 1;&lt;/P&gt;
&lt;P&gt;else imf = 0;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I have many such groups of variables (20 cd4 counts, 20 viral loads, 20 bp measurements, etc.) and am looking for a fast way to do the same.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please help me with this?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 21 Mar 2019 22:06:23 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-03-21T22:06:23Z</dc:date>
    <item>
      <title>If-then statement: Multiple variables into one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545073#M150755</link>
      <description>&lt;P&gt;Hi SAS users,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a wide-format datafile that consists of groups 20 of variables like:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; vl1&amp;nbsp; &amp;nbsp; &amp;nbsp; vl2&amp;nbsp; &amp;nbsp; vl3&amp;nbsp; ..... vl20&lt;/P&gt;&lt;P&gt;ob1&amp;nbsp; 1000&amp;nbsp; &amp;nbsp;240&amp;nbsp; 3004 ..... 10&lt;/P&gt;&lt;P&gt;ob2&amp;nbsp; &amp;nbsp;50&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;50&amp;nbsp; &amp;nbsp; 50&amp;nbsp; &amp;nbsp; ..... 20&lt;/P&gt;&lt;P&gt;ob3&amp;nbsp; &amp;nbsp;40&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&amp;nbsp; 150&amp;nbsp; ..... 30&lt;/P&gt;&lt;P&gt;ob4&amp;nbsp; &amp;nbsp;243&amp;nbsp; &amp;nbsp; 230&amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; .....&amp;nbsp; .&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;ob1200&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I have variables grouped by visits like vl1-vl20, bp1-bp20, kidney1-kidney20, cd4_1-cd4_20, etc.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to create an additional variable based on each group of 20, in case at least one of the variables satisfies a certain condition. for example for variables vl1-vl20, I need to create a new variable vl_success that take a value of 1 if vl1 &amp;lt; 50 or vl2 &amp;lt;50 or vl3 &amp;lt; 50...or vl20&amp;lt;50 etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since I need to do this for each of the group of variables (and I have many), I don't want to write it all by hand like here:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data repeated2;&lt;BR /&gt;set repeated1;&lt;BR /&gt;if (vl1 &amp;lt; 50 and vl1 ne .) or (vl2 &amp;lt; 50 and vl2 ne .) or (vl3 &amp;lt; 50 and vl3 ne .) ... or (vl20 &amp;lt; 50 and vl20 ne .) then&lt;BR /&gt;vl_success = 1;&lt;BR /&gt;else vl_success = 0;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please help me with this?&lt;/P&gt;</description>
      <pubDate>Thu, 21 Mar 2019 22:30:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545073#M150755</guid>
      <dc:creator>Dinurik</dc:creator>
      <dc:date>2019-03-21T22:30:31Z</dc:date>
    </item>
    <item>
      <title>Re: If-then statement: Multiple variables into one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545075#M150757</link>
      <description>&lt;P&gt;You'd have to expand your question with some more detail, but some starting points for you:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Arrays to loop instead of multiple statements&lt;/P&gt;
&lt;P&gt;2. arrays to hold diagnosis as well&lt;/P&gt;
&lt;P&gt;3. Or using WHICHC()/WHICHN() to search an array at once.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/267481"&gt;@Dinurik&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi SAS users,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have groups of 20 variables (for 20 repeated measurements) and now I need to create one new variable that takes a value of 1 if at least one of the 20 variables satisfy the condition. I know that I can do it with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if (cd41&amp;lt;= 200 and cd41 ne . ) or (cd42 &amp;lt;=200 and cd42 ne .) or ... or&amp;nbsp; (cd420 &amp;lt;= 200 and cd420 ne .) then imf = 1;&lt;/P&gt;
&lt;P&gt;else imf = 0;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I have many such groups of variables (20 cd4 counts, 20 viral loads, 20 bp measurements, etc.) and am looking for a fast way to do the same.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please help me with this?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Mar 2019 22:06:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545075#M150757</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-03-21T22:06:23Z</dc:date>
    </item>
    <item>
      <title>Re: If-then statement: Multiple variables into one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545084#M150759</link>
      <description>&lt;P&gt;See if this gives you hint.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data example;
   input v1 v2 v3 v4;
   array v v1-v4;
   imf = (.&amp;lt; min(of v(*)) le 200);
datalines;
. . . .
1 2 3 4 
220 240 400 600
20 . 300 120
;
run;&lt;/PRE&gt;
&lt;P&gt;The min function only returns missing when all values are missing. So if any of the values are between . and 200 the min will be in that range of values. The comparison returns a 1 when true and 0 when false. SAS allows comparisons like a&amp;lt;x&amp;lt;b so you need not use that "and ne to ."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "of v(*)" says to use all of the elements of the array for the calculation of the min value.&lt;/P&gt;
&lt;P&gt;You would need to get into something a bit more complex if you need different values compared for different variables. But if all of the values are considering the same boundary then MIN, or if you were looking for &amp;gt;400 for example, then MAX.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Mar 2019 22:29:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545084#M150759</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-03-21T22:29:28Z</dc:date>
    </item>
    <item>
      <title>Re: If-then statement: Multiple variables into one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545094#M150761</link>
      <description>&lt;P&gt;Hi Reeza,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks. I tried this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;array vl(20) vl1-vl20;&lt;/P&gt;&lt;P&gt;if&amp;nbsp; &amp;nbsp;. &amp;lt; vl(20) &amp;lt; 50 then vl_success = 1; else vl-success = 0;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But it seems that SAS interpreted it as&amp;nbsp; if (. &amp;lt; vl1 &amp;lt; 50) AND (. &amp;lt; vl2 &amp;lt; 50) ... AND (. &amp;lt; vl20 &amp;lt;50); so basically all values for vl_success are 0. I am not sure how to use other methods.&amp;nbsp;&lt;SPAN&gt;WHICHN sounds promising though. I will try it.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Mar 2019 23:19:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545094#M150761</guid>
      <dc:creator>Dinurik</dc:creator>
      <dc:date>2019-03-21T23:19:56Z</dc:date>
    </item>
    <item>
      <title>Re: If-then statement: Multiple variables into one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545120#M150766</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/267481"&gt;@Dinurik&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;For conditions as simple as posted in your sample you could go with the approach&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;proposed here:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Evaluate-all-array-elements-using-do-loop-to-create-a-single/m-p/545061#M150752" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Evaluate-all-array-elements-using-do-loop-to-create-a-single/m-p/545061#M150752&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For your example something like below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  array vl {20} 8 (20*100);
  vl5=.;
  output;
  vl6=49;
  output;
  stop;
run;

data want;
  set have;
  vl_success= ( min(of vl1-vl20)&amp;lt;50 );
run;

proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Missings aren't an issue in your case because:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;MIN function&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;returns a&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;missing value&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;(.) only if all arguments are&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;missing&lt;/STRONG&gt;&lt;SPAN&gt;.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Mar 2019 03:00:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545120#M150766</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-03-22T03:00:11Z</dc:date>
    </item>
    <item>
      <title>Re: If-then statement: Multiple variables into one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545236#M150802</link>
      <description>Your conditions can be simplified, checking if any value is less than 50 is equivalent to checking if the smallest value/minimum is less than 50. Otherwise, if it's not, that means no value is less than 50. So you can use the MIN() function to find the minimum and compare that against the 50 value.</description>
      <pubDate>Fri, 22 Mar 2019 14:46:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545236#M150802</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-03-22T14:46:51Z</dc:date>
    </item>
    <item>
      <title>Re: If-then statement: Multiple variables into one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545244#M150806</link>
      <description>&lt;P&gt;You have got solutions. The Minimum function has been used to get the Indicator (1 or 0). Since minimum function must inherently need to check all elements of the array, a slightly better approach for this particular problem is to stop the checking on the first occasion when the condition is met. The output below shows how many array elements are compared before exiting from the do loop (see I).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
input vl1 - vl5;
datalines;
100 34 49 60 200
200 60 10 70  80
 10 80 20 40 50
100 60 60 70 80
;
run;


data want;
   set have;
   array v[*] vl1 - vl5;
   do i = 1 to dim(v) until(ind = 1);
      ind = (v[i] &amp;lt; 50);
   end;
run;

Obs 	vl1 	vl2 	vl3 	vl4 	vl5 	i 	ind
1 	100 	34 	49 	60 	200 	2 	1
2 	200 	60 	10 	70 	80 	3 	1
3 	10 	80 	20 	40 	50 	1 	1
4 	100 	60 	60 	70 	80 	6 	0
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Mar 2019 15:04:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545244#M150806</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-03-22T15:04:07Z</dc:date>
    </item>
    <item>
      <title>Re: If-then statement: Multiple variables into one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545245#M150807</link>
      <description>&lt;P&gt;It worked! Thank you so much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Mar 2019 15:10:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545245#M150807</guid>
      <dc:creator>Dinurik</dc:creator>
      <dc:date>2019-03-22T15:10:58Z</dc:date>
    </item>
    <item>
      <title>Re: If-then statement: Multiple variables into one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545247#M150808</link>
      <description>&lt;P&gt;Thank you. Didn't know about this function. It's so useful!&lt;/P&gt;</description>
      <pubDate>Fri, 22 Mar 2019 15:11:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545247#M150808</guid>
      <dc:creator>Dinurik</dc:creator>
      <dc:date>2019-03-22T15:11:40Z</dc:date>
    </item>
    <item>
      <title>Re: If-then statement: Multiple variables into one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545248#M150809</link>
      <description>&lt;P&gt;Thank you! I like this logic, I will definitely use it for other tasks too.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Mar 2019 15:14:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545248#M150809</guid>
      <dc:creator>Dinurik</dc:creator>
      <dc:date>2019-03-22T15:14:35Z</dc:date>
    </item>
    <item>
      <title>Re: If-then statement: Multiple variables into one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545883#M151088</link>
      <description>&lt;P&gt;Thanks for the explanation! Very helpful.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2019 16:28:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-statement-Multiple-variables-into-one/m-p/545883#M151088</guid>
      <dc:creator>Dinurik</dc:creator>
      <dc:date>2019-03-25T16:28:36Z</dc:date>
    </item>
  </channel>
</rss>

