<?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: Get the total number of dimensions in an array? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635712#M188799</link>
    <description>&lt;P&gt;If you want a cleaner version of that hack:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if _n_=1 then do while(not missing(dim(harray,ndims+1))); ndims+1; end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But it will still generate the nasty notes in the log.&lt;/P&gt;
&lt;PRE&gt;NOTE: Invalid argument to function DIM at line 243 column 39.
ndims=2
ndims=2 _ERROR_=1 _N_=1
NOTE: Mathematical operations could not be performed at the following places. The results of the
      operations have been set to missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 243:39
&lt;/PRE&gt;</description>
    <pubDate>Mon, 30 Mar 2020 02:31:43 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-03-30T02:31:43Z</dc:date>
    <item>
      <title>Get the total number of dimensions in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635699#M188787</link>
      <description>&lt;P&gt;If I have an array testarray(2,4,6,8); I can use dim(testarray,i) to get the dimension of each index but how do I get the total number of dimensions? (in this case I want to return a 4).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; I see ndims exits for other sas products but not for base sas.&amp;nbsp; currently I loop&amp;nbsp; i until I get a missing value from&amp;nbsp; dim(testarray,i) but that feels rather inelegant.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Mar 2020 23:52:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635699#M188787</guid>
      <dc:creator>weg</dc:creator>
      <dc:date>2020-03-29T23:52:09Z</dc:date>
    </item>
    <item>
      <title>Re: Get the total number of dimensions in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635705#M188793</link>
      <description>&lt;P&gt;Is this just a theoretical question? The person writing the data step needs to have defined the array, so they already know how many dimensions it has.&amp;nbsp; The DIM() function is useful because you can define an array without knowing the number of elements.&amp;nbsp; For example when using a variable list.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array _num _numeric_;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But you cannot define an array with more than one dimensions without actually specifying the dimensions.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Mar 2020 01:14:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635705#M188793</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-03-30T01:14:01Z</dc:date>
    </item>
    <item>
      <title>Re: Get the total number of dimensions in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635706#M188794</link>
      <description>&lt;P&gt;No, its not theoretical.&amp;nbsp; I have to write a macro that automates many different scripts that each use a lot of arrays of different sizes, usually between 2 and 10 different dimensions.&amp;nbsp; So I need to be able to get the number of dimensions for my macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I currently have hacked together and it works, but it is ugly and inefficient:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;retain ndims 0;
flag = 0;
do while (flag = 0);
	b = ndims + 1;
	if missing(dim(Harray,b)) then 
		flag = 1;
	else
			ndims = b;
		end;
	put ndims=;		&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Mar 2020 01:34:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635706#M188794</guid>
      <dc:creator>weg</dc:creator>
      <dc:date>2020-03-30T01:34:02Z</dc:date>
    </item>
    <item>
      <title>Re: Get the total number of dimensions in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635707#M188795</link>
      <description>Same logic applies for a macro.  If the macro created the data step then it knows how many dimensions it used when it defined the array.  If you are writing a macro that generates part of a data step then one of the inputs needs to be the number of dimensions of the array.</description>
      <pubDate>Mon, 30 Mar 2020 01:43:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635707#M188795</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-03-30T01:43:16Z</dc:date>
    </item>
    <item>
      <title>Re: Get the total number of dimensions in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635709#M188797</link>
      <description>No the macro is called within the data step, the arrays have already been created and populated and they are passed to the macro (among other things). I could make the size an argument to pass to the macro but if my macro can figure out the size then I should just do that.&lt;BR /&gt;&lt;BR /&gt;But I take it there is no easy function to call to do this?</description>
      <pubDate>Mon, 30 Mar 2020 02:03:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635709#M188797</guid>
      <dc:creator>weg</dc:creator>
      <dc:date>2020-03-30T02:03:46Z</dc:date>
    </item>
    <item>
      <title>Re: Get the total number of dimensions in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635710#M188798</link>
      <description>&lt;P&gt;Macros are not really called "in a data step". The macro processor runs first and then the data step compiler interprets the resulting code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I said if the macro only generates statements that can be embedded in a data step then just add a parameter to the macro so that the person (or macro) that is generating the data step can pass in the number.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  array myarray [3,4,5] _temporary_;
  %mymacro(name=myarray,ndim=3);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could make it easier on the coder by just passing in the same information they need to code to make the array and have the macro do the counting for them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  array myarray [3,4,5] _temporary_;
  %mymacro(array_definition=myarray[3,4,5] );
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 30 Mar 2020 02:19:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635710#M188798</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-03-30T02:19:29Z</dc:date>
    </item>
    <item>
      <title>Re: Get the total number of dimensions in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635712#M188799</link>
      <description>&lt;P&gt;If you want a cleaner version of that hack:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if _n_=1 then do while(not missing(dim(harray,ndims+1))); ndims+1; end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But it will still generate the nasty notes in the log.&lt;/P&gt;
&lt;PRE&gt;NOTE: Invalid argument to function DIM at line 243 column 39.
ndims=2
ndims=2 _ERROR_=1 _N_=1
NOTE: Mathematical operations could not be performed at the following places. The results of the
      operations have been set to missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 243:39
&lt;/PRE&gt;</description>
      <pubDate>Mon, 30 Mar 2020 02:31:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635712#M188799</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-03-30T02:31:43Z</dc:date>
    </item>
    <item>
      <title>Re: Get the total number of dimensions in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635713#M188800</link>
      <description>&lt;P&gt;I don't know if it's more efficient, but it's definitely less ugly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   do ndim=0 by 1 while (dim(harray,ndim+1)^=.);
   end;
   put ndim=;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 30 Mar 2020 02:33:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635713#M188800</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-03-30T02:33:00Z</dc:date>
    </item>
    <item>
      <title>Re: Get the total number of dimensions in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635773#M188823</link>
      <description>&lt;P&gt;To avoid "&lt;SPAN&gt;the nasty notes in the log" with your algorithm, you could use something like this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;_t=n(of harray[*])+nmiss(of harray[*]);
do ndim=1 by 1 until(_t=1);
  _t=_t/dim(harray,ndim);
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Obviously, the initialization of &lt;FONT face="courier new,courier"&gt;_t&lt;/FONT&gt; assumes a &lt;EM&gt;numeric&lt;/EM&gt; array. For a character array (in the absence of a character analog of the N function) you could use something like&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;_t=countw(catx('~',of harray[*]),'~')+cmiss(of harray[*]);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;or, if the defined length of the character variables is known&amp;nbsp;(e.g., stored in a variable &lt;FONT face="courier new,courier"&gt;len&lt;/FONT&gt;)&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;_t=lengthc(cat(of harray[*]))/len;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Mar 2020 11:19:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635773#M188823</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-03-30T11:19:22Z</dc:date>
    </item>
    <item>
      <title>Re: Get the total number of dimensions in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635803#M188830</link>
      <description>&lt;P&gt;OK that is super clever. Best solution to my problem thank you.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Mar 2020 13:23:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-the-total-number-of-dimensions-in-an-array/m-p/635803#M188830</guid>
      <dc:creator>weg</dc:creator>
      <dc:date>2020-03-30T13:23:58Z</dc:date>
    </item>
  </channel>
</rss>

