<?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 to aggregate date data in different line in the same line? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-date-data-in-different-line-in-the-same-line/m-p/521411#M141475</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/251238"&gt;@MJLEE&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi everybody!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to aggregate a variable, between two specific dates for each&amp;nbsp;patient Id. So Imagine I have a database&amp;nbsp;with&amp;nbsp;two columns: Patient ID, Date. I want to calculate the&amp;nbsp;difference between the dates that patient visit&amp;nbsp;a clinic. It's a panel data. I have multiple observations for each patient ID&amp;nbsp; from 2007 to 2017. I want to aggregate each variable's value between specific dates for each&amp;nbsp;patient ID.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that helps,&amp;nbsp;I can you an example;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;patientID / date&lt;/P&gt;
&lt;P&gt;111/20071207&lt;/P&gt;
&lt;P&gt;111/20080102&lt;/P&gt;
&lt;P&gt;111/20080103&lt;/P&gt;
&lt;P&gt;111/20080201&lt;/P&gt;
&lt;P&gt;22/20160303&lt;/P&gt;
&lt;P&gt;22/20160315&lt;/P&gt;
&lt;P&gt;22/20160316&lt;/P&gt;
&lt;P&gt;22/20160507&lt;/P&gt;
&lt;P&gt;22/20160601&lt;/P&gt;
&lt;P&gt;22/20160616&lt;/P&gt;
&lt;P&gt;22/20160620&lt;/P&gt;
&lt;P&gt;3/20171010&lt;/P&gt;
&lt;P&gt;3/20271018&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;into&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;patientID / date1 / date2/ date3/ date4/ date5/ date6/ date7/ ...&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;111/20071207/20080102/20080103/20080201/20160507/./...&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;22/20160303/20160315/20160316/20160620/././././..&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;3/20171010/20271018 ./././././ ...&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Is it impossible to make data like this type?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;please help me to solve the problem.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thank you!&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Stay with the long format, as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt; suggested, and use lag() to calculate date differences:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dlm='/';
input patient_id $ visit_date :yymmdd8.;
format visit_date yymmddd10.;
cards;
111/20071207
111/20080102
111/20080103
111/20080201
22/20160303
22/20160315
22/20160316
22/20160507
22/20160601
22/20160616
22/20160620
3/20171010
3/20271018
;
run;

data want;
set have;
by patient_id;
date_diff = visit_date - lag(visit_date);
if first.patient_id then date_diff = .;
run;

proc print data=want noobs;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;patient_                  date_
   id       visit_date     diff

  111       2007-12-07        .
  111       2008-01-02       26
  111       2008-01-03        1
  111       2008-02-01       29
  22        2016-03-03        .
  22        2016-03-15       12
  22        2016-03-16        1
  22        2016-05-07       52
  22        2016-06-01       25
  22        2016-06-16       15
  22        2016-06-20        4
  3         2017-10-10        .
  3         2027-10-18     3660
&lt;/PRE&gt;</description>
    <pubDate>Fri, 14 Dec 2018 09:36:13 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-12-14T09:36:13Z</dc:date>
    <item>
      <title>How to aggregate date data in different line in the same line?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-date-data-in-different-line-in-the-same-line/m-p/521403#M141472</link>
      <description>&lt;P&gt;Hi everybody!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to aggregate a variable, between two specific dates for each&amp;nbsp;patient Id. So Imagine I have a database&amp;nbsp;with&amp;nbsp;two columns: Patient ID, Date. I want to calculate the&amp;nbsp;difference between the dates that patient visit&amp;nbsp;a clinic. It's a panel data. I have multiple observations for each patient ID&amp;nbsp; from 2007 to 2017. I want to aggregate each variable's value between specific dates for each&amp;nbsp;patient ID.&amp;nbsp;&lt;/P&gt;&lt;P&gt;If that helps,&amp;nbsp;I can you an example;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;patientID / date&lt;/P&gt;&lt;P&gt;111/20071207&lt;/P&gt;&lt;P&gt;111/20080102&lt;/P&gt;&lt;P&gt;111/20080103&lt;/P&gt;&lt;P&gt;111/20080201&lt;/P&gt;&lt;P&gt;22/20160303&lt;/P&gt;&lt;P&gt;22/20160315&lt;/P&gt;&lt;P&gt;22/20160316&lt;/P&gt;&lt;P&gt;22/20160507&lt;/P&gt;&lt;P&gt;22/20160601&lt;/P&gt;&lt;P&gt;22/20160616&lt;/P&gt;&lt;P&gt;22/20160620&lt;/P&gt;&lt;P&gt;3/20171010&lt;/P&gt;&lt;P&gt;3/20271018&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;into&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;patientID / date1 / date2/ date3/ date4/ date5/ date6/ date7/ ...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;111/20071207/20080102/20080103/20080201/20160507/./...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;22/20160303/20160315/20160316/20160620/././././..&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3/20171010/20271018 ./././././ ...&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Is it impossible to make data like this type?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;please help me to solve the problem.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thank you!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Dec 2018 09:15:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-date-data-in-different-line-in-the-same-line/m-p/521403#M141472</guid>
      <dc:creator>MJLEE</dc:creator>
      <dc:date>2018-12-14T09:15:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to aggregate date data in different line in the same line?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-date-data-in-different-line-in-the-same-line/m-p/521406#M141474</link>
      <description>&lt;P&gt;Whilst it is not impossible to create your want dataset - you can use a simple proc tranpose to get it - I would recommend that it is not the best method.&amp;nbsp; Transposed data is only useful for output reports so humans can read it.&amp;nbsp; Much simpler to code with long data.&amp;nbsp; You have not however provided any information about your problem.&amp;nbsp; Follow the posting a question guidance.&amp;nbsp; Post test data in the form of a datastep using a code window (its the {i} above post area):&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is so we have something to work with.&amp;nbsp; Then show what you want out at the end.&amp;nbsp; Explain your problem showing any errors/warnings log parts, or code you might have already done, and explain what does not work.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Dec 2018 09:25:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-date-data-in-different-line-in-the-same-line/m-p/521406#M141474</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-12-14T09:25:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to aggregate date data in different line in the same line?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-date-data-in-different-line-in-the-same-line/m-p/521411#M141475</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/251238"&gt;@MJLEE&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi everybody!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to aggregate a variable, between two specific dates for each&amp;nbsp;patient Id. So Imagine I have a database&amp;nbsp;with&amp;nbsp;two columns: Patient ID, Date. I want to calculate the&amp;nbsp;difference between the dates that patient visit&amp;nbsp;a clinic. It's a panel data. I have multiple observations for each patient ID&amp;nbsp; from 2007 to 2017. I want to aggregate each variable's value between specific dates for each&amp;nbsp;patient ID.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that helps,&amp;nbsp;I can you an example;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;patientID / date&lt;/P&gt;
&lt;P&gt;111/20071207&lt;/P&gt;
&lt;P&gt;111/20080102&lt;/P&gt;
&lt;P&gt;111/20080103&lt;/P&gt;
&lt;P&gt;111/20080201&lt;/P&gt;
&lt;P&gt;22/20160303&lt;/P&gt;
&lt;P&gt;22/20160315&lt;/P&gt;
&lt;P&gt;22/20160316&lt;/P&gt;
&lt;P&gt;22/20160507&lt;/P&gt;
&lt;P&gt;22/20160601&lt;/P&gt;
&lt;P&gt;22/20160616&lt;/P&gt;
&lt;P&gt;22/20160620&lt;/P&gt;
&lt;P&gt;3/20171010&lt;/P&gt;
&lt;P&gt;3/20271018&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;into&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;patientID / date1 / date2/ date3/ date4/ date5/ date6/ date7/ ...&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;111/20071207/20080102/20080103/20080201/20160507/./...&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;22/20160303/20160315/20160316/20160620/././././..&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;3/20171010/20271018 ./././././ ...&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Is it impossible to make data like this type?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;please help me to solve the problem.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thank you!&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Stay with the long format, as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt; suggested, and use lag() to calculate date differences:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dlm='/';
input patient_id $ visit_date :yymmdd8.;
format visit_date yymmddd10.;
cards;
111/20071207
111/20080102
111/20080103
111/20080201
22/20160303
22/20160315
22/20160316
22/20160507
22/20160601
22/20160616
22/20160620
3/20171010
3/20271018
;
run;

data want;
set have;
by patient_id;
date_diff = visit_date - lag(visit_date);
if first.patient_id then date_diff = .;
run;

proc print data=want noobs;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;patient_                  date_
   id       visit_date     diff

  111       2007-12-07        .
  111       2008-01-02       26
  111       2008-01-03        1
  111       2008-02-01       29
  22        2016-03-03        .
  22        2016-03-15       12
  22        2016-03-16        1
  22        2016-05-07       52
  22        2016-06-01       25
  22        2016-06-16       15
  22        2016-06-20        4
  3         2017-10-10        .
  3         2027-10-18     3660
&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 Dec 2018 09:36:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-date-data-in-different-line-in-the-same-line/m-p/521411#M141475</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-12-14T09:36:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to aggregate date data in different line in the same line?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-date-data-in-different-line-in-the-same-line/m-p/521469#M141493</link>
      <description>Thank you for your help! This is the solution that I'm finding!</description>
      <pubDate>Fri, 14 Dec 2018 14:26:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-aggregate-date-data-in-different-line-in-the-same-line/m-p/521469#M141493</guid>
      <dc:creator>MJLEE</dc:creator>
      <dc:date>2018-12-14T14:26:00Z</dc:date>
    </item>
  </channel>
</rss>

