<?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: Calculating Fractional Months between different observations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Fractional-Months-between-different-observations/m-p/894035#M353193</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input Obs	Key	Counter	Date :mmddyy10.;
format date mmddyy10.;
cards;
1	111	1	1/1/2023	
2	111	2	2/5/2023	
3	111	3	4/7/2023	
4	111	4	6/10/2023	
5	118	1	2/10/2023	
6	118	2	5/1/2023
;
data want;
 set have;
 lag_date=lag(date);
 if key=lag(key) then want=yrdif(lag_date,date,'30/360')*12;
 drop lag_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 13 Sep 2023 12:04:07 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2023-09-13T12:04:07Z</dc:date>
    <item>
      <title>Calculating Fractional Months between different observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Fractional-Months-between-different-observations/m-p/894031#M353190</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to calculate the fractional months between different observations from the prior observations with the same key. For example, for the observations with the key "111", I want to calculate the fractional months seen in Counter "2" vs Counter "1", then the fractional month seen in Counter "3" vs Counter "2", then Counter "4" vs Counter "2", then start over again for next key "118". In excel, it would be like using the formula&amp;nbsp;=DATEDIF(D7,D8,"M")+DATEDIF(D7,D8,"MD")/(365/12). Below is an example of the data set and I want to create the "Months btw Dates" column below. Thanks in advance!&lt;/P&gt;
&lt;TABLE width="255"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="31"&gt;Obs&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lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Wed, 13 Sep 2023 11:46:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Fractional-Months-between-different-observations/m-p/894031#M353190</guid>
      <dc:creator>ChickenLittle</dc:creator>
      <dc:date>2023-09-13T11:46:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Fractional Months between different observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Fractional-Months-between-different-observations/m-p/894032#M353191</link>
      <description>&lt;P&gt;I mispoke when I said Counter "4" vs Counter "2". What I meant to say was that I want Counter "4" vs Counter "3"&lt;/P&gt;</description>
      <pubDate>Wed, 13 Sep 2023 11:51:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Fractional-Months-between-different-observations/m-p/894032#M353191</guid>
      <dc:creator>ChickenLittle</dc:creator>
      <dc:date>2023-09-13T11:51:31Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Fractional Months between different observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Fractional-Months-between-different-observations/m-p/894033#M353192</link>
      <description>&lt;P&gt;I dislike the idea of "fractional months", as months are different lengths, and so I recommend you not use "fractional months". The following code will compute the number of days between the dates. You can do with that number of days anything you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input obs key counter date :mmddyy8.;
format date date9.;
cards;
1	111	1	1/1/2023	 
2	111	2	2/5/2023
3	111	3	4/7/2023
4	111	4	6/10/2023
5	118	1	2/10/2023	 
6	118	2	5/1/2023
;

data want;
    set have;
    by key;
    prev_date=lag(date);
    if first.key then days_between=.;
    else days_between=date-prev_date;
    drop prev_date;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note: we expect data to be presented as working SAS data step code, and not other formats such as the one your provided. I have given an example (and instructions &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;here&lt;/A&gt; for more complicated data sets). In your future questions, please provide data as shown.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Sep 2023 12:04:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Fractional-Months-between-different-observations/m-p/894033#M353192</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-13T12:04:21Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Fractional Months between different observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-Fractional-Months-between-different-observations/m-p/894035#M353193</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input Obs	Key	Counter	Date :mmddyy10.;
format date mmddyy10.;
cards;
1	111	1	1/1/2023	
2	111	2	2/5/2023	
3	111	3	4/7/2023	
4	111	4	6/10/2023	
5	118	1	2/10/2023	
6	118	2	5/1/2023
;
data want;
 set have;
 lag_date=lag(date);
 if key=lag(key) then want=yrdif(lag_date,date,'30/360')*12;
 drop lag_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Sep 2023 12:04:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-Fractional-Months-between-different-observations/m-p/894035#M353193</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-09-13T12:04:07Z</dc:date>
    </item>
  </channel>
</rss>

