<?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 Array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601591#M174030</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Please find a question regarding array.&lt;/P&gt;
&lt;P&gt;Way3 (creating array) is not working.(get error).&lt;/P&gt;
&lt;P&gt;ERROR: Undeclared array referenced: var1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please don't ask me to change the structure of data or stop define macro parameters.&lt;/P&gt;
&lt;P&gt;There are macro parameters because it is a dynamic data that changes everyday.&lt;/P&gt;
&lt;P&gt;I just want to know how to write Array correctly in this example and what is wrong with it&lt;/P&gt;
&lt;P&gt;thank you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data ttt2;
set ttt;
/*Way1*/
/*PCT1=(X1906-X1905)/X1905*100;*/
/*PCT2=(X1907-X1906)/X1906*100;*/
/*PCT3=(X1908-X1907)/X1907*100;*/
/*PCT4=(X1909-X1908)/X1908*100;*/

/*Way2*/
/*PCT1=(X&amp;amp;m1.-X&amp;amp;m0.)/X&amp;amp;m0.*100;*/
/*PCT2=(X&amp;amp;m2.-X&amp;amp;m1.)/X&amp;amp;m1.*100;*/
/*PCT3=(X&amp;amp;m3.-X&amp;amp;m2.)/X&amp;amp;m2.*100;*/
/*PCT4=(X&amp;amp;m4.-X&amp;amp;m3.)/X&amp;amp;m3.*100;*/

/*Way3-There is an error and I want to learn why*/
Array Var PCT1-PCT4;
Do j=1 To 4;
var1(j)=(X&amp;amp;mj.-X&amp;amp;mj-1)/X&amp;amp;mj. ;
END;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 05 Nov 2019 09:00:32 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2019-11-05T09:00:32Z</dc:date>
    <item>
      <title>Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601591#M174030</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Please find a question regarding array.&lt;/P&gt;
&lt;P&gt;Way3 (creating array) is not working.(get error).&lt;/P&gt;
&lt;P&gt;ERROR: Undeclared array referenced: var1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please don't ask me to change the structure of data or stop define macro parameters.&lt;/P&gt;
&lt;P&gt;There are macro parameters because it is a dynamic data that changes everyday.&lt;/P&gt;
&lt;P&gt;I just want to know how to write Array correctly in this example and what is wrong with it&lt;/P&gt;
&lt;P&gt;thank you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data ttt2;
set ttt;
/*Way1*/
/*PCT1=(X1906-X1905)/X1905*100;*/
/*PCT2=(X1907-X1906)/X1906*100;*/
/*PCT3=(X1908-X1907)/X1907*100;*/
/*PCT4=(X1909-X1908)/X1908*100;*/

/*Way2*/
/*PCT1=(X&amp;amp;m1.-X&amp;amp;m0.)/X&amp;amp;m0.*100;*/
/*PCT2=(X&amp;amp;m2.-X&amp;amp;m1.)/X&amp;amp;m1.*100;*/
/*PCT3=(X&amp;amp;m3.-X&amp;amp;m2.)/X&amp;amp;m2.*100;*/
/*PCT4=(X&amp;amp;m4.-X&amp;amp;m3.)/X&amp;amp;m3.*100;*/

/*Way3-There is an error and I want to learn why*/
Array Var PCT1-PCT4;
Do j=1 To 4;
var1(j)=(X&amp;amp;mj.-X&amp;amp;mj-1)/X&amp;amp;mj. ;
END;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Nov 2019 09:00:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601591#M174030</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-11-05T09:00:32Z</dc:date>
    </item>
    <item>
      <title>Re: Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601592#M174031</link>
      <description>&lt;P&gt;Hint : read the error message then read the last lines of your code and try to figure out what is wrong.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Nov 2019 08:53:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601592#M174031</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-11-05T08:53:30Z</dc:date>
    </item>
    <item>
      <title>Re: Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601594#M174032</link>
      <description>&lt;P&gt;Sorry,&lt;/P&gt;
&lt;P&gt;I didn't understand how to solve it.&lt;/P&gt;
&lt;P&gt;I think that error&amp;nbsp; is related to j-1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Nov 2019 09:01:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601594#M174032</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-11-05T09:01:26Z</dc:date>
    </item>
    <item>
      <title>Re: Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601595#M174033</link>
      <description>&lt;P&gt;Compare the array name in the ERROR message to the array name in your statement.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Nov 2019 09:04:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601595#M174033</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-11-05T09:04:49Z</dc:date>
    </item>
    <item>
      <title>Re: Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601596#M174034</link>
      <description>&lt;P&gt;I found one error (by mistake i wrote VAR1 instead of VAR).&lt;/P&gt;
&lt;P&gt;Now, i still have error&lt;/P&gt;
&lt;P&gt;43 var(j)=(X&amp;amp;mj.-X&amp;amp;mj-1)/X&amp;amp;mj. ;&lt;BR /&gt;___ ___&lt;BR /&gt;22 22&lt;BR /&gt;201 201&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data ttt2;
set ttt;
/*Way1*/
/*PCT1=(X1906-X1905)/X1905*100;*/
/*PCT2=(X1907-X1906)/X1906*100;*/
/*PCT3=(X1908-X1907)/X1907*100;*/
/*PCT4=(X1909-X1908)/X1908*100;*/

/*Way2*/
/*PCT1=(X&amp;amp;m1.-X&amp;amp;m0.)/X&amp;amp;m0.*100;*/
/*PCT2=(X&amp;amp;m2.-X&amp;amp;m1.)/X&amp;amp;m1.*100;*/
/*PCT3=(X&amp;amp;m3.-X&amp;amp;m2.)/X&amp;amp;m2.*100;*/
/*PCT4=(X&amp;amp;m4.-X&amp;amp;m3.)/X&amp;amp;m3.*100;*/

/*Way3-There is an error and I want to learn why*/
Array Var PCT1-PCT4;
Do j=1 To dim(Var);
var(j)=(X&amp;amp;mj.-X&amp;amp;mj-1)/X&amp;amp;mj. ;
END;
Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Nov 2019 09:05:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601596#M174034</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-11-05T09:05:25Z</dc:date>
    </item>
    <item>
      <title>Re: Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601598#M174035</link>
      <description>&lt;P&gt;What is contained in the macro variable &amp;amp;mj? And do you have the variable names created by the use of that macro variable in your dataset?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS &lt;STRONG&gt;Use the {i} button for posting logs and log snippets.&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Nov 2019 09:08:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601598#M174035</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-11-05T09:08:24Z</dc:date>
    </item>
    <item>
      <title>Re: Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601608#M174037</link>
      <description>&lt;P&gt;1. Don't use round brackets but square or curly ones.&lt;/P&gt;
&lt;PRE&gt;var[j]=.....&lt;/PRE&gt;
&lt;P&gt;2. Is your formula really doing what you want it to do?&lt;/P&gt;
&lt;PRE&gt;var[j]=(X&amp;amp;mj.-X&amp;amp;mj-1)/X&amp;amp;mj. ;
...is the same as...
var[j]= -1/X&amp;amp;mj. ;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Nov 2019 10:03:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601608#M174037</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-11-05T10:03:51Z</dc:date>
    </item>
    <item>
      <title>Re: Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601620#M174038</link>
      <description>&lt;P&gt;Hello and thank you .&lt;/P&gt;
&lt;PRE&gt;var[j]=(X&amp;amp;mj.-X&amp;amp;mj-1)/X&amp;amp;mj. ;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;j-1 is the index of M macro varaible&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;(X&amp;amp;m1. - X&amp;amp;m0.) /X&amp;amp;m0.&lt;/P&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>Tue, 05 Nov 2019 10:27:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601620#M174038</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-11-05T10:27:31Z</dc:date>
    </item>
    <item>
      <title>Re: Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601624#M174041</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:
&lt;P&gt;j-1 is the index of M macro varaible&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Sure, but the values of DATA step variable &lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;j&lt;/FONT&gt;&lt;/STRONG&gt;, which are created during &lt;EM&gt;execution time&lt;/EM&gt;, won't be "inserted" into macro variable reference &lt;FONT face="courier new,courier"&gt;&amp;amp;m&lt;STRONG&gt;j&lt;/STRONG&gt;&lt;/FONT&gt;, which needs to be resolved already at &lt;EM&gt;compile time&lt;/EM&gt;. While you could use an awkward construct such as&lt;/P&gt;
&lt;PRE&gt;input(vvaluex(cat('X',symget(cat('m',j)))),32.)&lt;/PRE&gt;
&lt;P&gt;I'd rather recommend something like this (given your data structure and macro variables m0=1905, ..., m4=1909):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create test data for demonstration */

data ttt;
array a x1905-x1909 (100 105 120 150 160);
run;

/* Compute percentage changes */

%let d=4;

data ttt2(drop=j);
set ttt;
array pct[&amp;amp;d];
array v[0:&amp;amp;d] x&amp;amp;m0--x&amp;amp;&amp;amp;m&amp;amp;d;
do j=1 to &amp;amp;d;
  pct[j]=(v[j]/v[j-1]-1)*100;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note the requirements of the &lt;A href="https://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=p0wphcpsfgx6o7n1sjtqzizp1n39.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#p1cr0027z6qx4an1maoh0fyizqtt" target="_blank" rel="noopener"&gt;name range variable list&lt;/A&gt; in the definition of array &lt;FONT face="courier new,courier"&gt;v&lt;/FONT&gt; (which I used to avoid issues when the &lt;EM&gt;YYMM&lt;/EM&gt; list crosses a year boundary like &lt;STRONG&gt;19&lt;/STRONG&gt;12, &lt;STRONG&gt;20&lt;/STRONG&gt;01, ...).&lt;/P&gt;</description>
      <pubDate>Tue, 05 Nov 2019 11:07:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601624#M174041</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-11-05T11:07:20Z</dc:date>
    </item>
    <item>
      <title>Re: Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601635#M174046</link>
      <description>&lt;P&gt;Transpose your dataset to a long structure, and the whole thing is simple by using the lag() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input period $ x;
datalines;
1905 100
1906 200
1907 250
;

data want;
set have;
pct = (x - lag(x)) / x;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Nov 2019 11:56:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601635#M174046</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-11-05T11:56:43Z</dc:date>
    </item>
    <item>
      <title>Re: Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601694#M174075</link>
      <description>&lt;P&gt;This is much easier if you show the log. The log will indicate where the error is by where it appears and usually even has the line and column number so you can better identify the issue. Working with just an error message isn't enough.&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/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I found one error (by mistake i wrote VAR1 instead of VAR).&lt;/P&gt;
&lt;P&gt;Now, i still have error&lt;/P&gt;
&lt;P&gt;43 var(j)=(X&amp;amp;mj.-X&amp;amp;mj-1)/X&amp;amp;mj. ;&lt;BR /&gt;___ ___&lt;BR /&gt;22 22&lt;BR /&gt;201 201&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data ttt2;
set ttt;
/*Way1*/
/*PCT1=(X1906-X1905)/X1905*100;*/
/*PCT2=(X1907-X1906)/X1906*100;*/
/*PCT3=(X1908-X1907)/X1907*100;*/
/*PCT4=(X1909-X1908)/X1908*100;*/

/*Way2*/
/*PCT1=(X&amp;amp;m1.-X&amp;amp;m0.)/X&amp;amp;m0.*100;*/
/*PCT2=(X&amp;amp;m2.-X&amp;amp;m1.)/X&amp;amp;m1.*100;*/
/*PCT3=(X&amp;amp;m3.-X&amp;amp;m2.)/X&amp;amp;m2.*100;*/
/*PCT4=(X&amp;amp;m4.-X&amp;amp;m3.)/X&amp;amp;m3.*100;*/

/*Way3-There is an error and I want to learn why*/
Array Var PCT1-PCT4;
Do j=1 To dim(Var);
var(j)=(X&amp;amp;mj.-X&amp;amp;mj-1)/X&amp;amp;mj. ;
END;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Nov 2019 16:19:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array/m-p/601694#M174075</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-11-05T16:19:46Z</dc:date>
    </item>
  </channel>
</rss>

