<?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: proc sql: summary functions using group by without changing the original order of the observatio in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-summary-functions-using-group-by-without-changing-the/m-p/758825#M80858</link>
    <description>&lt;P&gt;Tell PROC SQL to order by that sequence, in other words by MAKE and MODEL.&lt;/P&gt;</description>
    <pubDate>Mon, 02 Aug 2021 16:21:21 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-08-02T16:21:21Z</dc:date>
    <item>
      <title>proc sql: summary functions using group by without changing the original order of the observations</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-summary-functions-using-group-by-without-changing-the/m-p/758821#M80857</link>
      <description>&lt;P&gt;Wondered if there is a way to keep the same order of the observations when using summary functions in proc sql. For example, from sashelp.cars, I create a new table that has a new variable called "min_length', and the min is within "type". I believe the sashelp.cars is ordered by make first, then by other variables. If used the attached code to produce the new table, then compare two tables (exclude the new variable). I expect to see they should be the same, but I got the result that there are unequal obs. I think this is caused by the change in the order of obs.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table new_cars as
select *, min(length) as min_length
from sashelp.cars
group by type
;
quit;

/* I think they should be the same if in the same oder */
proc compare base=sashelp.cars compare=new_cars (drop=min_length);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 02 Aug 2021 15:52:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-summary-functions-using-group-by-without-changing-the/m-p/758821#M80857</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2021-08-02T15:52:27Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql: summary functions using group by without changing the original order of the observatio</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-summary-functions-using-group-by-without-changing-the/m-p/758825#M80858</link>
      <description>&lt;P&gt;Tell PROC SQL to order by that sequence, in other words by MAKE and MODEL.&lt;/P&gt;</description>
      <pubDate>Mon, 02 Aug 2021 16:21:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-summary-functions-using-group-by-without-changing-the/m-p/758825#M80858</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-08-02T16:21:21Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql: summary functions using group by without changing the original order of the observatio</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-summary-functions-using-group-by-without-changing-the/m-p/758836#M80859</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84484"&gt;@sasecn&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892" target="_blank" rel="noopener"&gt;PaigeMiller&lt;/A&gt; suggested, you need to add an ORDER BY clause to your PROC SQL step. Otherwise, PROC SQL doesn't really care about sort order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now the problem is to find a suitable sort key. PROC CONTENTS assures us that SASHELP.CARS is sorted by &lt;FONT face="courier new,courier"&gt;Make Type&lt;/FONT&gt;, but PROC FREQ tells us that this is not a unique key, hence insufficient for our intended ORDER BY clause. Since there is no obvious unique sort key, we must probably create one (temporarily), e.g., in a view:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;STRONG&gt;data _tmp / view=_tmp;
set sashelp.cars;
_seqno=_n_;
run;&lt;/STRONG&gt;

proc sql;
create table new_cars&lt;STRONG&gt;(drop=_seqno)&lt;/STRONG&gt; as
select *, min(length) as min_length
from _tmp
group by type
&lt;STRONG&gt;order by _seqno;
drop view _tmp;&lt;/STRONG&gt;
quit;&lt;/PRE&gt;</description>
      <pubDate>Mon, 02 Aug 2021 16:46:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-summary-functions-using-group-by-without-changing-the/m-p/758836#M80859</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-08-02T16:46:12Z</dc:date>
    </item>
  </channel>
</rss>

