<?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 Is there a way to check if array already exists in a data step? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-check-if-array-already-exists-in-a-data-step/m-p/638163#M189752</link>
    <description>&lt;P&gt;I have a macro program that performs some calculations on an array and sticks them in another array.&amp;nbsp; I don't want to declare the array if the macro has already been called, and just skip to changing the values.&lt;/P&gt;&lt;PRE&gt;if not( /* &amp;amp;arrayname exists /* ) then
				do;
					array &amp;amp;arrayname._calculations [&amp;amp;ndim] _TEMPORARY_; 
                              end;&lt;/PRE&gt;&lt;P&gt;Is there a function or some easy way to check this?&lt;/P&gt;</description>
    <pubDate>Tue, 07 Apr 2020 20:58:15 GMT</pubDate>
    <dc:creator>weg</dc:creator>
    <dc:date>2020-04-07T20:58:15Z</dc:date>
    <item>
      <title>Is there a way to check if array already exists in a data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-check-if-array-already-exists-in-a-data-step/m-p/638163#M189752</link>
      <description>&lt;P&gt;I have a macro program that performs some calculations on an array and sticks them in another array.&amp;nbsp; I don't want to declare the array if the macro has already been called, and just skip to changing the values.&lt;/P&gt;&lt;PRE&gt;if not( /* &amp;amp;arrayname exists /* ) then
				do;
					array &amp;amp;arrayname._calculations [&amp;amp;ndim] _TEMPORARY_; 
                              end;&lt;/PRE&gt;&lt;P&gt;Is there a function or some easy way to check this?&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2020 20:58:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-check-if-array-already-exists-in-a-data-step/m-p/638163#M189752</guid>
      <dc:creator>weg</dc:creator>
      <dc:date>2020-04-07T20:58:15Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to check if array already exists in a data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-check-if-array-already-exists-in-a-data-step/m-p/638170#M189756</link>
      <description>&lt;P&gt;Arrays get declared during compilation time and not execution time. The IF condition comes only into play during execution time. For this reason declaring an array conditionally on SAS data step level is not possible.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will need to implement this on macro level meaning that your macro needs logic to only generate the SAS array statement the first time you call it.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2020 22:18:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-check-if-array-already-exists-in-a-data-step/m-p/638170#M189756</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-04-07T22:18:27Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to check if array already exists in a data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-check-if-array-already-exists-in-a-data-step/m-p/638176#M189759</link>
      <description>&lt;P&gt;You can use VARRAY() to check if a variable belongs to an array, but not if the array exists AFAIK.&lt;/P&gt;
&lt;P&gt;Given your array declaration which doesn't use variable names it's a bit of a harder process.&amp;nbsp;&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/309205"&gt;@weg&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a macro program that performs some calculations on an array and sticks them in another array.&amp;nbsp; I don't want to declare the array if the macro has already been called, and just skip to changing the values.&lt;/P&gt;
&lt;PRE&gt;if not( /* &amp;amp;arrayname exists /* ) then
				do;
					array &amp;amp;arrayname._calculations [&amp;amp;ndim] _TEMPORARY_; 
                              end;&lt;/PRE&gt;
&lt;P&gt;Is there a function or some easy way to check this?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2020 22:05:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-check-if-array-already-exists-in-a-data-step/m-p/638176#M189759</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-04-07T22:05:11Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to check if array already exists in a data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-check-if-array-already-exists-in-a-data-step/m-p/638184#M189766</link>
      <description>&lt;P&gt;You cannot use DATA step code to conditionally generate an ARRAY statement. ARRAY statements only have an effect during the compilation of the data step.&amp;nbsp; Your macro needs to know whether or not it has already generated that ARRAY statement for this data step.&amp;nbsp; Or just have the macro generate a NEW array name each time it is called. For example by using the value of &amp;amp;SYSINDEX in the name.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array &amp;amp;arrayname._&amp;amp;sysindex._calc [&amp;amp;ndim] _TEMPORARY_; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2020 22:31:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-check-if-array-already-exists-in-a-data-step/m-p/638184#M189766</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-07T22:31:21Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to check if array already exists in a data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-check-if-array-already-exists-in-a-data-step/m-p/638206#M189778</link>
      <description>&lt;P&gt;edit-- benchmarking the performance doesn't show a real different in speed than just creating new arrays each time, so that seems like the simplest and most foolproof solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I made a new macro variable for each array input that counts how many times the macro function has been called and initalize based off of that, and then I need to run a macro at the end to clear all these "temp" macro variables from the symbol table.&amp;nbsp; It seems to be working, other than the fact that if it crashes halfway through I have to manually run the cleanup code by itself before the program runs.&amp;nbsp; I'm not sure if this is better or worse than just making separate arrays each time the function is called, but is there any obvious thing I could screw up by doing things this way?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro testmac(arrayname);
	%let ndim = 5;
	%if %symexist (_i_&amp;amp;arrayname) %then %let _i_&amp;amp;arrayname = %eval(&amp;amp;&amp;amp;_i_&amp;amp;arrayname+1);
	%else 
		%do;
			%global _i_&amp;amp;arrayname;
			%let _i_&amp;amp;arrayname=1;
		%end;

	%if &amp;amp;&amp;amp;_i_&amp;amp;arrayname=1 %then 
		%do;
			array &amp;amp;arrayname._calc[&amp;amp;ndim] ; /* its _temporary_ always, I just wanted to see output */
		%end;
		
	do i = 1 to &amp;amp;ndim;
		&amp;amp;arrayname._calc[i] = 5;
	end;
		
		
%mend;			

%macro rmHDPmac;
   	options nonotes;
  	%local vars;
 
  	proc sql noprint;
      	     select name into: vars separated by ' '
         	  from dictionary.macros
            	      where scope='GLOBAL' 
			   and name contains '_i_';
   	quit;
 
 	data _null_;
   	call execute('%symdel &amp;amp;vars;');
   	run;
 
   	options notes;
    	%put NOTE: Macro variables deleted: &amp;amp;vars;
 
%mend rmHDPmac;


data test;
%testmac(arrayname=array1);
%testmac(arrayname=array1);
%testmac(arrayname=array1);
%testmac(arrayname=array2);

run;	

%rmHDPmac;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Apr 2020 00:50:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-check-if-array-already-exists-in-a-data-step/m-p/638206#M189778</guid>
      <dc:creator>weg</dc:creator>
      <dc:date>2020-04-08T00:50:55Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to check if array already exists in a data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-check-if-array-already-exists-in-a-data-step/m-p/638216#M189783</link>
      <description>I don't get how you are telling the macro how large to make the array.  Your posted code uses a fixed value of 5 for NDIM.  What does that number mean in terms of the larger problem. Where is it defined? What happens if NDIM changes between calls?  Do you want the DO loop to use DIM(arrayname) instead of &amp;amp;NDIM as the upper bound?&lt;BR /&gt;One risk is that forget to run the clean up step so that you never end up ever generating the array statement.  Then the generated SAS code will fail.&lt;BR /&gt;Another is whether these separate calls actually WANT to use the same array or not.  What if they want to use it for different things? And want to put different values into the variables in the array.&lt;BR /&gt;This whole series of questions in this vein you have asked about all really look like XY problems to me. &lt;A href="http://xyproblem.info/" target="_blank"&gt;http://xyproblem.info/&lt;/A&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 08 Apr 2020 01:27:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-check-if-array-already-exists-in-a-data-step/m-p/638216#M189783</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-08T01:27:59Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to check if array already exists in a data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-check-if-array-already-exists-in-a-data-step/m-p/638220#M189785</link>
      <description>&lt;P&gt;Thanks for the help, I ended up using the SYSINDEX method as it didn't substantially increase the run time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are right that I have to calculate NDIMS, arrayvalues etc I have all of that tested and working fine, the only hitch was that last array generation part and it didn't seem that the ndims or actual values of the array would change the process of initializing the new arrays themselves which is the only thing I have trouble with. I definitely understand where you are coming from by giving the full problem and code, but I &lt;STRONG&gt;have&lt;/STRONG&gt; to give a minimum working example because even if I was authorized to share a the input and output to the full macro it wont make sense anyway on its own, and it has to interface with code people have already written and can't change. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the macros I've needed to make over the past week are complete and working as of now, and I am really grateful for the help.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Apr 2020 02:35:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-check-if-array-already-exists-in-a-data-step/m-p/638220#M189785</guid>
      <dc:creator>weg</dc:creator>
      <dc:date>2020-04-08T02:35:55Z</dc:date>
    </item>
  </channel>
</rss>

