<?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: There was 1 unclosed DO block. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580422#M164880</link>
    <description>I ran this and this works too, sadly I haven't reached the stage where I could use arrays. But I've made a note of it for future learning, this is great. Thank you!</description>
    <pubDate>Sun, 11 Aug 2019 18:34:18 GMT</pubDate>
    <dc:creator>AJ_Brien</dc:creator>
    <dc:date>2019-08-11T18:34:18Z</dc:date>
    <item>
      <title>Error: There was 1 unclosed DO block.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580320#M164836</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just trying to learn loops, can't figure out why it keeps giving me this error even though there are no unclosed loops. Thanks in advance for pointing my mistake out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data main;
infile datalines; 
input acc $19.;
obs=_n_; 
datalines; 
12344567969705
56754846834684
12344567969715
56754846834694
56754846834696
; 
run;

proc rank data=main out=main1 groups=2;                               
     var obs;                                                          
     ranks rank;                                                      
  run;

%macro create (n);
  data main_&amp;amp;n.;
  set main1;
  if rank = &amp;amp;n. then output;
run;
%mend;

data _null_;
i=0;
do until (i=2);
%create(i);
i=i+1;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Log:&lt;/P&gt;&lt;P&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.00 seconds&lt;BR /&gt;cpu time 0.00 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;NOTE: Line generated by the invoked macro "CREATE".&lt;BR /&gt;2174 data main_&amp;amp;n.; set main1; if rank = &amp;amp;n. then output; run;&lt;BR /&gt;-&lt;BR /&gt;117&lt;/P&gt;&lt;P&gt;ERROR 117-185: There was 1 unclosed DO block.&lt;/P&gt;&lt;P&gt;NOTE: Variable i is uninitialized.&lt;BR /&gt;NOTE: There were 5 observations read from the data set WORK.MAIN1.&lt;BR /&gt;NOTE: The data set WORK.MAIN_I has 0 observations and 4 variables.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.00 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;2175 i=i+1;&lt;BR /&gt;-&lt;BR /&gt;180&lt;/P&gt;&lt;P&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/P&gt;&lt;P&gt;2176 end;&lt;BR /&gt;---&lt;BR /&gt;180&lt;/P&gt;&lt;P&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/P&gt;</description>
      <pubDate>Sat, 10 Aug 2019 03:06:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580320#M164836</guid>
      <dc:creator>AJ_Brien</dc:creator>
      <dc:date>2019-08-10T03:06:28Z</dc:date>
    </item>
    <item>
      <title>Re: Error: There was 1 unclosed DO block.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580327#M164840</link>
      <description>&lt;P&gt;You cannot nest data steps, but that is what your macro does. The "data" statement created by the macro ends the "outer" step before the "end" statement could be compiled.&lt;/P&gt;</description>
      <pubDate>Sat, 10 Aug 2019 04:42:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580327#M164840</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-08-10T04:42:56Z</dc:date>
    </item>
    <item>
      <title>Re: Error: There was 1 unclosed DO block.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580328#M164841</link>
      <description>&lt;P&gt;If you are just learning loops don't complicate things by adding macros. The way you have written it you are trying to run one DATA step inside another which is not allowed in SAS. Remove the macro call and add a simple SAS statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
i=0;
do until (i=2);
%create(i); * &amp;lt;== remove this;
put i=;  &amp;lt;== add this;
i=i+1;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 10 Aug 2019 04:44:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580328#M164841</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2019-08-10T04:44:26Z</dc:date>
    </item>
    <item>
      <title>Re: Error: There was 1 unclosed DO block.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580343#M164843</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265086"&gt;@AJ_Brien&lt;/a&gt;: Adding to all the above good comments&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macros perform text substitution — when the program runs, the macro elements of the code are replaced by the values of the macro variables or the values of the macro, and once this text substitution happens, you MUST have valid legal working SAS code, which you do not have.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When writing macros, or using macro variables, you first need to write some code that works for one or two instances WITHOUT macros and WITHOUT macro variables. If you had done that, you would know the problem isn't the macros, the problem is that your code WITHOUT macros and WITHOUT macro variables has errors. Once you have non-macro code without errors, then you can go ahead and insert macros or macro variables, and have a reasonable likelihood that the code will work with macros and with macro variables.&lt;/P&gt;</description>
      <pubDate>Sat, 10 Aug 2019 11:47:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580343#M164843</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-10T11:47:09Z</dc:date>
    </item>
    <item>
      <title>Re: Error: There was 1 unclosed DO block.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580352#M164848</link>
      <description>&lt;P&gt;It looks like the problem has been well identified, but the solutions are not really sufficient.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Change two pieces to your program.&amp;nbsp; First, get rid of the macro parameter N on %create, so it just reads:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro create;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The definition of the macro does not otherwise change, and should still reference &amp;amp;n.&amp;nbsp; However, &amp;amp;n will be set using different methods.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then address the problem DATA step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
i=0;
do until (i=2);
%create(i);
i=i+1;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Replace that with:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
i=0;
do until (i=2);
   call symputx('n', i) ;
   call execute ('%create');
   i=i+1;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There are tricky reasons why this will work, that require good understanding of CALL EXECUTE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Aug 2019 12:49:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580352#M164848</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-08-10T12:49:30Z</dc:date>
    </item>
    <item>
      <title>Re: Error: There was 1 unclosed DO block.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580355#M164849</link>
      <description>&lt;P&gt;That's great, except if usually does not make any sense to create a macro that uses "magical" macro variables that appear from no where when they are clearly parameters.&amp;nbsp; Pass the value when calling the macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;call execute (cats('%nrstr(%create)(n=',i,')'));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 10 Aug 2019 13:56:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580355#M164849</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-10T13:56:51Z</dc:date>
    </item>
    <item>
      <title>Re: Error: There was 1 unclosed DO block.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580362#M164853</link>
      <description>&lt;P&gt;All of the comment above are spot-on as to the issue - the fact that macro is a code generator.&lt;BR /&gt;&lt;BR /&gt;It looks like the intent is to use a data step to loop thru the distinct values (instead of a macro %do loop) and create a separate data set for each value.&lt;BR /&gt;&lt;BR /&gt;If so, a more direct approach would be a macro like this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro split(data=
            ,var=
            );

  proc sql noprint;
   /* create an "array" (value1, value2,...) of distinct values */
   select distinct &amp;amp;var into:value1- from &amp;amp;data;
  quit;

  %local i;
  data %do i = 1 %to &amp;amp;sqlobs; main_&amp;amp;&amp;amp;value&amp;amp;i %end;
  ;
   set &amp;amp;data;
   select (&amp;amp;var);
      %do i = 1 %to &amp;amp;sqlobs; 
          when (&amp;amp;&amp;amp;value&amp;amp;i) output main_&amp;amp;&amp;amp;value&amp;amp;i;
      %end;
   end;
  run;

%mend split;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which can be called for this case as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;
%split(data=main1,var=rank)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I included the mprint option so the generate code is shown in the log:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MPRINT(SPLIT):   proc sql noprint;
MPRINT(SPLIT):   select distinct rank into:value1- from main1;
MPRINT(SPLIT):   quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


MPRINT(SPLIT):   data main_0 main_1 ;
MPRINT(SPLIT):   set main1;
MPRINT(SPLIT):   select (rank);
MPRINT(SPLIT):   when (0) output main_0;
MPRINT(SPLIT):   when (1) output main_1;
MPRINT(SPLIT):   end;
MPRINT(SPLIT):   run;

NOTE: There were 5 observations read from the data set WORK.MAIN1.
NOTE: The data set WORK.MAIN_0 has 2 observations and 3 variables.
NOTE: The data set WORK.MAIN_1 has 3 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This code is a variable of one of the example covered in the current SAS Macro Language course.&lt;BR /&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;&lt;EM&gt;&lt;STRONG&gt;Disclaimer&lt;/STRONG&gt;&lt;/EM&gt;: this code is not intended as a bulletproof macro - just a POC.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Aug 2019 14:47:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580362#M164853</guid>
      <dc:creator>DonH</dc:creator>
      <dc:date>2019-08-10T14:47:53Z</dc:date>
    </item>
    <item>
      <title>Re: Error: There was 1 unclosed DO block.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580363#M164854</link>
      <description>&lt;P&gt;For those of you familiar with CALL EXECUTE, I would suggest that you look into the &lt;A href="https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p09dcftd1xxg1kn1brnjyc0q93yk.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank" rel="noopener"&gt;DOSUBL&lt;/A&gt; routine as an alternative to EXECUTE. EXECUTE generates code which runs after the DATA step finishes. DOSUBL runs the code immediately.&lt;/P&gt;</description>
      <pubDate>Sat, 10 Aug 2019 14:51:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580363#M164854</guid>
      <dc:creator>DonH</dc:creator>
      <dc:date>2019-08-10T14:51:51Z</dc:date>
    </item>
    <item>
      <title>Re: Error: There was 1 unclosed DO block.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580421#M164879</link>
      <description>this works perfectly, thank you!</description>
      <pubDate>Sun, 11 Aug 2019 18:33:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580421#M164879</guid>
      <dc:creator>AJ_Brien</dc:creator>
      <dc:date>2019-08-11T18:33:01Z</dc:date>
    </item>
    <item>
      <title>Re: Error: There was 1 unclosed DO block.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580422#M164880</link>
      <description>I ran this and this works too, sadly I haven't reached the stage where I could use arrays. But I've made a note of it for future learning, this is great. Thank you!</description>
      <pubDate>Sun, 11 Aug 2019 18:34:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-There-was-1-unclosed-DO-block/m-p/580422#M164880</guid>
      <dc:creator>AJ_Brien</dc:creator>
      <dc:date>2019-08-11T18:34:18Z</dc:date>
    </item>
  </channel>
</rss>

