<?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: Difference between Two dates for Single Variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/310772#M67053</link>
    <description>&lt;P&gt;I tried your code but it only produced 1 for the first observations of each combination. I would like 2 for the second combination. Any ideas?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 10 Nov 2016 21:00:51 GMT</pubDate>
    <dc:creator>tbanh</dc:creator>
    <dc:date>2016-11-10T21:00:51Z</dc:date>
    <item>
      <title>Difference between Two dates for Single Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/309885#M66778</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/5685i04F4EFB11531A526/image-size/original?v=v2&amp;amp;px=-1" alt="Screen Shot 2016-11-07 at 11.20.12 AM.png" title="Screen Shot 2016-11-07 at 11.20.12 AM.png" border="0" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi all, I have a longitudinal data set where the above dates are nested by household and person. The above are dates for one person and one household. I would like to compute the difference between two subsequent dates without creating a lagged variable. Is this possible? Thanks and all help is appreciated.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Nov 2016 21:53:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/309885#M66778</guid>
      <dc:creator>tbanh</dc:creator>
      <dc:date>2016-11-07T21:53:44Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dates for Single Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/309891#M66781</link>
      <description>&lt;P&gt;It's easy, but you are missing some of the description.&amp;nbsp; When should the variable not be calculated?&amp;nbsp; When beginning a new household?&amp;nbsp; When beginning a new member of the household?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an approach that assumes you should get no differences whenever the member of the household changes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have;&lt;/P&gt;
&lt;P&gt;by household member date;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by household member;&lt;/P&gt;
&lt;P&gt;days_difference = dif(date);&lt;/P&gt;
&lt;P&gt;if first.member then days_difference=.;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Similar to the LAG function, the DIF function must execute on every observation in order to get the right answer.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Nov 2016 22:08:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/309891#M66781</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-11-07T22:08:00Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dates for Single Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/309893#M66782</link>
      <description>&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/5688iC838D36A6D735C26/image-size/original?v=v2&amp;amp;px=-1" alt="Screen Shot 2016-11-07 at 2.10.26 PM.png" title="Screen Shot 2016-11-07 at 2.10.26 PM.png" border="0" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's more of the data set. I would like to compute the difference for each unique basebrgy (household) and basewman (person) combination. And if there is no date, I would like it to not compute anything.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Nov 2016 22:13:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/309893#M66782</guid>
      <dc:creator>tbanh</dc:creator>
      <dc:date>2016-11-07T22:13:22Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dates for Single Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/309894#M66783</link>
      <description>&lt;P&gt;Then the code I gave you is the right approach.&amp;nbsp; You just need to change the variable names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, DATE cannot be a character string.&amp;nbsp; DATE has to be a numeric variable stored on SAS's usual scale for storing dates.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Nov 2016 22:18:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/309894#M66783</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-11-07T22:18:38Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dates for Single Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/310587#M67009</link>
      <description>&lt;P&gt;I have a different question but related to the same dataset. I am trying to create a singular ID variable for each household and person combination. This is the code I have currently that sort of gets at what I am trying to do:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data subset2;
	set subset2;
	by basebrgy basewman;
	ID + 1;

	if first.basebrgy OR first.basewman then ID = 1;


run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is what the output looks like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/5745iDDF16ECFFCA2FD10/image-size/medium?v=v2&amp;amp;px=-1" alt="Screen Shot 2016-11-09 at 11.39.42 PM.png" title="Screen Shot 2016-11-09 at 11.39.42 PM.png" border="0" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For the ID variable, I'd like it to be a column of 1's for basebrgy = 1/basewman = 12 and a column of 2's for basebrgy = 1/basewman = 14. Hope I was clear enough. Thanks for any help.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Nov 2016 07:42:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/310587#M67009</guid>
      <dc:creator>tbanh</dc:creator>
      <dc:date>2016-11-10T07:42:27Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dates for Single Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/310588#M67010</link>
      <description>&lt;P&gt;Not sure I understant but wouldn't this simply be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data subset2;
	set subset2;

   if basebrgy = 1 and basewman = 12 then ID=1;
   else if basebrgy = 1 and basewman = 14 then ID=2;
   else;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Nov 2016 07:49:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/310588#M67010</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2016-11-10T07:49:44Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dates for Single Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/310630#M67017</link>
      <description>&lt;P&gt;You're playing with the right tools, but you need a slightly different combination:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by basebrgy basewman;&lt;/P&gt;
&lt;P&gt;if first.basewman then ID + 1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is definitely a topic worth spending time on to understand. &amp;nbsp;Many DATA steps create BY variables.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Nov 2016 11:55:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/310630#M67017</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-11-10T11:55:08Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dates for Single Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/310772#M67053</link>
      <description>&lt;P&gt;I tried your code but it only produced 1 for the first observations of each combination. I would like 2 for the second combination. Any ideas?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Nov 2016 21:00:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/310772#M67053</guid>
      <dc:creator>tbanh</dc:creator>
      <dc:date>2016-11-10T21:00:51Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dates for Single Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/310773#M67054</link>
      <description>&lt;P&gt;This works but subset2 is a subset of the much larger dataset. I would like to automate the process instead of doing it manually.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Nov 2016 21:01:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/310773#M67054</guid>
      <dc:creator>tbanh</dc:creator>
      <dc:date>2016-11-10T21:01:57Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dates for Single Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/310775#M67055</link>
      <description>&lt;P&gt;The code worked just fine for me.&amp;nbsp; Can you post the log from your attempt?&lt;/P&gt;</description>
      <pubDate>Thu, 10 Nov 2016 21:07:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/310775#M67055</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-11-10T21:07:45Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dates for Single Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/310782#M67057</link>
      <description>&lt;P&gt;1867&amp;nbsp; data subset2;&lt;BR /&gt;1868&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set subset2;&lt;BR /&gt;1869&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by basebrgy basewman;&lt;BR /&gt;1870&lt;BR /&gt;1871&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if first.basewman then ID = 1;&lt;BR /&gt;1872&amp;nbsp; run;&lt;BR /&gt;&lt;BR /&gt;NOTE: There were 18 observations read from the data set WORK.SUBSET2.&lt;BR /&gt;NOTE: The data set WORK.SUBSET2 has 18 observations and 7 variables.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; real time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.01 seconds&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cpu time&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.01 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's the output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/5766iD541F3FAB530F857/image-size/medium?v=v2&amp;amp;px=-1" alt="Screen Shot 2016-11-10 at 1.11.49 PM.png" title="Screen Shot 2016-11-10 at 1.11.49 PM.png" border="0" /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Nov 2016 21:15:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/310782#M67057</guid>
      <dc:creator>tbanh</dc:creator>
      <dc:date>2016-11-10T21:15:53Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Two dates for Single Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/310786#M67059</link>
      <description>&lt;P&gt;Never mind, I figured it out. I set ID = 1 instead of ID + 1&lt;/P&gt;</description>
      <pubDate>Thu, 10 Nov 2016 21:19:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Two-dates-for-Single-Variable/m-p/310786#M67059</guid>
      <dc:creator>tbanh</dc:creator>
      <dc:date>2016-11-10T21:19:23Z</dc:date>
    </item>
  </channel>
</rss>

