<?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: How do I collapse rows/observations and incorporate a minimum and maximum from different rows? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/499334#M132853</link>
    <description>Hi RW9, thank you for this. I'm not sure, why but when I applied this to my real data (sensitive so couldn't post real), it did not work. I will explore further and advise if I figure it out! It may be that there is some characteristic of the data that I did not capture appropriately with my small example.</description>
    <pubDate>Thu, 27 Sep 2018 01:01:15 GMT</pubDate>
    <dc:creator>Count</dc:creator>
    <dc:date>2018-09-27T01:01:15Z</dc:date>
    <item>
      <title>How do I collapse rows/observations and incorporate a minimum and maximum from different rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/498963#M132703</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using Base SAS v9.3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have data set&amp;nbsp;HAVE as follows:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
input Dept $ CM_start CM_end Year Count;
length dept $ 4;
datalines;
M82T 1 3 2005 789
M82T 4 5 2005 789
M82T 7 9 2005 789
M82T 10 11 2006 985
M82T 12 14 2006 985
UTYD 5 7 2008 980
UTYD 8 9 2008 980
UTYD 10 15 2011 784
UTYD 16 17 2011 784
UTYD 18 20 2011 784
SRT6 5 7 2008 452
SRT6 8 9 2008 452
SRT6 10 15 2008 452
SRT6 16 17 2008 452
SRT6 18 20 2008 452
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want data set WANT that collapses the rows into one record for all rows that have the same DEPT, YEAR and COUNT.&lt;/P&gt;&lt;P&gt;BUT, I also want CM_START to represent the lowest CM value and CM_END to represent the highest from that DEPT/YEAR/COUNT group.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Dept&lt;/TD&gt;&lt;TD&gt;CM_start&lt;/TD&gt;&lt;TD&gt;CM_end&lt;/TD&gt;&lt;TD&gt;Year&lt;/TD&gt;&lt;TD&gt;Count&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;M82T&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;9&lt;/TD&gt;&lt;TD&gt;2005&lt;/TD&gt;&lt;TD&gt;789&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;M82T&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;14&lt;/TD&gt;&lt;TD&gt;2006&lt;/TD&gt;&lt;TD&gt;985&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;UTYD&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;9&lt;/TD&gt;&lt;TD&gt;2008&lt;/TD&gt;&lt;TD&gt;980&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;UTYD&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;2011&lt;/TD&gt;&lt;TD&gt;784&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;SRT6&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;2008&lt;/TD&gt;&lt;TD&gt;452&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I feel like this is a multi-step process, that might incorporate by-group processing, with use of&amp;nbsp;&lt;EM&gt;first&lt;/EM&gt; and &lt;EM&gt;last&lt;/EM&gt; but can't for the life of me figure it out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your time!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Sep 2018 06:23:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/498963#M132703</guid>
      <dc:creator>Count</dc:creator>
      <dc:date>2018-09-26T06:23:21Z</dc:date>
    </item>
    <item>
      <title>Re: How do I collapse rows/observations and incorporate a minimum and maximum from different rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/498977#M132712</link>
      <description>&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;data have;&lt;BR /&gt; length dept $ 4;&lt;BR /&gt; input dept $ cm_start cm_end year count;&lt;BR /&gt;datalines;&lt;BR /&gt;M82T 1 3 2005 789&lt;BR /&gt;M82T 4 5 2005 789&lt;BR /&gt;M82T 7 9 2005 789&lt;BR /&gt;M82T 10 11 2006 985&lt;BR /&gt;M82T 12 14 2006 985&lt;BR /&gt;UTYD 5 7 2008 980&lt;BR /&gt;UTYD 8 9 2008 980&lt;BR /&gt;UTYD 10 15 2011 784&lt;BR /&gt;UTYD 16 17 2011 784&lt;BR /&gt;UTYD 18 20 2011 784&lt;BR /&gt;SRT6 5 7 2008 452&lt;BR /&gt;SRT6 8 9 2008 452&lt;BR /&gt;SRT6 10 15 2008 452&lt;BR /&gt;SRT6 16 17 2008 452&lt;BR /&gt;SRT6 18 20 2008 452&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;data want;&lt;BR /&gt; set have;&lt;BR /&gt; by dept year count notsorted;&lt;BR /&gt; retain f_start f_year f_count;&lt;BR /&gt; if first.count then do;&lt;BR /&gt;   f_start=cm_start;&lt;BR /&gt;   f_year=year;&lt;BR /&gt;   f_count=count;&lt;BR /&gt; end;&lt;BR /&gt; if last.count then do;&lt;BR /&gt;   cm_start=f_start;&lt;BR /&gt;   year=f_year;&lt;BR /&gt;   count=f_count;&lt;BR /&gt;   output;&lt;BR /&gt; end;&lt;BR /&gt;run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Sep 2018 07:42:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/498977#M132712</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-09-26T07:42:40Z</dc:date>
    </item>
    <item>
      <title>Re: How do I collapse rows/observations and incorporate a minimum and maximum from different rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/498982#M132716</link>
      <description>&lt;P&gt;Does it need to be so complicated, why not just:?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
 create table want as
  select dept, min(cm_start) as cm_start, max(cm_end) as cm_end, year, count
  from have
  group by dept, year, count
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;//Fredrik&lt;/P&gt;</description>
      <pubDate>Wed, 26 Sep 2018 08:07:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/498982#M132716</guid>
      <dc:creator>FredrikE</dc:creator>
      <dc:date>2018-09-26T08:07:56Z</dc:date>
    </item>
    <item>
      <title>Re: How do I collapse rows/observations and incorporate a minimum and maximum from different rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/498988#M132721</link>
      <description>&lt;P&gt;It depends, if there is a lot of data sql is not the best procedure to process it - been shown in other posts.&amp;nbsp; SQL isn't also known by everyone.&amp;nbsp; Everyone however who uses SAS knows Base SAS datastep language, and to be perfectly honest, there is very little difference between the two codes there anyways, a by group, you use a min/max function, I use a retained variable, big whoop.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Sep 2018 08:12:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/498988#M132721</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-09-26T08:12:44Z</dc:date>
    </item>
    <item>
      <title>Re: How do I collapse rows/observations and incorporate a minimum and maximum from different rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/498989#M132722</link>
      <description>&lt;P&gt;What would be the expected result of data like this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;bob42 1 3 2009 123
bob42 5 8 2009 123
bob42 9 10 2009 123&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or is it sure that there are no breaks between cm_end and the next cm_start?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Sep 2018 08:16:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/498989#M132722</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-09-26T08:16:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do I collapse rows/observations and incorporate a minimum and maximum from different rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/499030#M132734</link>
      <description>&lt;P&gt;Here's a proc summary solution&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have nway;
   class Dept year count;
   output out=want(drop=_:) min(CM_start)=CM_start max(CM_end)=CM_end;
   run;quit;
proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Sep 2018 11:35:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/499030#M132734</guid>
      <dc:creator>himalayan</dc:creator>
      <dc:date>2018-09-26T11:35:58Z</dc:date>
    </item>
    <item>
      <title>Re: How do I collapse rows/observations and incorporate a minimum and maximum from different rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/499334#M132853</link>
      <description>Hi RW9, thank you for this. I'm not sure, why but when I applied this to my real data (sensitive so couldn't post real), it did not work. I will explore further and advise if I figure it out! It may be that there is some characteristic of the data that I did not capture appropriately with my small example.</description>
      <pubDate>Thu, 27 Sep 2018 01:01:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/499334#M132853</guid>
      <dc:creator>Count</dc:creator>
      <dc:date>2018-09-27T01:01:15Z</dc:date>
    </item>
    <item>
      <title>Re: How do I collapse rows/observations and incorporate a minimum and maximum from different rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/499335#M132854</link>
      <description>&lt;P&gt;This is a valid point&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;and one I should have included in my post (apologies). The data provider assures me that there are no breaks and review of the data indicates this is true. Of course, that doesn't mean that an error could occur in pre-processing in the future. I believe (perhaps wrongly) that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13827"&gt;@FredrikE&lt;/a&gt;'s code would account for such an instance and creating separate rows where a break occurs. This is sufficient for my purposes.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Sep 2018 01:05:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/499335#M132854</guid>
      <dc:creator>Count</dc:creator>
      <dc:date>2018-09-27T01:05:04Z</dc:date>
    </item>
    <item>
      <title>Re: How do I collapse rows/observations and incorporate a minimum and maximum from different rows?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/499336#M132855</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/229259"&gt;@himalayan&lt;/a&gt;. This worked well, and I don't regularly use proc summary so it was nice to see an alternative method! It was however a bit slower to process than the SQL method, so have accepted that solution.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Sep 2018 01:07:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-collapse-rows-observations-and-incorporate-a-minimum/m-p/499336#M132855</guid>
      <dc:creator>Count</dc:creator>
      <dc:date>2018-09-27T01:07:40Z</dc:date>
    </item>
  </channel>
</rss>

