<?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 Using arrays in a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-arrays-in-a-macro/m-p/438114#M109208</link>
    <description>&lt;P&gt;I am trying to run a macro, but I am getting this message: "Array subscript out of range at line 12 column 31". What am I doing wrong? I am attaching the program, and the data to this post. I have already checked if the number of variables match my array and do definitions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the part of the macro that I am having problems (please see files for the entire macro, and &lt;STRONG&gt;note that this section is inside of a macro&lt;/STRONG&gt;).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array change {11}  Adjusted_gross_income_category Number_of_returns Adjusted_gross_income
Wages_Number_of_returns Wages_Amount UE_comp_number_of_returns UE_comp_amount
SS_number_benefits_AGI SS_amount_benefits_AGI Taxable_inc_number_returns Taxable_amount
;
%do i = 1 %to 11;
  %if change{i} = "**" %then change{i}=" ";
%end;
array numeric_change {10} Number_of_returns Adjusted_gross_income Wages_Number_of_returns
Wages_Amount UE_comp_number_of_returns UE_comp_amount SS_number_benefits_AGI SS_amount_benefits_AGI
Taxable_inc_number_returns Taxable_amount
;
array new_numeric {10} Number_of_returns_2 Adjusted_gross_income_2 Wages_Number_of_returns_2
Wages_Amount_2 UE_comp_number_of_returns_2 UE_comp_amount_2 SS_number_benefits_AGI_2
SS_amount_benefits_AGI_2 Taxable_inc_number_returns_2 Taxable_amount_2
;
%do i = 1 %to 10;
  new_numeric{i}=input(numeric_change{i},comma12.);
%end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 20 Feb 2018 17:30:08 GMT</pubDate>
    <dc:creator>priscilabaddouh</dc:creator>
    <dc:date>2018-02-20T17:30:08Z</dc:date>
    <item>
      <title>Using arrays in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-arrays-in-a-macro/m-p/438114#M109208</link>
      <description>&lt;P&gt;I am trying to run a macro, but I am getting this message: "Array subscript out of range at line 12 column 31". What am I doing wrong? I am attaching the program, and the data to this post. I have already checked if the number of variables match my array and do definitions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the part of the macro that I am having problems (please see files for the entire macro, and &lt;STRONG&gt;note that this section is inside of a macro&lt;/STRONG&gt;).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array change {11}  Adjusted_gross_income_category Number_of_returns Adjusted_gross_income
Wages_Number_of_returns Wages_Amount UE_comp_number_of_returns UE_comp_amount
SS_number_benefits_AGI SS_amount_benefits_AGI Taxable_inc_number_returns Taxable_amount
;
%do i = 1 %to 11;
  %if change{i} = "**" %then change{i}=" ";
%end;
array numeric_change {10} Number_of_returns Adjusted_gross_income Wages_Number_of_returns
Wages_Amount UE_comp_number_of_returns UE_comp_amount SS_number_benefits_AGI SS_amount_benefits_AGI
Taxable_inc_number_returns Taxable_amount
;
array new_numeric {10} Number_of_returns_2 Adjusted_gross_income_2 Wages_Number_of_returns_2
Wages_Amount_2 UE_comp_number_of_returns_2 UE_comp_amount_2 SS_number_benefits_AGI_2
SS_amount_benefits_AGI_2 Taxable_inc_number_returns_2 Taxable_amount_2
;
%do i = 1 %to 10;
  new_numeric{i}=input(numeric_change{i},comma12.);
%end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2018 17:30:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-arrays-in-a-macro/m-p/438114#M109208</guid>
      <dc:creator>priscilabaddouh</dc:creator>
      <dc:date>2018-02-20T17:30:08Z</dc:date>
    </item>
    <item>
      <title>Re: Using arrays in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-arrays-in-a-macro/m-p/438167#M109212</link>
      <description>&lt;P&gt;I have no idea what the overall problems are but the mistake in the snippet you posted was using macro&amp;nbsp;code&amp;nbsp;where you needed to use data step code.&amp;nbsp; For example this statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  %if change{i} = "**" %then change{i}=" ";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The condition in the %IF statement is always false since the letter c does not equal a double quote character.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you wanted was this data step code that could run for each iteration of the data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i = 1 to 11;
  if change{i} = "**" then change{i}=" ";
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Feb 2018 16:02:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-arrays-in-a-macro/m-p/438167#M109212</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-02-17T16:02:42Z</dc:date>
    </item>
    <item>
      <title>Re: Using arrays in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-arrays-in-a-macro/m-p/438733#M109413</link>
      <description>&lt;P&gt;Hi Tom. Thank you for answering me! I copied and pasted only part of the code, and then uploaded the entire code (the entire macro)&amp;nbsp; as one of my uploaded files. Are you able to see it? So actually, the macro notation is correct, since it is inside of a macro. But I have not figured out what is the real issue yet...&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2018 17:28:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-arrays-in-a-macro/m-p/438733#M109413</guid>
      <dc:creator>priscilabaddouh</dc:creator>
      <dc:date>2018-02-20T17:28:19Z</dc:date>
    </item>
    <item>
      <title>Re: Using arrays in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-arrays-in-a-macro/m-p/438734#M109414</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/146623"&gt;@priscilabaddouh&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi Tom. Thank you for answering me! I copied and pasted only part of the code, and then uploaded the entire code (the entire macro)&amp;nbsp; as one of my uploaded files. Are you able to see it? So actually, the macro notation is correct, since it is inside of a macro. But I have not figured out what is the real issue yet...&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This loop that I pointed to before is not doing anything at all.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i = 1 %to 11;
  %if change{i} = "**" %then change{i}=" ";
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the first time through the loop the macro variable I is set to 1. Then the %IF statement compares the string 'change{i}' to the string '"**"' and finds that they are different so SAS statement 'change{i}=" "' is not generated.&amp;nbsp; &amp;nbsp;The same thing happens eleven times since nothing inside the loop is referencing the the macro variable I.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general macro code is used to generate SAS code. So make sure you are generating valid SAS code.&amp;nbsp; Don't change your SAS IF statements into macro %IF statement just because you are using a macro to generate the SAS code. You only need to use %IF if you want to conditionally generate some code.&amp;nbsp; The code your macro generates still needs to be valid SAS code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2018 17:40:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-arrays-in-a-macro/m-p/438734#M109414</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-02-20T17:40:01Z</dc:date>
    </item>
    <item>
      <title>Re: Using arrays in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-arrays-in-a-macro/m-p/438841#M109458</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/146623"&gt;@priscilabaddouh&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;was giving you the right advice.&amp;nbsp; But I believe the error message you got arose from this code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;BR /&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp; %do i = 1 %to 10;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; new_numeric{i}=input(numeric_change{i},comma12.);&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp; %end;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;which executes the statement&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp; new_numeric{i}=input(numeric_change{i},comma12.);&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;ten times, without changing the value of i.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a result, the variable i may have a missing value, or some value other than the natural numbers 1 through 10.&amp;nbsp; Hence the "array index out of range" message.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now the %do I=1 %to 10 statement changes the &lt;EM&gt;&lt;STRONG&gt;macrovar i&lt;/STRONG&gt;&lt;/EM&gt; (not the sas variable i).&amp;nbsp; In that case, the macrovar&amp;nbsp;would have to be referred to with an ampersand, as in&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp; new_numeric{&amp;amp;i}=input(numeric_change{&amp;amp;i},comma12.);&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;which in turn would generate these ten lines of sas code:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new_numeric{1}=input(numeric_change{1},comma12.);&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new_numeric{2}=input(numeric_change{2},comma12.);&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ....&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new_numeric{9}=input(numeric_change{9},comma12.);&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new_numeric{10}=input(numeric_change{10},comma12.);&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; points out, that's just not good coding (imagine if the loop were 1 to 200).&amp;nbsp; Instead change both of your loops from&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp; %do i=1 %to ....&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;to&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp; do i=1 to ...&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;and of course change the corresponding %end to end.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The benefit is you will not be populating your program with excessives lines of code.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2018 01:43:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-arrays-in-a-macro/m-p/438841#M109458</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-02-21T01:43:27Z</dc:date>
    </item>
    <item>
      <title>Re: Using arrays in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-arrays-in-a-macro/m-p/439111#M109535</link>
      <description>Thank you, I will try it soon. I am still a beginner, so I really appreciate all help.</description>
      <pubDate>Wed, 21 Feb 2018 23:08:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-arrays-in-a-macro/m-p/439111#M109535</guid>
      <dc:creator>priscilabaddouh</dc:creator>
      <dc:date>2018-02-21T23:08:01Z</dc:date>
    </item>
    <item>
      <title>Re: Using arrays in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-arrays-in-a-macro/m-p/439114#M109536</link>
      <description>&lt;P&gt;Now I understand what you mean, Tom. Thank you so much! I have just started learning macro by myself. So this is really helpful!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2018 23:19:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-arrays-in-a-macro/m-p/439114#M109536</guid>
      <dc:creator>priscilabaddouh</dc:creator>
      <dc:date>2018-02-21T23:19:27Z</dc:date>
    </item>
  </channel>
</rss>

