<?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: ERROR 124-185 when macro with array is invoked more than once in DATA step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/ERROR-124-185-when-macro-with-array-is-invoked-more-than-once-in/m-p/304422#M64799</link>
    <description>&lt;P&gt;Thank you for your help-- the macro&amp;nbsp;worked perfectly. For some reason, your original macro is not showing on this page. As a reference for others, I've retyped it here:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro dummylevel (root= , num=);&lt;BR /&gt;array d_&amp;amp;root. (*) &amp;amp;root._1 - &amp;amp;root._&amp;amp;num.;&lt;BR /&gt;do d__i = 1 to dim(d_&amp;amp;root.);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; d_&amp;amp;root.{d__i} = 0;&lt;BR /&gt;end;&lt;BR /&gt;if &amp;amp;root. ne . then d_&amp;amp;root.(&amp;amp;root.) = 1;&lt;BR /&gt;drop d__i;&lt;BR /&gt;%mend;&lt;/P&gt;</description>
    <pubDate>Thu, 13 Oct 2016 16:52:32 GMT</pubDate>
    <dc:creator>STTran</dc:creator>
    <dc:date>2016-10-13T16:52:32Z</dc:date>
    <item>
      <title>ERROR 124-185 when macro with array is invoked more than once in DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ERROR-124-185-when-macro-with-array-is-invoked-more-than-once-in/m-p/303901#M64627</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;I have 50 categorical variables for&amp;nbsp;which&amp;nbsp;I need to create dummy variables for each level of response. For instance, variable Q1 has 3 levels: 1,2,3. The macro DUMMYLEVEL below will create 3 dummy&amp;nbsp;variables for Q1:&amp;nbsp;Q1_1, Q1_2, and Q1_3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My codes below-- which invokes the macro DUMMYLEVEL twice in the DATA step-- produces the following error message:&amp;nbsp;&lt;/P&gt;&lt;P&gt;"ERROR 124-185: The variable D has already been defined."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This error only occurs when the macro DUMMYLEVEL is called more than once in the DATA step. Anybody have experience working around this error to achieve my goal of creating dummy variables for 50 categorical variables? Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* Macro to create dummy variables for ROOT variable with NUM levels&amp;nbsp;*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%MACRO DUMMYLEVEL (ROOT= , NUM= );&lt;BR /&gt;ARRAY D(*) &amp;amp;ROOT._1 - &amp;amp;ROOT._&amp;amp;NUM. (&amp;amp;NUM.*0);&lt;BR /&gt;&amp;nbsp; &amp;nbsp;IF &amp;amp;ROOT. NE . THEN D(&amp;amp;ROOT.)= 1;&amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;OUTPUT;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;IF &amp;amp;ROOT. NE . THEN D(&amp;amp;ROOT.)= 0;&lt;BR /&gt;%MEND;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* Sample DATA step: call macro DUMMYLEVEL twice */&lt;BR /&gt;DATA OUT;&lt;BR /&gt;SET IN&amp;nbsp;(KEEP= ID Q1--Q50);&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;%Dummylevel (root= Q1, num= 3);&lt;BR /&gt;&amp;nbsp; &amp;nbsp;%Dummylevel (root= Q2, num= 2);&lt;BR /&gt;RUN;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2016 23:34:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ERROR-124-185-when-macro-with-array-is-invoked-more-than-once-in/m-p/303901#M64627</guid>
      <dc:creator>STTran</dc:creator>
      <dc:date>2016-10-11T23:34:57Z</dc:date>
    </item>
    <item>
      <title>Re: ERROR 124-185 when macro with array is invoked more than once in DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ERROR-124-185-when-macro-with-array-is-invoked-more-than-once-in/m-p/303902#M64628</link>
      <description>&lt;P&gt;You need a unique array declaration for each macro call.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Although you can do this, have you looked into some of the other methods? Here are some references that illustrate alternatives.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.sas.com/content/iml/2016/02/22/create-dummy-variables-in-sas.html" target="_blank"&gt;http://blogs.sas.com/content/iml/2016/02/22/create-dummy-variables-in-sas.html&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/SAS-Statistical-Procedures/How-to-create-dummy-variables-Categorical-Variables/m-p/258702/highlight/true#M13685" target="_blank"&gt;https://communities.sas.com/t5/SAS-Statistical-Procedures/How-to-create-dummy-variables-Categorical-Variables/m-p/258702/highlight/true#M13685&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2016 23:47:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ERROR-124-185-when-macro-with-array-is-invoked-more-than-once-in/m-p/303902#M64628</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-11T23:47:18Z</dc:date>
    </item>
    <item>
      <title>Re: ERROR 124-185 when macro with array is invoked more than once in DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ERROR-124-185-when-macro-with-array-is-invoked-more-than-once-in/m-p/303971#M64655</link>
      <description>&lt;P&gt;Adapt your macro to create individually named arrays for each invocation:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Macro to create dummy variables for ROOT variable with NUM levels */

%macro dummylevel(root,num);
array d_&amp;amp;root.(*) &amp;amp;root._1 - &amp;amp;root._&amp;amp;num.;
do d__i = 1 to dim(d_&amp;amp;root.);
  d_&amp;amp;root.{d__i) = 0;
end;
if &amp;amp;root. ne . then d_&amp;amp;root.(&amp;amp;root.)= 1; 
drop d__i;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also note that I removed the output and reset to 0, and instead intialized thé whole array for each data step iteration. Otherwise you'd multiply your dataset observations.&lt;/P&gt;
&lt;P&gt;In example code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Q1 Q2;
cards;
1 2 .
2 1 1
3 2 2
;
run;

data want;
set have;
%dummylevel(Q1,3);
%dummylevel(Q2,3);
run;

proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;you then get this result:&lt;/P&gt;
&lt;PRE&gt;Obs    ID    Q1    Q2    Q1_1    Q1_2    Q1_3    Q2_1    Q2_2    Q2_3

 1      1     2     .      0       1       0       0       0       0 
 2      2     1     1      1       0       0       1       0       0 
 3      3     2     2      0       1       0       0       1       0 
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: Corrected to the proper macrocode.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Oct 2016 06:04:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ERROR-124-185-when-macro-with-array-is-invoked-more-than-once-in/m-p/303971#M64655</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-10-14T06:04:19Z</dc:date>
    </item>
    <item>
      <title>Re: ERROR 124-185 when macro with array is invoked more than once in DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ERROR-124-185-when-macro-with-array-is-invoked-more-than-once-in/m-p/303975#M64658</link>
      <description>&lt;P&gt;Sorry, I am not sure why all that is necessary? &amp;nbsp;Arrays can be defined with multiple dimensions, its then just the need to get your variables in the necessary order in the define context:&lt;/P&gt;
&lt;PRE&gt;data out;
  set in (keep=id q1--q50);
  array resp{50,3) $20;
  do i=1 to 50;
    resp{i,1}=...;
    resp{i,2}=...;
...
  end;
run;&lt;/PRE&gt;
&lt;P&gt;And please dont code in all upper case, its one of the worse visual things to do code.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2016 08:13:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ERROR-124-185-when-macro-with-array-is-invoked-more-than-once-in/m-p/303975#M64658</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-10-12T08:13:37Z</dc:date>
    </item>
    <item>
      <title>Re: ERROR 124-185 when macro with array is invoked more than once in DATA step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ERROR-124-185-when-macro-with-array-is-invoked-more-than-once-in/m-p/304422#M64799</link>
      <description>&lt;P&gt;Thank you for your help-- the macro&amp;nbsp;worked perfectly. For some reason, your original macro is not showing on this page. As a reference for others, I've retyped it here:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro dummylevel (root= , num=);&lt;BR /&gt;array d_&amp;amp;root. (*) &amp;amp;root._1 - &amp;amp;root._&amp;amp;num.;&lt;BR /&gt;do d__i = 1 to dim(d_&amp;amp;root.);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; d_&amp;amp;root.{d__i} = 0;&lt;BR /&gt;end;&lt;BR /&gt;if &amp;amp;root. ne . then d_&amp;amp;root.(&amp;amp;root.) = 1;&lt;BR /&gt;drop d__i;&lt;BR /&gt;%mend;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Oct 2016 16:52:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ERROR-124-185-when-macro-with-array-is-invoked-more-than-once-in/m-p/304422#M64799</guid>
      <dc:creator>STTran</dc:creator>
      <dc:date>2016-10-13T16:52:32Z</dc:date>
    </item>
  </channel>
</rss>

