<?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 to extract records expect first and last records in each group by using sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-records-expect-first-and-last-records-in-each/m-p/485034#M125974</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/209685"&gt;@thanikondharish&lt;/a&gt;:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. You didn't state what "group" means. Is it SUBID?&lt;/P&gt;&lt;P&gt;2. If it is, you didn't present your desired output. Do you want to (a) keep the groups with fewer than 3 rows or (b) drop them? Because if a group contains 1 or 2 rows, your criterion of keeping only the rows in&amp;nbsp;&lt;EM&gt;except&lt;/EM&gt; the first and the last would mean that you'd keep no rows from that group at all.&lt;/P&gt;&lt;P&gt;3. Are your dates within the group (c) unique as in your data sample or (d) they can be duplicate?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If the answers are (a) and (c), then you can code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data treatment ;                   
  input subid $ date:date. dose $ ;
  format date date9. ;             
  cards ;                          
P101    13Jan2005     Asp-05mg     
P102    14Jan2005     Asp-10mg     
P101    19Jan2005     Asp-10mg     
P103    19Jan2005     Asp-15mg     
P102    20Jan2005     Asp-05mg     
P101    25Jan2005     Asp-15mg     
P103    25Jan2005     Asp-10mg     
P102    26Jan2005     Asp-15mg     
P104    26Jan2005     Asp-10mg     
P101    31Jan2005     Asp-05mg     
P103    30Jan2005     Asp-10mg     
P102    01Feb2005     Asp-15mg     
P104    02Feb2005     Asp-10mg     
P105    02Feb2005     Asp-20mg     
run ;        
                      
proc sql ;                                                       
   create table want as                                          
   select *&lt;BR /&gt;   from   treatment                                       
   group  subid                                                  
   having (date &amp;lt; max(date) and date &amp;gt; min(date)) or count(*) &amp;lt; 3
   ;                                                             
quit ;                                                            &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;SPAN&gt;If the answers are (a) and (d), you can't rely on the dates and have to rely on a surrogate unique row number (RID below) instead:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;                                                     
   create table want as                                        
   select *                                                    
   from (select *, monotonic() as rid from treatment)          
   group  subid                                                
   having (rid &amp;lt; max (rid) and rid &amp;gt; min (rid)) or count(*) &amp;lt; 3
   ;                                                           
quit ;                                                         &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;This approach is generally safer since you while may &lt;EM&gt;think&lt;/EM&gt; that your DATE values per SUBID are unique, the actual data may know otherwise. Finally, if your answer is (b), i.e. you don't want to keep the groups with fewer than 3 rows, then eliminate the text:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;or count(*) &amp;lt; 3&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;from both queries shown above.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Paul D.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 08 Aug 2018 07:23:28 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2018-08-08T07:23:28Z</dc:date>
    <item>
      <title>how to extract records expect first and last records in each group by using sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-records-expect-first-and-last-records-in-each/m-p/484985#M125940</link>
      <description>&lt;P&gt;Data Treatment ;&lt;/P&gt;&lt;P&gt;input subid $ Date : date9. Dose $;&lt;/P&gt;&lt;P&gt;Format Date : date9. ;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;P101&amp;nbsp;&amp;nbsp;&amp;nbsp; 13Jan2005&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Asp-05mg&lt;/P&gt;&lt;P&gt;P102&amp;nbsp;&amp;nbsp;&amp;nbsp; 14Jan2005&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Asp-10mg&lt;/P&gt;&lt;P&gt;P101&amp;nbsp;&amp;nbsp;&amp;nbsp; 19Jan2005&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Asp-10mg&lt;/P&gt;&lt;P&gt;P103&amp;nbsp;&amp;nbsp;&amp;nbsp; 19Jan2005&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Asp-15mg&lt;/P&gt;&lt;P&gt;P102&amp;nbsp;&amp;nbsp;&amp;nbsp; 20Jan2005&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Asp-05mg&lt;/P&gt;&lt;P&gt;P101&amp;nbsp;&amp;nbsp;&amp;nbsp; 25Jan2005&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Asp-15mg&lt;/P&gt;&lt;P&gt;P103&amp;nbsp;&amp;nbsp;&amp;nbsp; 25Jan2005&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Asp-10mg&lt;/P&gt;&lt;P&gt;P102&amp;nbsp;&amp;nbsp;&amp;nbsp; 26Jan2005&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Asp-15mg&lt;/P&gt;&lt;P&gt;P104&amp;nbsp;&amp;nbsp;&amp;nbsp; 26Jan2005&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Asp-10mg&lt;/P&gt;&lt;P&gt;P101&amp;nbsp;&amp;nbsp;&amp;nbsp; 31Jan2005&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Asp-05mg&lt;/P&gt;&lt;P&gt;P103&amp;nbsp;&amp;nbsp;&amp;nbsp; 30Jan2005&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Asp-10mg&lt;/P&gt;&lt;P&gt;P102&amp;nbsp;&amp;nbsp;&amp;nbsp; 01Feb2005&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Asp-15mg&lt;/P&gt;&lt;P&gt;P104&amp;nbsp;&amp;nbsp;&amp;nbsp; 02Feb2005&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Asp-10mg&lt;/P&gt;&lt;P&gt;P105&amp;nbsp;&amp;nbsp;&amp;nbsp; 02Feb2005&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Asp-20mg&lt;/P&gt;&lt;P&gt;Run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;how to extract records except first and last records in each group by using sql?&lt;/P&gt;</description>
      <pubDate>Wed, 08 Aug 2018 05:14:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-records-expect-first-and-last-records-in-each/m-p/484985#M125940</guid>
      <dc:creator>thanikondharish</dc:creator>
      <dc:date>2018-08-08T05:14:36Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract records expect first and last records in each group by using sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-records-expect-first-and-last-records-in-each/m-p/484996#M125949</link>
      <description>&lt;P&gt;Why do you want to limit yourself to PROC SQL when doing this in a data step is much simpler. SQL deals with set operations. The data step deals with individual rows. If you absolutely want to do this in SQL, create a variable &lt;STRONG&gt;n&amp;nbsp;&lt;/STRONG&gt;to keep track of first/last occurrences within by groups and use SQL like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Treatment ;
input subid $ Date : date9. Dose $;
Format Date : date9. ;
cards;
P101 13Jan2005 Asp-05mg
P102 14Jan2005 Asp-10mg
P101 19Jan2005 Asp-10mg
P103 19Jan2005 Asp-15mg
P102 20Jan2005 Asp-05mg
P101 25Jan2005 Asp-15mg
P103 25Jan2005 Asp-10mg
P102 26Jan2005 Asp-15mg
P104 26Jan2005 Asp-10mg
P101 31Jan2005 Asp-05mg
P103 30Jan2005 Asp-10mg
P102 01Feb2005 Asp-15mg
P104 02Feb2005 Asp-10mg
P105 02Feb2005 Asp-20mg
Run;

data temp;
   set Treatment;
   n=_N_;
run;

proc sql;
   create table want as
   select * from temp
   group by subid
   having n ne max(n) &amp;amp; n ne min(n);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Aug 2018 05:57:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-records-expect-first-and-last-records-in-each/m-p/484996#M125949</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-08-08T05:57:23Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract records expect first and last records in each group by using sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-records-expect-first-and-last-records-in-each/m-p/485018#M125966</link>
      <description>I know how to extract in dataset block but I want to know in process SQL.</description>
      <pubDate>Wed, 08 Aug 2018 06:54:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-records-expect-first-and-last-records-in-each/m-p/485018#M125966</guid>
      <dc:creator>thanikondharish</dc:creator>
      <dc:date>2018-08-08T06:54:22Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract records expect first and last records in each group by using sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-records-expect-first-and-last-records-in-each/m-p/485020#M125968</link>
      <description>&lt;P&gt;Is your data sorted by Date? Ie does the earliest/latest date imply the first/last record within each By Group? In that case, do&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 * from Treatment
   group by subid
   having Date ne max(Date) and Date ne min(Date);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Aug 2018 06:58:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-records-expect-first-and-last-records-in-each/m-p/485020#M125968</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-08-08T06:58:33Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract records expect first and last records in each group by using sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-records-expect-first-and-last-records-in-each/m-p/485034#M125974</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/209685"&gt;@thanikondharish&lt;/a&gt;:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. You didn't state what "group" means. Is it SUBID?&lt;/P&gt;&lt;P&gt;2. If it is, you didn't present your desired output. Do you want to (a) keep the groups with fewer than 3 rows or (b) drop them? Because if a group contains 1 or 2 rows, your criterion of keeping only the rows in&amp;nbsp;&lt;EM&gt;except&lt;/EM&gt; the first and the last would mean that you'd keep no rows from that group at all.&lt;/P&gt;&lt;P&gt;3. Are your dates within the group (c) unique as in your data sample or (d) they can be duplicate?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If the answers are (a) and (c), then you can code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data treatment ;                   
  input subid $ date:date. dose $ ;
  format date date9. ;             
  cards ;                          
P101    13Jan2005     Asp-05mg     
P102    14Jan2005     Asp-10mg     
P101    19Jan2005     Asp-10mg     
P103    19Jan2005     Asp-15mg     
P102    20Jan2005     Asp-05mg     
P101    25Jan2005     Asp-15mg     
P103    25Jan2005     Asp-10mg     
P102    26Jan2005     Asp-15mg     
P104    26Jan2005     Asp-10mg     
P101    31Jan2005     Asp-05mg     
P103    30Jan2005     Asp-10mg     
P102    01Feb2005     Asp-15mg     
P104    02Feb2005     Asp-10mg     
P105    02Feb2005     Asp-20mg     
run ;        
                      
proc sql ;                                                       
   create table want as                                          
   select *&lt;BR /&gt;   from   treatment                                       
   group  subid                                                  
   having (date &amp;lt; max(date) and date &amp;gt; min(date)) or count(*) &amp;lt; 3
   ;                                                             
quit ;                                                            &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;SPAN&gt;If the answers are (a) and (d), you can't rely on the dates and have to rely on a surrogate unique row number (RID below) instead:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;                                                     
   create table want as                                        
   select *                                                    
   from (select *, monotonic() as rid from treatment)          
   group  subid                                                
   having (rid &amp;lt; max (rid) and rid &amp;gt; min (rid)) or count(*) &amp;lt; 3
   ;                                                           
quit ;                                                         &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;This approach is generally safer since you while may &lt;EM&gt;think&lt;/EM&gt; that your DATE values per SUBID are unique, the actual data may know otherwise. Finally, if your answer is (b), i.e. you don't want to keep the groups with fewer than 3 rows, then eliminate the text:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;or count(*) &amp;lt; 3&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;from both queries shown above.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Paul D.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Aug 2018 07:23:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-records-expect-first-and-last-records-in-each/m-p/485034#M125974</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-08-08T07:23:28Z</dc:date>
    </item>
  </channel>
</rss>

