<?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 do I calculate a duration variable using multiple dates? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-duration-variable-using-multiple-dates/m-p/731208#M227777</link>
    <description>&lt;P&gt;So you just want to use the minimum and maximum date for each ID?&lt;/P&gt;</description>
    <pubDate>Sun, 04 Apr 2021 08:16:21 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2021-04-04T08:16:21Z</dc:date>
    <item>
      <title>How do I calculate a duration variable using multiple dates?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-duration-variable-using-multiple-dates/m-p/731193#M227771</link>
      <description>&lt;P&gt;&lt;FONT size="2"&gt;Hi SAS Communities,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;I would like to create a new variable called duration that will calculate the total number of months an individual (ID) was in a health program using a variable called visit_date (already converted to numeric and is in SAS date format). My data is currently in long format, so each ID has a value for the visit_date variable.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;My dataset is quite large and each ID has a varying # of visits, but the variables mentioned above are structured as follows:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;ID1&amp;nbsp; &amp;nbsp; 3/15/2020&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;ID1&amp;nbsp; &amp;nbsp; 6/27/2020&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;ID1&amp;nbsp; &amp;nbsp; 01/25/2021&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;ID2&amp;nbsp; &amp;nbsp; 3/02/2020&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;ID2&amp;nbsp; &amp;nbsp;09/15/2020&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;I've used arrays in the past to calculate duration of time, but I've only done that with data in wide format. How would I go about accomplishing this task? Thank you in advance.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 04 Apr 2021 05:40:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-duration-variable-using-multiple-dates/m-p/731193#M227771</guid>
      <dc:creator>mvhoya</dc:creator>
      <dc:date>2021-04-04T05:40:34Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculate a duration variable using multiple dates?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-duration-variable-using-multiple-dates/m-p/731200#M227773</link>
      <description>&lt;P&gt;It might help to show what you expect for output given the input. And exactly which duration might be involved, between each record or overall.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One of the nice things about SAS dates once you understand that the units are days that the statistical functions can do some interesting things.&lt;/P&gt;
&lt;PRE&gt;Proc summary data=have;
   id variable;
   var datevariable;
  output out=work.summary Range= daterange;
run;&lt;/PRE&gt;
&lt;P&gt;The above calculates the range of values for each id. That means the value when you take the largest (latest date) minus the smalles (earliest date). The value would be in days. "Months" you would have to provide a definition for as the number of days varies.&amp;nbsp; You will get 0 for ID with only one record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;OR you could use a data step, using By group processing with the id variable, retain a date value for the first of an Id and when you get to the last of the id use the INTCK function to do the duration.&lt;/P&gt;</description>
      <pubDate>Sun, 04 Apr 2021 07:33:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-duration-variable-using-multiple-dates/m-p/731200#M227773</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-04T07:33:53Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculate a duration variable using multiple dates?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-duration-variable-using-multiple-dates/m-p/731208#M227777</link>
      <description>&lt;P&gt;So you just want to use the minimum and maximum date for each ID?&lt;/P&gt;</description>
      <pubDate>Sun, 04 Apr 2021 08:16:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-duration-variable-using-multiple-dates/m-p/731208#M227777</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-04-04T08:16:21Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculate a duration variable using multiple dates?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-duration-variable-using-multiple-dates/m-p/731250#M227789</link>
      <description>I want to calculate the duration of time in months each ID spent in the program and since the dates are in chronological order for each ID, I can use the first and last date for each ID.</description>
      <pubDate>Sun, 04 Apr 2021 15:45:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-duration-variable-using-multiple-dates/m-p/731250#M227789</guid>
      <dc:creator>mvhoya</dc:creator>
      <dc:date>2021-04-04T15:45:33Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculate a duration variable using multiple dates?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-duration-variable-using-multiple-dates/m-p/731251#M227790</link>
      <description>For the output, I expect to have the variable 'duration' show the number of months for each ID. Using a data step is more aligned with what I would like to achieve since I would like to use the duration variable later in my analysis. Would you kindly show an example of code of the data step explanation you provided at the end of your response?</description>
      <pubDate>Sun, 04 Apr 2021 15:52:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-duration-variable-using-multiple-dates/m-p/731251#M227790</guid>
      <dc:creator>mvhoya</dc:creator>
      <dc:date>2021-04-04T15:52:51Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculate a duration variable using multiple dates?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-duration-variable-using-multiple-dates/m-p/731254#M227792</link>
      <description>&lt;P&gt;Use a retained variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
retain start;
if first.id then start = date;
if last.id;
duration = intck('month,start,date);
keep id duration;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 04 Apr 2021 16:05:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-duration-variable-using-multiple-dates/m-p/731254#M227792</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-04-04T16:05:33Z</dc:date>
    </item>
  </channel>
</rss>

