<?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 Returning the index of row in a multi-dimensional array. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Returning-the-index-of-row-in-a-multi-dimensional-array/m-p/631512#M187120</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I'm writing a program that stores lookup data in a multi-dimensional array and I need to return the row index of the array to eliminate hard coding of values.&amp;nbsp; The sas functions whichc and whichn are designed for this but they return the position of the lookup value expressed as a list.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	array foo [3,5] $2 _temporary_
	(
		'a1' 'b1' 'c1' 'd1'
		'a2' 'b2' 'c2' 'd2'
		'a3' 'b3' 'c3' 'd3'		
	);&lt;BR /&gt;	drop i;
	do i = 1 to dim(foo);
		key = foo[i,1];
		does_lkup_as_1_dimension = whichc(key,of foo[*]);
		real_index = int(whichc(key,of foo[*])/dim2(foo))+1;
		output;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The above code returns the following values for a lookup on a 3 * 4 dimension array in the variable 'does_lkup_as_1_dimension'.&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;key&lt;/TD&gt;&lt;TD&gt;does_lkup_as_1_dimension&lt;/TD&gt;&lt;TD&gt;real_index&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a2&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a3&lt;/TD&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using this as a workaround which returns the value I want.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class="sas"&gt;real_index = int(whichc(key,of foo[*])/dim2(foo))+1;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Is there a better way with native SAS functions?&amp;nbsp; I'm surprised I have not needed to do this until now and I expect I'm not the first!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;Cam&lt;/P&gt;</description>
    <pubDate>Thu, 12 Mar 2020 11:00:33 GMT</pubDate>
    <dc:creator>foobarbaz</dc:creator>
    <dc:date>2020-03-12T11:00:33Z</dc:date>
    <item>
      <title>Returning the index of row in a multi-dimensional array.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Returning-the-index-of-row-in-a-multi-dimensional-array/m-p/631512#M187120</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I'm writing a program that stores lookup data in a multi-dimensional array and I need to return the row index of the array to eliminate hard coding of values.&amp;nbsp; The sas functions whichc and whichn are designed for this but they return the position of the lookup value expressed as a list.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	array foo [3,5] $2 _temporary_
	(
		'a1' 'b1' 'c1' 'd1'
		'a2' 'b2' 'c2' 'd2'
		'a3' 'b3' 'c3' 'd3'		
	);&lt;BR /&gt;	drop i;
	do i = 1 to dim(foo);
		key = foo[i,1];
		does_lkup_as_1_dimension = whichc(key,of foo[*]);
		real_index = int(whichc(key,of foo[*])/dim2(foo))+1;
		output;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The above code returns the following values for a lookup on a 3 * 4 dimension array in the variable 'does_lkup_as_1_dimension'.&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;key&lt;/TD&gt;&lt;TD&gt;does_lkup_as_1_dimension&lt;/TD&gt;&lt;TD&gt;real_index&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a2&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a3&lt;/TD&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using this as a workaround which returns the value I want.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class="sas"&gt;real_index = int(whichc(key,of foo[*])/dim2(foo))+1;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Is there a better way with native SAS functions?&amp;nbsp; I'm surprised I have not needed to do this until now and I expect I'm not the first!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;Cam&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 11:00:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Returning-the-index-of-row-in-a-multi-dimensional-array/m-p/631512#M187120</guid>
      <dc:creator>foobarbaz</dc:creator>
      <dc:date>2020-03-12T11:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: Returning the index of row in a multi-dimensional array.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Returning-the-index-of-row-in-a-multi-dimensional-array/m-p/631523#M187124</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	array foo [3,5] $2 _temporary_
	(
		'a1' 'b1' 'c1' 'd1'
		'a2' 'b2' 'c2' 'd2'
		'a3' 'b3' 'c3' 'd3'		
	);	drop i;
	do i = 1 to dim(foo);
		key = foo[i,1];
		do j=1 to dim2(foo);
		 if foo[i,j]=key then do;
		  does_lkup_as_1_dimension=i ;
		  output;
		 end;
		end;
	end;
run;
proc print;run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Mar 2020 11:45:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Returning-the-index-of-row-in-a-multi-dimensional-array/m-p/631523#M187124</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-03-12T11:45:08Z</dc:date>
    </item>
    <item>
      <title>Re: Returning the index of row in a multi-dimensional array.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Returning-the-index-of-row-in-a-multi-dimensional-array/m-p/631565#M187142</link>
      <description>&lt;P&gt;Is there a reason you define the array as 3*5, but provide values implying 3*4?&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 14:15:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Returning-the-index-of-row-in-a-multi-dimensional-array/m-p/631565#M187142</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-03-12T14:15:39Z</dc:date>
    </item>
    <item>
      <title>Re: Returning the index of row in a multi-dimensional array.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Returning-the-index-of-row-in-a-multi-dimensional-array/m-p/631670#M187173</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;No it's just a typo on my part for the example.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 18:45:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Returning-the-index-of-row-in-a-multi-dimensional-array/m-p/631670#M187173</guid>
      <dc:creator>foobarbaz</dc:creator>
      <dc:date>2020-03-12T18:45:32Z</dc:date>
    </item>
    <item>
      <title>Re: Returning the index of row in a multi-dimensional array.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Returning-the-index-of-row-in-a-multi-dimensional-array/m-p/631672#M187175</link>
      <description>&lt;P&gt;I think what you want is the CEILing function applied to the whichc value divided by the row length (dim(foo,2)):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	array foo [3,4] $2 _temporary_
	(
		'a1' 'b1' 'c1' 'd1'
		'a2' 'b2' 'c2' 'd2'
		'a3' 'b3' 'c3' 'd3'		
	);

    do key='a1','a2','b3';
      row_index=ceil(whichc(key,of foo{*})/dim(foo,2));
      col_index=mod(whichc(key,of foo{*})-1,dim(foo,2))+1;
      output;
    end;
run;
proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;No need to add 1 for row index.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 19:34:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Returning-the-index-of-row-in-a-multi-dimensional-array/m-p/631672#M187175</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-03-12T19:34:12Z</dc:date>
    </item>
    <item>
      <title>Re: Returning the index of row in a multi-dimensional array.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Returning-the-index-of-row-in-a-multi-dimensional-array/m-p/631674#M187176</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;&amp;nbsp; I'm merely looking to return the index of the array in the most efficient way possible so I can do additional lookups on the array.&amp;nbsp; I've used whichc as it's essentially a one liner vs. nested loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For context the real array in my code contains a key value, a regex pattern and then a series of flags that is being used to tokenize the contents of a file (A SAS Log).&amp;nbsp; An array is used vs. hash table because the order of the regex patterns is important.&amp;nbsp; My code applies a token to each line of the file (first dimension of the array).&amp;nbsp; I then need to re-use the array to apply a series of business rules which the flags in the array are used for. Thanks for responding though.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 18:57:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Returning-the-index-of-row-in-a-multi-dimensional-array/m-p/631674#M187176</guid>
      <dc:creator>foobarbaz</dc:creator>
      <dc:date>2020-03-12T18:57:26Z</dc:date>
    </item>
    <item>
      <title>Re: Returning the index of row in a multi-dimensional array.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Returning-the-index-of-row-in-a-multi-dimensional-array/m-p/631675#M187177</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;That makes a lot more sense.&amp;nbsp; The int(...) + 1 part felt like a hack to me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2020 18:59:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Returning-the-index-of-row-in-a-multi-dimensional-array/m-p/631675#M187177</guid>
      <dc:creator>foobarbaz</dc:creator>
      <dc:date>2020-03-12T18:59:19Z</dc:date>
    </item>
  </channel>
</rss>

