<?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: Substract rows from same column by same id and group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Substract-rows-from-same-column-by-same-id-and-group/m-p/605093#M175522</link>
    <description>&lt;P&gt;This works well!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
    <pubDate>Mon, 18 Nov 2019 16:35:23 GMT</pubDate>
    <dc:creator>dmanri</dc:creator>
    <dc:date>2019-11-18T16:35:23Z</dc:date>
    <item>
      <title>Substract rows from same column by same id and group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Substract-rows-from-same-column-by-same-id-and-group/m-p/604658#M175317</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am working with data from one dairy farm. I want to subtract the value of the column "calving_date" only if the column abort_dim = 0 to the calving_date of the previous lactation and within the same ID.&lt;/P&gt;&lt;P&gt;For example from ID 1, I want to subtract 2/25/2018 - 1/30/2018 because it has a 0 on the abort_dim column on lactation 3 and the previous lactation was 2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been trying to use SQL, but since I have three grouping conditions I am struggling to get want I want.&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is sample data, my original data has thousands of rows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help will be greatly appreciated.&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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 16 Nov 2019 00:30:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Substract-rows-from-same-column-by-same-id-and-group/m-p/604658#M175317</guid>
      <dc:creator>dmanri</dc:creator>
      <dc:date>2019-11-16T00:30:13Z</dc:date>
    </item>
    <item>
      <title>Re: Substract rows from same column by same id and group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Substract-rows-from-same-column-by-same-id-and-group/m-p/604662#M175318</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/260123"&gt;@dmanri&lt;/a&gt;&amp;nbsp;Can you please post the expected output for the sample. Also, can you please paste here as plain text rather than as attachement?&lt;/P&gt;</description>
      <pubDate>Sat, 16 Nov 2019 00:53:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Substract-rows-from-same-column-by-same-id-and-group/m-p/604662#M175318</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-11-16T00:53:31Z</dc:date>
    </item>
    <item>
      <title>Re: Substract rows from same column by same id and group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Substract-rows-from-same-column-by-same-id-and-group/m-p/604676#M175324</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/260123"&gt;@dmanri&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is something you can try.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input ID calving_date lactation ABORT_DIM;
	informat calving_date MMDDYY10.;
	format calving_date MMDDYY10.;
	cards;
1 1/15/2018  1 .
1 1/30/2018  2 .
1 2/25/2018  3 0
1 4/4/2018   4 .
2 4/8/2018   3 .
2 4/23/2018  4 .
2 6/10/2018  5 0
3 8/14/2018  2 .
3 10/19/2018 3 0
3 11/5/2018  4 .
3 12/26/2018 5 .
3 7/15/2019  6 .
4 5/26/2035  2 0
5 12/27/2035 1 .
5 1/18/2036  2 .
5 1/19/2036  3 .
6 1/24/2036  5 .
7 3/22/2036  4 .
7 6/18/2036  5 0
7 6/27/2036  6 .
;
run;

proc sort data=have;
	by ID lactation;
run;

data want;
	set have;
	day_int = intck('day',lag(calving_date),calving_date);
	if ID ne lag(ID) or lactation ne lag(lactation)+1 or abort_dim ne 0 then call missing (day_int);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Sat, 16 Nov 2019 09:34:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Substract-rows-from-same-column-by-same-id-and-group/m-p/604676#M175324</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-11-16T09:34:47Z</dc:date>
    </item>
    <item>
      <title>Re: Substract rows from same column by same id and group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Substract-rows-from-same-column-by-same-id-and-group/m-p/604686#M175327</link>
      <description>&lt;P&gt;Do you mean:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* assuming data have is sorted by ID and - Location or calving_date as need */
data want;
 set have;
       by ID;
       if not first.id then do;
          if abort_dim = 0 then 
             dif_date = calving_date - lag(calving_date);
      end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 16 Nov 2019 11:50:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Substract-rows-from-same-column-by-same-id-and-group/m-p/604686#M175327</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-11-16T11:50:29Z</dc:date>
    </item>
    <item>
      <title>Re: Substract rows from same column by same id and group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Substract-rows-from-same-column-by-same-id-and-group/m-p/605093#M175522</link>
      <description>&lt;P&gt;This works well!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Mon, 18 Nov 2019 16:35:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Substract-rows-from-same-column-by-same-id-and-group/m-p/605093#M175522</guid>
      <dc:creator>dmanri</dc:creator>
      <dc:date>2019-11-18T16:35:23Z</dc:date>
    </item>
  </channel>
</rss>

