<?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: Position of %END; in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Position-of-END/m-p/328279#M62484</link>
    <description>&lt;P&gt;If you look at the log youu will see that RUN is after all sorts.&lt;/P&gt;
&lt;P&gt;In this specific case, as 2nd sort is independent of first that realy soesn't matter,&lt;/P&gt;
&lt;P&gt;but as a rule better put the %END after RUN, so that each PROC or datastep has its own RUN.&lt;/P&gt;</description>
    <pubDate>Sun, 29 Jan 2017 12:05:46 GMT</pubDate>
    <dc:creator>Shmuel</dc:creator>
    <dc:date>2017-01-29T12:05:46Z</dc:date>
    <item>
      <title>Position of %END;</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Position-of-END/m-p/328278#M62483</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO MERGE;
%Do RO=1 %to 5;
PROC SORT DATA = MERGED_&amp;amp;RO; BY KEY;%END; RUN;

/*what if %END; is placed after RUN*/


%MEND;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hi&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to know how SAS process a Macro DO-loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Should&amp;nbsp;I place the %END after RUN or before?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it is placed before RUN, does it mean that the DO-Loop substitute from MERGED_1 to MERGED_5 but it only executes the Proc Sort for MERGED_5?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Sun, 29 Jan 2017 11:38:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Position-of-END/m-p/328278#M62483</guid>
      <dc:creator>apple</dc:creator>
      <dc:date>2017-01-29T11:38:53Z</dc:date>
    </item>
    <item>
      <title>Re: Position of %END;</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Position-of-END/m-p/328279#M62484</link>
      <description>&lt;P&gt;If you look at the log youu will see that RUN is after all sorts.&lt;/P&gt;
&lt;P&gt;In this specific case, as 2nd sort is independent of first that realy soesn't matter,&lt;/P&gt;
&lt;P&gt;but as a rule better put the %END after RUN, so that each PROC or datastep has its own RUN.&lt;/P&gt;</description>
      <pubDate>Sun, 29 Jan 2017 12:05:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Position-of-END/m-p/328279#M62484</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-01-29T12:05:46Z</dc:date>
    </item>
    <item>
      <title>Re: Position of %END;</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Position-of-END/m-p/328280#M62485</link>
      <description>&lt;P&gt;A PROC statement always constitutes a step boundary (as does a RUN statement), so all PROC SORT's will be executed.&lt;/P&gt;</description>
      <pubDate>Sun, 29 Jan 2017 12:07:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Position-of-END/m-p/328280#M62485</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-01-29T12:07:03Z</dc:date>
    </item>
    <item>
      <title>Re: Position of %END;</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Position-of-END/m-p/328283#M62486</link>
      <description>&lt;P&gt;Macro language is not processing your data. &amp;nbsp;It is generating a program. &amp;nbsp;With %END in its current location, the generated program looks like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC SORT DATA=MERGED_1; BY KEY;&lt;/P&gt;
&lt;P&gt;PROC SORT DATA=MERGED_2; BY KEY;&lt;/P&gt;
&lt;P&gt;PROC SORT DATA=MERGED_3; BY KEY;&lt;/P&gt;
&lt;P&gt;PROC SORT DATA=MERGED_4; BY KEY;&lt;/P&gt;
&lt;P&gt;PROC SORT DATA=MERGED_5; BY KEY;&lt;/P&gt;
&lt;P&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now it becomes a question of how SAS language would handle this program. &amp;nbsp;The same principles apply with %END moved to after the RUN statement. &amp;nbsp;Then the generated SAS code would look like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC SORT DATA=MERGED_1; BY KEY; RUN;&lt;/P&gt;
&lt;P&gt;PROC SORT DATA=MERGED_2; BY KEY; RUN;&lt;/P&gt;
&lt;P&gt;PROC SORT DATA=MERGED_3; BY KEY; RUN;&lt;/P&gt;
&lt;P&gt;PROC SORT DATA=MERGED_4; BY KEY; RUN;&lt;/P&gt;
&lt;P&gt;PROC SORT DATA=MERGED_5; BY KEY; RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Again, SAS language&amp;nbsp;receives this code, interprets it, and executes it. &amp;nbsp;But macro language is never processing the data, only generating the program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I agree with everything that Kurt wrote, including the suggestion to move the %END statement so that every PROC SORT will have its own RUN statement.&lt;/P&gt;</description>
      <pubDate>Sun, 29 Jan 2017 14:01:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Position-of-END/m-p/328283#M62486</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-29T14:01:31Z</dc:date>
    </item>
    <item>
      <title>Re: Position of %END;</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Position-of-END/m-p/328344#M62488</link>
      <description>&lt;P&gt;Whilst the guys have answered the techinical part above I would also like to add that making your program readable should be your number 1 priority (unless your dealing with big data). &amp;nbsp;To this end use lower and consistent case, indent blocks etc.:&lt;/P&gt;
&lt;PRE&gt;%macro merge ();
  %do ro=1 %to 5;
    proc sort data=merged_&amp;amp;ro.;
      by key;
    run;
  %end;
%mend merge;&lt;/PRE&gt;
&lt;P&gt;You will note the dot after the macro variable - not vital, but highlights nicely in the editor, other things like %mend &amp;lt;macro name&amp;gt; rather than just %mend. &amp;nbsp;At the end of the day other people have to look at your code.&lt;/P&gt;
&lt;P&gt;Also, a further note, the macro above is pretty much useless. &amp;nbsp;You have create a problem for yourself in code before this by creating separate datasets for the same data as indicated by the merged_1-5. &amp;nbsp;It is rarely a good idea to split up same data, as then each time you code you need to loop over it. &amp;nbsp;A simpler method - if you just had the 5 datasets:&lt;/P&gt;
&lt;PRE&gt;data want;
  set merged_:;
run;
proc sort data=want;
  by key;
run;&lt;/PRE&gt;
&lt;P&gt;You could use the indsname option to get filename if necessary.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jan 2017 09:30:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Position-of-END/m-p/328344#M62488</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-01-30T09:30:08Z</dc:date>
    </item>
  </channel>
</rss>

