<?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: Efficient sorting when some variables are already sorted in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Efficient-sorting-when-some-variables-are-already-sorted/m-p/91421#M289782</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you for your help! Your suggestions improved the code a lot!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 26 Jul 2013 15:33:29 GMT</pubDate>
    <dc:creator>Georg_UPB</dc:creator>
    <dc:date>2013-07-26T15:33:29Z</dc:date>
    <item>
      <title>Efficient sorting when some variables are already sorted</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-sorting-when-some-variables-are-already-sorted/m-p/91418#M289779</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a dataset containing for each store and date the IDs of different product types (Product_ID) and the amount of sold units (Product_sold). The data are already sorted by store and date.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My tasks are to&lt;/P&gt;&lt;P&gt;* consolidate sales for identical product types which were sold in the same store and on the same day (done in the first PROC SQL step in the example below)&lt;/P&gt;&lt;P&gt;* insert columns having the information sorted by product type in ascending order (Product_ID_asc Product_sold_asc) and columns having the information sorted in descending order (Product_ID_desc Product_sold_desc) for each store and date.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'd really like to know if there are better solutions than the (pretty slow) one I posted below. The original dataset is A and A2 represents the desired result.&lt;/P&gt;&lt;P&gt;1) Dataset A is very big. Is it really necessary to create a second table (A2) in order to consolidate sales (first PROC SQL step)?&lt;/P&gt;&lt;P&gt;2) Aren't there more efficient ways that take advantage of the fact that the data is already sorted by store and date? I still mention both variables after group by/sort by.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks. I really appreciate any suggestions.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Data A;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Format&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Store $4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Date date11.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Product_ID $6.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Product_sold best2.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Input&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Store :$4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Date :date11.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Product_ID :$6.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Product_sold best2.;&lt;/P&gt;&lt;P&gt;Datalines;&lt;/P&gt;&lt;P&gt;West 19-FEB-2013 Type_B 9&lt;/P&gt;&lt;P&gt;West 19-FEB-2013 Type_A 9&lt;/P&gt;&lt;P&gt;West 19-FEB-2013 Type_B 11&lt;/P&gt;&lt;P&gt;West 20-FEB-2013 Type_C 5&lt;/P&gt;&lt;P&gt;West 20-FEB-2013 Type_A 6&lt;/P&gt;&lt;P&gt;West 20-FEB-2013 Type_B 7&lt;/P&gt;&lt;P&gt;West 20-FEB-2013 Type_A 10&lt;/P&gt;&lt;P&gt;Run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Proc Sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Create Table A2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; As Select&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Store, Date, Product_ID, SUM(Product_sold) AS Product_sold&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; From&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Group By&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Store, Date, Product_ID;&lt;/P&gt;&lt;P&gt;Quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Proc Sort&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Data=A2 Out=A2Product_asc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; (&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Keep=Product_ID Product_sold&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Rename=(Product_ID=Product_ID_asc Product_sold=Product_sold_asc)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; );&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; By Store Date Product_ID;&lt;/P&gt;&lt;P&gt;Run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Proc Sort&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Data=A2 Out=A2Product_desc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; (&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Keep=Product_ID Product_sold&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Rename=(Product_ID=Product_ID_desc Product_sold=Product_sold_desc)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; );&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; By Store Date Descending Product_ID;&lt;/P&gt;&lt;P&gt;Run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Data A2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Set A2(Keep=Store Date);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Set A2Product_asc;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Set A2Product_desc;&lt;/P&gt;&lt;P&gt;Run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 25 Jul 2013 22:49:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-sorting-when-some-variables-are-already-sorted/m-p/91418#M289779</guid>
      <dc:creator>Georg_UPB</dc:creator>
      <dc:date>2013-07-25T22:49:31Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient sorting when some variables are already sorted</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-sorting-when-some-variables-are-already-sorted/m-p/91419#M289780</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your first sort is redundant (omit it!) because GROUP BY in SAS will generate a sorted table.&amp;nbsp; To be certain, you could insert and ORDER BY clause.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;Data A2;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp; Set A2 (Rename = (Product_Sold = Product_Sold_Asc ) );&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp; Set A2Product_desc;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;Run;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;Richard&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 26 Jul 2013 01:45:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-sorting-when-some-variables-are-already-sorted/m-p/91419#M289780</guid>
      <dc:creator>RichardinOz</dc:creator>
      <dc:date>2013-07-26T01:45:31Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient sorting when some variables are already sorted</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-sorting-when-some-variables-are-already-sorted/m-p/91420#M289781</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I would guess that PROC SUMMARY would be faster than PROC SQL:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc summary data=A nway;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by store date;&lt;/P&gt;&lt;P&gt;&amp;nbsp; class product_id;;&lt;/P&gt;&lt;P&gt;&amp;nbsp; var product_sold;&lt;/P&gt;&lt;P&gt;&amp;nbsp; output out=a2 (keep=store date product_id product_sold_asc rename=(product_id=product_id_asc)) sum=product_sold_asc;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Of course, there's just one way to be sure which would be faster.&amp;nbsp; And this does require the data to be in sorted order BY STORE DATE (which you had indicated was the case).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As RichardinOz pointed out, the result will be sorted already (regardless of whether you use SQL or SUMMARY), and you only need two data sets not three.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 26 Jul 2013 02:42:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-sorting-when-some-variables-are-already-sorted/m-p/91420#M289781</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2013-07-26T02:42:00Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient sorting when some variables are already sorted</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-sorting-when-some-variables-are-already-sorted/m-p/91421#M289782</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you for your help! Your suggestions improved the code a lot!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 26 Jul 2013 15:33:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-sorting-when-some-variables-are-already-sorted/m-p/91421#M289782</guid>
      <dc:creator>Georg_UPB</dc:creator>
      <dc:date>2013-07-26T15:33:29Z</dc:date>
    </item>
  </channel>
</rss>

