<?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: Loop through duplicate Id's and subtract row2-row1 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-duplicate-Id-s-and-subtract-row2-row1/m-p/551905#M153399</link>
    <description>I got it. I modified the code to just the difference between k and date, hence i get the numbers i was hoping for, thank you, this was a simple solution rather than doing Do loop; appreciate timely help.</description>
    <pubDate>Wed, 17 Apr 2019 21:22:10 GMT</pubDate>
    <dc:creator>sghatak</dc:creator>
    <dc:date>2019-04-17T21:22:10Z</dc:date>
    <item>
      <title>Loop through duplicate Id's and subtract row2-row1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-duplicate-Id-s-and-subtract-row2-row1/m-p/551893#M153394</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;I have data is following format&lt;BR /&gt;Id Date&lt;BR /&gt;1 201701&lt;BR /&gt;1 201711&lt;BR /&gt;2 201809&lt;BR /&gt;2 201812&lt;BR /&gt;3 201702&lt;BR /&gt;3 201708&lt;BR /&gt;3 201812&lt;/P&gt;&lt;P&gt;I need to group by ID's and find the difference between the dates, so my output looks like -&lt;BR /&gt;Id Date Difference&lt;BR /&gt;1 201701 0&lt;BR /&gt;1 201711 10&lt;BR /&gt;2 201809 0&lt;BR /&gt;2 201812 3&lt;BR /&gt;3 201702 0&lt;BR /&gt;3 201708 6&lt;BR /&gt;3 201812 4&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;_n_ =0;&lt;BR /&gt;do until(last.ID);&lt;BR /&gt;set have; by Id;&lt;BR /&gt;_n_+1;&lt;BR /&gt;if _n_ = _n_ - 1 then dateflag = date;&lt;BR /&gt;if _n_ = _n_ +1 then difference = dateflag - date;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2019 20:52:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-duplicate-Id-s-and-subtract-row2-row1/m-p/551893#M153394</guid>
      <dc:creator>sghatak</dc:creator>
      <dc:date>2019-04-17T20:52:34Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through duplicate Id's and subtract row2-row1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-duplicate-Id-s-and-subtract-row2-row1/m-p/551895#M153396</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Id Date yymmn6.;
format date date9.;
cards;
1 201701
1 201711
2 201809
2 201812
3 201702
3 201708
3 201812
;

data want;
set have;
by id;
k=lag(date);
if first.id then want=0;
else want=intck('month',k,date);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/207723"&gt;@sghatak&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;3 201708 6&lt;/SPAN&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;&lt;SPAN&gt;3 201812 4&lt;/SPAN&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;In your sample is incorrect . The year should be either 2017 or Difference should be 16. Please review and&amp;nbsp; verify&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2019 21:01:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-duplicate-Id-s-and-subtract-row2-row1/m-p/551895#M153396</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-04-17T21:01:21Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through duplicate Id's and subtract row2-row1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-duplicate-Id-s-and-subtract-row2-row1/m-p/551896#M153397</link>
      <description>My Id is char, date is num, hence i am trying to subtract rows, thank you for quick response, appreciate it.</description>
      <pubDate>Wed, 17 Apr 2019 21:11:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-duplicate-Id-s-and-subtract-row2-row1/m-p/551896#M153397</guid>
      <dc:creator>sghatak</dc:creator>
      <dc:date>2019-04-17T21:11:36Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through duplicate Id's and subtract row2-row1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-duplicate-Id-s-and-subtract-row2-row1/m-p/551898#M153398</link>
      <description>&lt;P&gt;No problem with your ID being char or num. We aren't using that at all. Basically your date is a numeric date and what i understand is you are trying to compute the month intervals between two dates, hence I used the intck function. Please test.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I really hope you are understanding the code and mostly importantly your requirement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/207723"&gt;@sghatak&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;My Id is char, date is num, hence i am trying to subtract rows, thank you for quick response, appreciate it.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2019 21:14:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-duplicate-Id-s-and-subtract-row2-row1/m-p/551898#M153398</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-04-17T21:14:28Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through duplicate Id's and subtract row2-row1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-duplicate-Id-s-and-subtract-row2-row1/m-p/551905#M153399</link>
      <description>I got it. I modified the code to just the difference between k and date, hence i get the numbers i was hoping for, thank you, this was a simple solution rather than doing Do loop; appreciate timely help.</description>
      <pubDate>Wed, 17 Apr 2019 21:22:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-duplicate-Id-s-and-subtract-row2-row1/m-p/551905#M153399</guid>
      <dc:creator>sghatak</dc:creator>
      <dc:date>2019-04-17T21:22:10Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through duplicate Id's and subtract row2-row1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-duplicate-Id-s-and-subtract-row2-row1/m-p/551907#M153400</link>
      <description>&lt;P&gt;one more thing forgot to add, i needed to group by Id's , so for iD = 1 subtract, ID=2, subtract until last.ID. Anyway , have to loop it for this. Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2019 21:25:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-duplicate-Id-s-and-subtract-row2-row1/m-p/551907#M153400</guid>
      <dc:creator>sghatak</dc:creator>
      <dc:date>2019-04-17T21:25:07Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through duplicate Id's and subtract row2-row1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-duplicate-Id-s-and-subtract-row2-row1/m-p/551908#M153401</link>
      <description>&lt;P&gt;what do you mean?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's a straight forward current-lag(current) within an id(by group)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Relax. Do not confuse &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2019 21:26:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-duplicate-Id-s-and-subtract-row2-row1/m-p/551908#M153401</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-04-17T21:26:31Z</dc:date>
    </item>
  </channel>
</rss>

