<?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: Array for if then statement for large number of variables? in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Array-for-if-then-statement-for-large-number-of-variables/m-p/329930#M9687</link>
    <description>&lt;P&gt;That is exactly what arrays are designed for. &amp;nbsp;From your code it looks like you have one input array and two output arrays. &amp;nbsp;Note that I would change the naming of the output variables to have the numeric part as the suffix since then it is easier to use variable name lists.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array VAL A1-A4;
array DEN DEN_A1-DEN_A4 ;
array POS POS_A1-POS_A4 ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now just wrap your logic inside of a DO loop and replace your variable references with array references.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=1 to dim(VAL);
  if val(i) in (4,5) then pos(i)=1;
  else if val(i) in (1,2,3) then pos(i)=0;

  if val(i)=. then den(i)=0
  else den(i)=1;

end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 04 Feb 2017 00:51:46 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-02-04T00:51:46Z</dc:date>
    <item>
      <title>Array for if then statement for large number of variables?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Array-for-if-then-statement-for-large-number-of-variables/m-p/329862#M9684</link>
      <description>&lt;P&gt;I have a large number of variables for which I'd like to apply the same If then criteria:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	if A1 = 4 or A1 = 5 then
		A1_pos = 1;

	if A1 = 3 or A1 = 2 or A1 = 1 then
		A1_pos = 0;

	if A1 = . then
		A1_den = 0;
	else A1_den = 1;

	if A3 = 4 or A3 = 5 then
		A3_pos = 1;

	if A3 = 3 or A3 = 2 or A3 = 1 then
		A3_pos = 0;

	if A3 = . then
		A3_den = 0;
	else A3_den = 1;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rather than write out a large number of if then statements, I'm thinking an array would could down on my code and be a more efficient way to do this. &amp;nbsp; Any suggestions are greatly appreciated!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Fri, 03 Feb 2017 20:46:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Array-for-if-then-statement-for-large-number-of-variables/m-p/329862#M9684</guid>
      <dc:creator>simkinm2</dc:creator>
      <dc:date>2017-02-03T20:46:40Z</dc:date>
    </item>
    <item>
      <title>Re: Array for if then statement for large number of variables?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Array-for-if-then-statement-for-large-number-of-variables/m-p/329874#M9686</link>
      <description>&lt;P&gt;Something like this should get you started. Change the 10 the largest number of like variables. Assumes that you do NOT have a variable named "a" in the data. If you do you will need a different name for the first array. Also assumes that the A variables are consecutively numbered, if that is not the case then you need to provide a list that does match. Note that you can mix sequences and single values such as Array a a1-a10 a15&amp;nbsp; a20-a26;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   array a a1-a10;
   /* assumes the Pos and Den variables do not already exist. NOTE that your
   SAS code will run much smoother if you use the suffix as the number and not
   in the middle if the variables already exist you will need to list them out
   or possibly A1_Pos -- A10_Pos will work if they are in order in the data set
   (yes that is two dashes)*/ 
   array Pos_A {10};
   array Den_a {10};
   do i=1 to dim(a);
      if a[i] in (4,5) then Pos_A[i]=1;
      else if a[i] in (3,2,1) then Pos_A[i]=0;
      Den_A[i]= not missing(A[i]);
   end;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Feb 2017 19:04:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Array-for-if-then-statement-for-large-number-of-variables/m-p/329874#M9686</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-02-03T19:04:58Z</dc:date>
    </item>
    <item>
      <title>Re: Array for if then statement for large number of variables?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Array-for-if-then-statement-for-large-number-of-variables/m-p/329930#M9687</link>
      <description>&lt;P&gt;That is exactly what arrays are designed for. &amp;nbsp;From your code it looks like you have one input array and two output arrays. &amp;nbsp;Note that I would change the naming of the output variables to have the numeric part as the suffix since then it is easier to use variable name lists.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array VAL A1-A4;
array DEN DEN_A1-DEN_A4 ;
array POS POS_A1-POS_A4 ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now just wrap your logic inside of a DO loop and replace your variable references with array references.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=1 to dim(VAL);
  if val(i) in (4,5) then pos(i)=1;
  else if val(i) in (1,2,3) then pos(i)=0;

  if val(i)=. then den(i)=0
  else den(i)=1;

end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 04 Feb 2017 00:51:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Array-for-if-then-statement-for-large-number-of-variables/m-p/329930#M9687</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-02-04T00:51:46Z</dc:date>
    </item>
  </channel>
</rss>

