<?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: How to create, initialize, and populate an array if the dimension of the array is a variable? in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786976#M40024</link>
    <description>&lt;P&gt;The data step complier needs to know the dimensions so it can finish compiling the data step.&amp;nbsp; So you cannot use the value of any variable to define an array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you have the value in a macro variable you should not have any trouble. The macro processor finishes converting your macro code into SAS code BEFORE that datastep compiler starts trying to understand what SAS code you want to run.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let n_rows=3;
%let n_cols=4;
data _null_;
  array test_array[&amp;amp;n_rows,&amp;amp;n_cols] (%eval(&amp;amp;n_rows*&amp;amp;n_cols)*0);
  do i=1 to dim(test_array,1);
    do j=1 to dim(test_array,2);
      test_array[i,j] = (i-1)*&amp;amp;n_cols + j;
      put i= j= test_array[i,j]=;
    end;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;1178    %let n_rows=3;
1179    %let n_cols=4;
1180    data _null_;
1181      array test_array[&amp;amp;n_rows,&amp;amp;n_cols] (%eval(&amp;amp;n_rows*&amp;amp;n_cols)*0);
1182      do i=1 to dim(test_array,1);
1183        do j=1 to dim(test_array,2);
1184          test_array[i,j] = (i-1)*&amp;amp;n_cols + j;
1185          put i= j= test_array[i,j]=;
1186        end;
1187      end;
1188    run;

i=1 j=1 test_array1=1
i=1 j=2 test_array2=2
i=1 j=3 test_array3=3
i=1 j=4 test_array4=4
i=2 j=1 test_array5=5
i=2 j=2 test_array6=6
i=2 j=3 test_array7=7
i=2 j=4 test_array8=8
i=3 j=1 test_array9=9
i=3 j=2 test_array10=10
i=3 j=3 test_array11=11
i=3 j=4 test_array12=12
&lt;/PRE&gt;</description>
    <pubDate>Tue, 21 Dec 2021 16:42:18 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-12-21T16:42:18Z</dc:date>
    <item>
      <title>How to create, initialize, and populate an array if the dimension of the array is a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786970#M40021</link>
      <description>&lt;P&gt;I don't recall how to create, initialize, and populate an array in SAS within a data step if the dimension of the array is a variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've experimented with using dim_array and the macro variable &amp;amp;dim_array_m in the following code but neither work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is much appreciated! Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data _null_;&lt;BR /&gt;&amp;nbsp; rows = 3;&lt;BR /&gt;&amp;nbsp; columns = 4;&lt;BR /&gt;&amp;nbsp; dim_array = rows*columns;&lt;BR /&gt;&amp;nbsp; put dim_array;&lt;BR /&gt;&amp;nbsp; /* %let dim_array_m = dim_array;*/&lt;BR /&gt;&amp;nbsp; /* %put &amp;amp;dim_array_m;*/&lt;BR /&gt;&amp;nbsp; array test_array{*} (dim_array_m*(0)); /* Create array and initialize with 12 zeroes. */&lt;BR /&gt;&amp;nbsp; do i=1 to dim_array; /* Populate array with values 1-12. */&lt;BR /&gt;&amp;nbsp; &amp;nbsp; test_array{i} = i;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; put test_array{i};&lt;BR /&gt;&amp;nbsp; end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Dec 2021 16:24:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786970#M40021</guid>
      <dc:creator>RobertWF1</dc:creator>
      <dc:date>2021-12-21T16:24:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to create, initialize, and populate an array if the dimension of the array is a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786973#M40022</link>
      <description>&lt;P&gt;What are the variable names in this array? Give examples, please.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Dec 2021 16:28:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786973#M40022</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-21T16:28:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to create, initialize, and populate an array if the dimension of the array is a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786974#M40023</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  rows = 3;
  columns = 4;
  dim_array = rows*columns;
  call symputx('dim_array', dim_array);
run;

data want;
set .....;
  array test_array{&amp;amp;dim_array} (&amp;amp;dim_array*(0)); /* Create array and initialize with 12 zeroes. */
  do i=1 to &amp;amp;dim_array; /* Populate array with values 1-12. */
    test_array{i} = i;
    put test_array{i};
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/408179"&gt;@RobertWF1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I don't recall how to create, initialize, and populate an array in SAS within a data step if the dimension of the array is a variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've experimented with using dim_array and the macro variable &amp;amp;dim_array_m in the following code but neither work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help is much appreciated! Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;BR /&gt;&amp;nbsp; rows = 3;&lt;BR /&gt;&amp;nbsp; columns = 4;&lt;BR /&gt;&amp;nbsp; dim_array = rows*columns;&lt;BR /&gt;&amp;nbsp; put dim_array;&lt;BR /&gt;&amp;nbsp; /* %let dim_array_m = dim_array;*/&lt;BR /&gt;&amp;nbsp; /* %put &amp;amp;dim_array_m;*/&lt;BR /&gt;&amp;nbsp; array test_array{*} (dim_array_m*(0)); /* Create array and initialize with 12 zeroes. */&lt;BR /&gt;&amp;nbsp; do i=1 to dim_array; /* Populate array with values 1-12. */&lt;BR /&gt;&amp;nbsp; &amp;nbsp; test_array{i} = i;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; put test_array{i};&lt;BR /&gt;&amp;nbsp; end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Dec 2021 16:31:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786974#M40023</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-12-21T16:31:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to create, initialize, and populate an array if the dimension of the array is a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786976#M40024</link>
      <description>&lt;P&gt;The data step complier needs to know the dimensions so it can finish compiling the data step.&amp;nbsp; So you cannot use the value of any variable to define an array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you have the value in a macro variable you should not have any trouble. The macro processor finishes converting your macro code into SAS code BEFORE that datastep compiler starts trying to understand what SAS code you want to run.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let n_rows=3;
%let n_cols=4;
data _null_;
  array test_array[&amp;amp;n_rows,&amp;amp;n_cols] (%eval(&amp;amp;n_rows*&amp;amp;n_cols)*0);
  do i=1 to dim(test_array,1);
    do j=1 to dim(test_array,2);
      test_array[i,j] = (i-1)*&amp;amp;n_cols + j;
      put i= j= test_array[i,j]=;
    end;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;1178    %let n_rows=3;
1179    %let n_cols=4;
1180    data _null_;
1181      array test_array[&amp;amp;n_rows,&amp;amp;n_cols] (%eval(&amp;amp;n_rows*&amp;amp;n_cols)*0);
1182      do i=1 to dim(test_array,1);
1183        do j=1 to dim(test_array,2);
1184          test_array[i,j] = (i-1)*&amp;amp;n_cols + j;
1185          put i= j= test_array[i,j]=;
1186        end;
1187      end;
1188    run;

i=1 j=1 test_array1=1
i=1 j=2 test_array2=2
i=1 j=3 test_array3=3
i=1 j=4 test_array4=4
i=2 j=1 test_array5=5
i=2 j=2 test_array6=6
i=2 j=3 test_array7=7
i=2 j=4 test_array8=8
i=3 j=1 test_array9=9
i=3 j=2 test_array10=10
i=3 j=3 test_array11=11
i=3 j=4 test_array12=12
&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Dec 2021 16:42:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786976#M40024</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-21T16:42:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to create, initialize, and populate an array if the dimension of the array is a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786978#M40025</link>
      <description>Do we need variable names or can we use test_array{1}, test_array{2}, etc. as the variables?</description>
      <pubDate>Tue, 21 Dec 2021 16:54:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786978#M40025</guid>
      <dc:creator>RobertWF1</dc:creator>
      <dc:date>2021-12-21T16:54:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to create, initialize, and populate an array if the dimension of the array is a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786979#M40026</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/408179"&gt;@RobertWF1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Do we need variable names or can we use test_array{1}, test_array{2}, etc. as the variables?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That's pretty much up to you ... but its really really really hard to give advice without concrete examples that illustrate the differences in variable names that you will see from data set to data set in the variable names that go into the array. We need a few concrete examples.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Dec 2021 16:58:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786979#M40026</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-21T16:58:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to create, initialize, and populate an array if the dimension of the array is a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786986#M40027</link>
      <description>Thanks Reeza!&lt;BR /&gt;&lt;BR /&gt;And looks like I can combine the two data steps together:&lt;BR /&gt;&lt;BR /&gt;data _null_;&lt;BR /&gt;rows = 3;&lt;BR /&gt;columns = 4;&lt;BR /&gt;dim_array = rows*columns;&lt;BR /&gt;call symputx('dim_array', dim_array);&lt;BR /&gt;array test_array{&amp;amp;dim_array} (&amp;amp;dim_array*(0)); /* Create array and initialize with 12 zeroes. */&lt;BR /&gt;do i=1 to &amp;amp;dim_array; /* Populate array with values 1-12. */&lt;BR /&gt;test_array{i} = i;&lt;BR /&gt;put test_array{i};&lt;BR /&gt;end;&lt;BR /&gt;run;</description>
      <pubDate>Tue, 21 Dec 2021 17:40:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786986#M40027</guid>
      <dc:creator>RobertWF1</dc:creator>
      <dc:date>2021-12-21T17:40:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to create, initialize, and populate an array if the dimension of the array is a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786987#M40028</link>
      <description>Thanks Tom!</description>
      <pubDate>Tue, 21 Dec 2021 17:42:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786987#M40028</guid>
      <dc:creator>RobertWF1</dc:creator>
      <dc:date>2021-12-21T17:42:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to create, initialize, and populate an array if the dimension of the array is a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786988#M40029</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/408179"&gt;@RobertWF1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thanks Reeza!&lt;BR /&gt;&lt;BR /&gt;And looks like I can combine the two data steps together:&lt;BR /&gt;&lt;BR /&gt;data _null_;&lt;BR /&gt;rows = 3;&lt;BR /&gt;columns = 4;&lt;BR /&gt;dim_array = rows*columns;&lt;BR /&gt;call symputx('dim_array', dim_array);&lt;BR /&gt;array test_array{&amp;amp;dim_array} (&amp;amp;dim_array*(0)); /* Create array and initialize with 12 zeroes. */&lt;BR /&gt;do i=1 to &amp;amp;dim_array; /* Populate array with values 1-12. */&lt;BR /&gt;test_array{i} = i;&lt;BR /&gt;put test_array{i};&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;If you create a macro variable it's not usable in that same data step. If it works in your tests, it's likely because it was defined before as well (initial test).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try it in a new session and see if your code still works.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Dec 2021 17:47:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786988#M40029</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-12-21T17:47:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to create, initialize, and populate an array if the dimension of the array is a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786989#M40030</link>
      <description>&lt;P&gt;No. No. No. No.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You cannot use the value of a macro variable you have not created yet to generate the code to create a macro variable.&amp;nbsp; The CALL SYMPUTX() function cannot travel back in time.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Dec 2021 17:47:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786989#M40030</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-21T17:47:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to create, initialize, and populate an array if the dimension of the array is a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786991#M40031</link>
      <description>&lt;P&gt;A few ideas in case they will help ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't want to name the variables, you don't have to.&amp;nbsp; The alternative is to create a temporary array, one that vanishes once the DATA step is over:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array t {100} _temporary_;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Just as with a regular array composed of variables,&amp;nbsp; you can assign initial values.&amp;nbsp; One major difference is that _temporary_ array elements are automaticvally retained.&amp;nbsp; Define enough variables that you won't need to use them all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Even if you want a two-dimensional array, you can use a one-dimensional array instead.&amp;nbsp; Just use a formula to indicate which array element you want to refer to.&amp;nbsp; For example, if you have a DATA step variable named ROW_SIZE, you can refer to the second element of the 4th row using:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;t{(4-1)*(row_size) + 2} = 0;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Dec 2021 18:04:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/786991#M40031</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-12-21T18:04:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to create, initialize, and populate an array if the dimension of the array is a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/787244#M40032</link>
      <description>Ah you're right, has to be two data steps (can both be _null_).&lt;BR /&gt;&lt;BR /&gt;It actually worked in one data step, but only because I had already run Reeza's code as two separate data steps so dim_array had been defined.</description>
      <pubDate>Thu, 23 Dec 2021 15:56:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-create-initialize-and-populate-an-array-if-the-dimension/m-p/787244#M40032</guid>
      <dc:creator>RobertWF1</dc:creator>
      <dc:date>2021-12-23T15:56:14Z</dc:date>
    </item>
  </channel>
</rss>

