<?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: For the same id, how to calculate number of months between two dates given a condition in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460689#M29698</link>
    <description>&lt;P&gt;Begin with a question ... are these true SAS dates, or are they character strings?&amp;nbsp; If they are character strings, they can be converted easily enough:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;sasdate1 = input(date1, date9.);&lt;/P&gt;
&lt;P&gt;format sasdate1 yymmdd10.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second, the rules about counting months need a little more detail.&amp;nbsp; For example, does this constitute a month?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;January 30 to February 28&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Counting days instead of months would be easy, but I'm not sure if that would meet your needs.&lt;/P&gt;</description>
    <pubDate>Tue, 08 May 2018 13:29:53 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2018-05-08T13:29:53Z</dc:date>
    <item>
      <title>For the same id, how to calculate number of months between two dates given a condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460681#M29696</link>
      <description>&lt;P&gt;Hi. Can anyone please help me to figure out how to calculate the number of months between 2 dates for the same id, and given a condition?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For each id I need to find the number of months between date2&amp;nbsp;where column replay=y and the previous date2 where the column replay=y&amp;nbsp;. I need help to find a way to calculate the column called months, i.e. like this:&lt;/P&gt;
&lt;TABLE width="364"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="82"&gt;date1&lt;/TD&gt;
&lt;TD width="48"&gt;curr&lt;/TD&gt;
&lt;TD width="46"&gt;id&lt;/TD&gt;
&lt;TD width="82"&gt;date2&lt;/TD&gt;
&lt;TD width="48"&gt;replay&lt;/TD&gt;
&lt;TD width="58"&gt;months&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;01.Jun.17&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;23.Nov.16&lt;/TD&gt;
&lt;TD&gt;n&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;01.Jun.17&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;29.Nov.16&lt;/TD&gt;
&lt;TD&gt;y&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;01.Jun.17&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;01.Jun.17&lt;/TD&gt;
&lt;TD&gt;y&lt;/TD&gt;
&lt;TD&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;19.Jul.17&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;06.Sep.16&lt;/TD&gt;
&lt;TD&gt;y&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;19.Jul.17&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD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TD&gt;3&lt;/TD&gt;
&lt;TD&gt;02.Mar.17&lt;/TD&gt;
&lt;TD&gt;n&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;02.Mar.17&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;02.Mar.17&lt;/TD&gt;
&lt;TD&gt;y&lt;/TD&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Help is highly appreciated.&lt;/P&gt;</description>
      <pubDate>Tue, 08 May 2018 13:12:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460681#M29696</guid>
      <dc:creator>ak2</dc:creator>
      <dc:date>2018-05-08T13:12:29Z</dc:date>
    </item>
    <item>
      <title>Re: For the same id, how to calculate number of months between two dates given a condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460688#M29697</link>
      <description>&lt;P&gt;First you need to have date1 and date2 as actual SAS dates, and not character strings such as '23.NOV.16'. I believe that the informat ANYDTDTE will convert these to actual SAS dates, but I haven't tried. So I leave that as a homework assignment for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming you now have actual SAS dates&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    prevdate2=lag(date2);
    previd=lag(id);
    if replay='y' and id=previd then months=intck('month',prevdate2,date2);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 May 2018 13:29:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460688#M29697</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-05-08T13:29:39Z</dc:date>
    </item>
    <item>
      <title>Re: For the same id, how to calculate number of months between two dates given a condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460689#M29698</link>
      <description>&lt;P&gt;Begin with a question ... are these true SAS dates, or are they character strings?&amp;nbsp; If they are character strings, they can be converted easily enough:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;sasdate1 = input(date1, date9.);&lt;/P&gt;
&lt;P&gt;format sasdate1 yymmdd10.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second, the rules about counting months need a little more detail.&amp;nbsp; For example, does this constitute a month?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;January 30 to February 28&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Counting days instead of months would be easy, but I'm not sure if that would meet your needs.&lt;/P&gt;</description>
      <pubDate>Tue, 08 May 2018 13:29:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460689#M29698</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-05-08T13:29:53Z</dc:date>
    </item>
    <item>
      <title>Re: For the same id, how to calculate number of months between two dates given a condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460701#M29700</link>
      <description>&lt;P&gt;Properly sorting the data and then using FIRST. and LAG() and INTCK() function can solve your problem.&lt;/P&gt;
&lt;P&gt;You might get negative values if same month, adjust accordingly to ignore negative values&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm='09'x dsd truncover;
format date1 date2 date7.;
input date1 :date7. curr  id date2 :date7. replay	:$;
datalines;
01Jun17	0	1	23Nov16	n
01Jun17	0	1	29Nov16	y
01Jun17	1	1	01Jun17	y
19Jul17	0	2	06Sep16	y
19Jul17	0	2	23Sep16	y
19Jul17	0	2	19Jan17	n
19Jul17	1	2	19Jul17	y
02Mar17	0	3	07Sep16	y
02Mar17	0	3	14Feb17	n
02Mar17	0	3	02Mar17	n
02Mar17	0	3	02Mar17	n
02Mar17	1	3	02Mar17	y
;
run;

proc sort data=have;
by id replay date2;
run;
data want;
format lag_date2 date7.;
set have;
by id replay date2;
lag_date2=lag(date2);
if replay='y' and not first.replay then month=intck('month',lag_date2,date2)-1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 May 2018 13:59:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460701#M29700</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-05-08T13:59:43Z</dc:date>
    </item>
    <item>
      <title>Re: For the same id, how to calculate number of months between two dates given a condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460712#M29703</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm='09'x dsd truncover;
format date1 date2 date7.;
input date1 :date7. curr  id date2 :date7. replay	:$;
datalines;
01Jun17	0	1	23Nov16	n
01Jun17	0	1	29Nov16	y
01Jun17	1	1	01Jun17	y
19Jul17	0	2	06Sep16	y
19Jul17	0	2	23Sep16	y
19Jul17	0	2	19Jan17	n
19Jul17	1	2	19Jul17	y
02Mar17	0	3	07Sep16	y
02Mar17	0	3	14Feb17	n
02Mar17	0	3	02Mar17	n
02Mar17	0	3	02Mar17	n
02Mar17	1	3	02Mar17	y
;
run;
data want;
if _n_=1 then do;
  if 0 then set have;
  declare hash h(dataset:'have(where=(replay="y"))');
  h.definekey('id','replay','date2');
  h.definedone();
end;
set have;
by id;
retain temp;
if first.id then temp=date2;
if replay='y' then do;
  do i=date2-1 to temp by -1;
     if h.check(key:id,key:replay,key:i)=0 then do;month=intck('month',i,date2,'c');leave; end;
  end;
end;
drop i temp;
format i temp date9.;
run;


proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 May 2018 14:33:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460712#M29703</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-05-08T14:33:40Z</dc:date>
    </item>
    <item>
      <title>Re: For the same id, how to calculate number of months between two dates given a condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460722#M29705</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
format date1 date2 date7.;
input date1 :date7. curr  id date2 :date7. replay	$;
datalines;
01Jun17	0	1	23Nov16	n
01Jun17	0	1	29Nov16	y
01Jun17	1	1	01Jun17	y
19Jul17	0	2	06Sep16	y
19Jul17	0	2	23Sep16	y
19Jul17	0	2	19Jan17	n
19Jul17	1	2	19Jul17	y
02Mar17	0	3	07Sep16	y
02Mar17	0	3	14Feb17	n
02Mar17	0	3	02Mar17	n
02Mar17	0	3	02Mar17	n
02Mar17	1	3	02Mar17	y
;
run;

data want;
call missing(_prev);
do until(last.id);
set have;
by id;
if replay='y' and missing(_prev) then _prev=date2;
else if replay='y' and _prev then do;
months=intck('month',_prev, date2,'c');
_prev=date2;
end;
output;
end;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 May 2018 15:07:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460722#M29705</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-05-08T15:07:31Z</dc:date>
    </item>
    <item>
      <title>Re: For the same id, how to calculate number of months between two dates given a condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460923#M29715</link>
      <description>Thank you all for your quick answeres! Both Ksharp and novinosrin gave the same answers, but unfortunately I don't know hashing, so I could not understand the solution too well. &lt;BR /&gt;I'm sorry I forgot to say that the dates in my data set were sas dates, they just turned out like that when I pasted them into my question.</description>
      <pubDate>Wed, 09 May 2018 06:28:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460923#M29715</guid>
      <dc:creator>ak2</dc:creator>
      <dc:date>2018-05-09T06:28:18Z</dc:date>
    </item>
    <item>
      <title>Re: For the same id, how to calculate number of months between two dates given a condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460989#M29721</link>
      <description>&lt;P&gt;Here is a simple code.I wonder why I am unable to reach it yesterday.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
format date1 date2 date7.;
input date1 :date7. curr  id date2 :date7. replay	$;
datalines;
01Jun17	0	1	23Nov16	n
01Jun17	0	1	29Nov16	y
01Jun17	1	1	01Jun17	y
19Jul17	0	2	06Sep16	y
19Jul17	0	2	23Sep16	y
19Jul17	0	2	19Jan17	n
19Jul17	1	2	19Jul17	y
02Mar17	0	3	07Sep16	y
02Mar17	0	3	14Feb17	n
02Mar17	0	3	02Mar17	n
02Mar17	0	3	02Mar17	n
02Mar17	1	3	02Mar17	y
;
run;
data temp;
 set have(where=(replay='y'));
 prev=lag(date2);
 if id=lag(id) then months=intck('month',prev, date2,'c');
 drop prev;
run;
data want;
 merge have temp;
 by id date2 replay;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 May 2018 13:03:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460989#M29721</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-05-09T13:03:33Z</dc:date>
    </item>
    <item>
      <title>Re: For the same id, how to calculate number of months between two dates given a condition</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460991#M29722</link>
      <description>It was good that you posted the hash Object sloution as well. It'll be a teaser for Learning. Thanks.</description>
      <pubDate>Wed, 09 May 2018 13:07:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/For-the-same-id-how-to-calculate-number-of-months-between-two/m-p/460991#M29722</guid>
      <dc:creator>ak2</dc:creator>
      <dc:date>2018-05-09T13:07:30Z</dc:date>
    </item>
  </channel>
</rss>

