<?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 Transpose consecutive pairs of rows by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Transpose-consecutive-pairs-of-rows-by-group/m-p/797299#M255939</link>
    <description>&lt;P&gt;After years of reading the forum, I'm finally making my first post for something that is stumping me and there must be a solution.&amp;nbsp; I have the dataset below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
  input id:32. event:$10. date:DATE9. reason:$8.;
  format date DATE9.;
  label id="id" event="event" date="date" reason="reason";
datalines;
1 entered 01JAN2020 .
1 discharged 01FEB2020 finished
2 entered 05MAR2021 .
2 discharged 15JUL2021 left
2 entered 20SEP2021 .
3 entered 05FEB2020 .
3 discharged 05MAR2020 finished
3 entered 11APR2021 .
3 discharged 01SEP2021 finished
4 entered 01DEC2018 .
4 discharged 01FEB2019 finished
4 entered 05MAR2019 .
4 discharged 06DEC2020 left
4 entered 03APR2021 .
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I would like to transpose this dataset to create the following:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2022-02-18 131647.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68667i622FB76F613108D3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Screenshot 2022-02-18 131647.png" alt="Screenshot 2022-02-18 131647.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Essentially the solution will need to look within each ID, find each pair of consecutive dates, and then transpose the data.&amp;nbsp; If an id has an odd number of records, then there would be a "last" row for that id in the new dataset that is missing for non-present pair in the original data (see ids 2 and 4 in the image above).&amp;nbsp; Using SAS 9.4 here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is greatly appreciated.&lt;/P&gt;</description>
    <pubDate>Fri, 18 Feb 2022 18:21:51 GMT</pubDate>
    <dc:creator>medsocgrad</dc:creator>
    <dc:date>2022-02-18T18:21:51Z</dc:date>
    <item>
      <title>Transpose consecutive pairs of rows by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-consecutive-pairs-of-rows-by-group/m-p/797299#M255939</link>
      <description>&lt;P&gt;After years of reading the forum, I'm finally making my first post for something that is stumping me and there must be a solution.&amp;nbsp; I have the dataset below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
  input id:32. event:$10. date:DATE9. reason:$8.;
  format date DATE9.;
  label id="id" event="event" date="date" reason="reason";
datalines;
1 entered 01JAN2020 .
1 discharged 01FEB2020 finished
2 entered 05MAR2021 .
2 discharged 15JUL2021 left
2 entered 20SEP2021 .
3 entered 05FEB2020 .
3 discharged 05MAR2020 finished
3 entered 11APR2021 .
3 discharged 01SEP2021 finished
4 entered 01DEC2018 .
4 discharged 01FEB2019 finished
4 entered 05MAR2019 .
4 discharged 06DEC2020 left
4 entered 03APR2021 .
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I would like to transpose this dataset to create the following:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2022-02-18 131647.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68667i622FB76F613108D3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Screenshot 2022-02-18 131647.png" alt="Screenshot 2022-02-18 131647.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Essentially the solution will need to look within each ID, find each pair of consecutive dates, and then transpose the data.&amp;nbsp; If an id has an odd number of records, then there would be a "last" row for that id in the new dataset that is missing for non-present pair in the original data (see ids 2 and 4 in the image above).&amp;nbsp; Using SAS 9.4 here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is greatly appreciated.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Feb 2022 18:21:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-consecutive-pairs-of-rows-by-group/m-p/797299#M255939</guid>
      <dc:creator>medsocgrad</dc:creator>
      <dc:date>2022-02-18T18:21:51Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose consecutive pairs of rows by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-consecutive-pairs-of-rows-by-group/m-p/797301#M258637</link>
      <description>&lt;P&gt;Have you checked to see if you have any "discharged" that do not have a matching "entered"? Or a discharge prior to entered?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For your example data:&lt;/P&gt;
&lt;PRE&gt;
data want;
   set have;
   by id date;
   retain date1 date2 event_date1 event_date2 ;
   length event_date1 event_date2 $ 10 ;
   if event='entered' then do;
      /* clear out retained*/
      call missing(date1, date2, event_date1, event_date2);
      date1=date;
      event_date1=event;
      if last.id then output;
   end;
   if event='discharged' then do;
      date2=date;
      event_date2=event;
      output;
   end;
   keep id date1 date2 event_date1 event_date2 reason;
   format date1 date2 date9.;
run; &lt;/PRE&gt;
&lt;P&gt;If you have a serious requirement for the order of the reason variable to be at the right look up any of the questions on this forum about such.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Feb 2022 18:53:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-consecutive-pairs-of-rows-by-group/m-p/797301#M258637</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-02-18T18:53:11Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose consecutive pairs of rows by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-consecutive-pairs-of-rows-by-group/m-p/797302#M260016</link>
      <description>&lt;P&gt;Here's an approach:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Just to be sure of order... */
proc sort data=have;
   by ID Date;
run;

data want;
   length id 8 Date1 Date2 8 Event1 Event2 $10 reason:$8.;
   set have;
   array d[2] Date1 - Date2;
   array e[2] Event1 - Event2;
   retain Date1 - Date2 Event1 - Event2;
   format Date1 - Date2 mmddyy10.;
   drop event date _:;
   by id;
   if first.id then do;
     call missing (of d[*], of e[*], _count);
   end;
   _count+1 ;
   d[_count]=date;
   e[_count]=event;
   if not mod(_count,2) then do;
      output;
      _count=0;
   end;
   else if last.id then do;
      call missing(date2, event2, reason);
      output;
   end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which produced this result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r b header" scope="col"&gt;id&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;Date1&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;Date2&lt;/TH&gt;
&lt;TH class="l b header" scope="col"&gt;Event1&lt;/TH&gt;
&lt;TH class="l b header" scope="col"&gt;Event2&lt;/TH&gt;
&lt;TH class="l b header" scope="col"&gt;reason&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;01/01/2020&lt;/TD&gt;
&lt;TD class="r data"&gt;02/01/2020&lt;/TD&gt;
&lt;TD class="l data"&gt;entered&lt;/TD&gt;
&lt;TD class="l data"&gt;discharged&lt;/TD&gt;
&lt;TD class="l data"&gt;finished&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;03/05/2021&lt;/TD&gt;
&lt;TD class="r data"&gt;07/15/2021&lt;/TD&gt;
&lt;TD class="l data"&gt;entered&lt;/TD&gt;
&lt;TD class="l data"&gt;discharged&lt;/TD&gt;
&lt;TD class="l data"&gt;left&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;09/20/2021&lt;/TD&gt;
&lt;TD class="r data"&gt;.&lt;/TD&gt;
&lt;TD class="l data"&gt;entered&lt;/TD&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;02/05/2020&lt;/TD&gt;
&lt;TD class="r data"&gt;03/05/2020&lt;/TD&gt;
&lt;TD class="l data"&gt;entered&lt;/TD&gt;
&lt;TD class="l data"&gt;discharged&lt;/TD&gt;
&lt;TD class="l data"&gt;finished&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;04/11/2021&lt;/TD&gt;
&lt;TD class="r data"&gt;09/01/2021&lt;/TD&gt;
&lt;TD class="l data"&gt;entered&lt;/TD&gt;
&lt;TD class="l data"&gt;discharged&lt;/TD&gt;
&lt;TD class="l data"&gt;finished&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="r data"&gt;12/01/2018&lt;/TD&gt;
&lt;TD class="r data"&gt;02/01/2019&lt;/TD&gt;
&lt;TD class="l data"&gt;entered&lt;/TD&gt;
&lt;TD class="l data"&gt;discharged&lt;/TD&gt;
&lt;TD class="l data"&gt;finished&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="r data"&gt;03/05/2019&lt;/TD&gt;
&lt;TD class="r data"&gt;12/06/2020&lt;/TD&gt;
&lt;TD class="l data"&gt;entered&lt;/TD&gt;
&lt;TD class="l data"&gt;discharged&lt;/TD&gt;
&lt;TD class="l data"&gt;left&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="r data"&gt;04/03/2021&lt;/TD&gt;
&lt;TD class="r data"&gt;.&lt;/TD&gt;
&lt;TD class="l data"&gt;entered&lt;/TD&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Fri, 18 Feb 2022 19:03:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-consecutive-pairs-of-rows-by-group/m-p/797302#M260016</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2022-02-18T19:03:57Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose consecutive pairs of rows by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-consecutive-pairs-of-rows-by-group/m-p/797342#M287459</link>
      <description>&lt;P&gt;Thank you for the quick response and elegant solution. This worked as hoped for!&lt;/P&gt;</description>
      <pubDate>Fri, 18 Feb 2022 22:34:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-consecutive-pairs-of-rows-by-group/m-p/797342#M287459</guid>
      <dc:creator>medsocgrad</dc:creator>
      <dc:date>2022-02-18T22:34:32Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose consecutive pairs of rows by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-consecutive-pairs-of-rows-by-group/m-p/797370#M287474</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id:32. event:$10. date:DATE9. reason:$8.;
  format date DATE9.;
  label id="id" event="event" date="date" reason="reason";
datalines;
1 entered 01JAN2020 .
1 discharged 01FEB2020 finished
2 entered 05MAR2021 .
2 discharged 15JUL2021 left
2 entered 20SEP2021 .
3 entered 05FEB2020 .
3 discharged 05MAR2020 finished
3 entered 11APR2021 .
3 discharged 01SEP2021 finished
4 entered 01DEC2018 .
4 discharged 01FEB2019 finished
4 entered 05MAR2019 .
4 discharged 06DEC2020 left
4 entered 03APR2021 .
;
run;
data want;
 merge have(where=(date1_event='entered') rename=(event=date1_event date=date_1 reason=reason_1))
       have(where=(date2_event='discharged') rename=(event=date2_event date=date_2 reason=reason_2));
 by id;
 output;
 call missing(of _all_);
 format date_1 date_2 mmddyy10.;
 drop reason_1;
 label date1_event=' ' date2_event=' ' date1_event=' ' date2_event=' ' date_1=' ' date_2=' ';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 19 Feb 2022 10:39:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-consecutive-pairs-of-rows-by-group/m-p/797370#M287474</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-02-19T10:39:47Z</dc:date>
    </item>
  </channel>
</rss>

