<?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: why using 'by statement' twice ? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/why-using-by-statement-twice/m-p/741751#M231920</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;In the data step, the by-statement is simply 'telling the data step' that there IS a sort order that can be exploited by the code. It's not doing a sort operation or so.&lt;/P&gt;
&lt;P&gt;If you don't use the by-statement, the FIRST. and LAST. variables won't exist!!&lt;/P&gt;
&lt;P&gt;Kind regards,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
    <pubDate>Sun, 16 May 2021 15:59:07 GMT</pubDate>
    <dc:creator>sbxkoenk</dc:creator>
    <dc:date>2021-05-16T15:59:07Z</dc:date>
    <item>
      <title>why using 'by statement' twice ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-using-by-statement-twice/m-p/741748#M231918</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname results 'C:\practice everyday\input';
proc sort data = cert.input06 out = input06std;
by department descending income;
run;
data results.output06;
set input06std;
by department descending income;
if first.department;
run;
proc print data = results.output06;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;After 'proc sort', the 'input06std' has been sorted by variable department and ordered by variable&amp;nbsp; income,&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;proc sort data = cert.input06 out = input06std;
by department descending income;   (the first time）
run;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;why do the same thing again in the next step ----'data step' ?&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;data results.output06;
set input06std;
by department descending income;    （the second time）
if first.department;
run;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; &lt;/P&gt;</description>
      <pubDate>Sun, 16 May 2021 15:50:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-using-by-statement-twice/m-p/741748#M231918</guid>
      <dc:creator>tianerhu</dc:creator>
      <dc:date>2021-05-16T15:50:40Z</dc:date>
    </item>
    <item>
      <title>Re: why using 'by statement' twice ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-using-by-statement-twice/m-p/741751#M231920</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;In the data step, the by-statement is simply 'telling the data step' that there IS a sort order that can be exploited by the code. It's not doing a sort operation or so.&lt;/P&gt;
&lt;P&gt;If you don't use the by-statement, the FIRST. and LAST. variables won't exist!!&lt;/P&gt;
&lt;P&gt;Kind regards,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sun, 16 May 2021 15:59:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-using-by-statement-twice/m-p/741751#M231920</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-05-16T15:59:07Z</dc:date>
    </item>
    <item>
      <title>Re: why using 'by statement' twice ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-using-by-statement-twice/m-p/741754#M231921</link>
      <description>&lt;P&gt;Thank you for your help.&lt;/P&gt;</description>
      <pubDate>Sun, 16 May 2021 16:20:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-using-by-statement-twice/m-p/741754#M231921</guid>
      <dc:creator>tianerhu</dc:creator>
      <dc:date>2021-05-16T16:20:17Z</dc:date>
    </item>
    <item>
      <title>Re: why using 'by statement' twice ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-using-by-statement-twice/m-p/741755#M231922</link>
      <description>&lt;P&gt;In the second 'by statement', Can I delete 'descending income' , the resulting output is the same when I run the program.&lt;/P&gt;</description>
      <pubDate>Sun, 16 May 2021 16:37:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-using-by-statement-twice/m-p/741755#M231922</guid>
      <dc:creator>tianerhu</dc:creator>
      <dc:date>2021-05-16T16:37:10Z</dc:date>
    </item>
    <item>
      <title>Re: why using 'by statement' twice ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-using-by-statement-twice/m-p/741756#M231923</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/268447"&gt;@tianerhu&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;In the second 'by statement', Can I delete 'descending income' , the resulting output is the same when I run the program.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Just think about it.&lt;/P&gt;
&lt;LI-SPOILER&gt;Which FIRST. variable is used?&lt;/LI-SPOILER&gt;</description>
      <pubDate>Sun, 16 May 2021 16:44:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-using-by-statement-twice/m-p/741756#M231923</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-16T16:44:24Z</dc:date>
    </item>
    <item>
      <title>Re: why using 'by statement' twice ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-using-by-statement-twice/m-p/741758#M231925</link>
      <description>&lt;P&gt;The variable 'First' use on variable 'department' , so we can delete the code 'descending income' .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your enlighten .&lt;/P&gt;</description>
      <pubDate>Sun, 16 May 2021 17:02:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-using-by-statement-twice/m-p/741758#M231925</guid>
      <dc:creator>tianerhu</dc:creator>
      <dc:date>2021-05-16T17:02:55Z</dc:date>
    </item>
    <item>
      <title>Re: why using 'by statement' twice ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-using-by-statement-twice/m-p/741780#M231936</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/268447"&gt;@tianerhu&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The variable 'First' use on variable 'department' , so we can delete the code 'descending income' .&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/268447"&gt;@tianerhu&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Wait a moment. It's true that only &lt;FONT face="courier new,courier"&gt;department&lt;/FONT&gt; is &lt;EM&gt;necessary&lt;/EM&gt; in the BY statement of the DATA step in order to make &lt;FONT face="courier new,courier"&gt;first.department&lt;/FONT&gt; work. But it's safer (and therefore good practice) to repeat also&amp;nbsp;&lt;FONT face="courier new,courier"&gt;descending income&lt;/FONT&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider the situation that, for some reason (e.g., later changes to the program), the department BY-groups in dataset INPUT06STD were &lt;EM&gt;not&lt;/EM&gt; sorted by descending income. With the fully repeated BY statement from the PROC SORT step the DATA step would then produce an error message ("&lt;FONT face="courier new,courier"&gt;ERROR: BY variables are not properly sorted&lt;/FONT&gt; ...") and thus remind you that this second-level sort order is needed for the program logic (i.e., for selecting an observation with the highest income per department).&amp;nbsp;With the "parsimonious" &lt;FONT face="courier new,courier"&gt;by department;&lt;/FONT&gt;, however, the DATA step would produce a clean log, yet the result might be wrong!&amp;nbsp;You might think that you've selected the highest income per department, as intended, while in fact you've just selected the &lt;EM&gt;first observation&lt;/EM&gt; of each&amp;nbsp;department BY-group, regardless of the&amp;nbsp;&lt;FONT face="courier new,courier"&gt;income&lt;/FONT&gt; value.&lt;/P&gt;</description>
      <pubDate>Sun, 16 May 2021 21:14:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-using-by-statement-twice/m-p/741780#M231936</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-05-16T21:14:35Z</dc:date>
    </item>
  </channel>
</rss>

