<?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 bring all data in one row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652840#M196070</link>
    <description>&lt;P&gt;Then I suggest this data step solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm='09'x;
input
  date :mmddyy10.
  Time :time11.
  AVD_ID :$12.
  device_NO
  device__No_id
  ID :$12.
  Check_In
  Check_out
  Route
  Stop_name :$30.
  Amount
;
dt = dhms(date,0,0,time);
format dt e8601dt19.;
datalines;
01/03/2019	5:06:00 PM	B1910_EVD_04	166450	23442	602803314916:	1	0	22	Canada Water bus station	7.5
01/03/2019	5:38:00 PM	B1910_EVD_05	166451	23443	602803314916:	0	1	22	Tottenham Court Road station	-4.5
;

proc sort data=have;
by id dt;
run;

data trips;
retain /* for column order also */
  id
  in_time
  out_time
  check_in
  check_out
  in_stop_name
  out_stop_name
  in_amount
  out_amount
;
format
  in_time
  out_time e8601dt19.
;
set have (rename=(
  check_in=_check_in
  amount=out_amount
  dt=out_time
  stop_name=out_stop_name
));
by id route notsorted;
if first.route
then do;
  in_time = out_time;
  check_in = _check_in;
  in_stop_name = out_stop_name;
  in_amount = out_amount;
end;
if last.route;
keep
  id
  in_time
  out_time
  check_in
  check_out
  in_stop_name
  out_stop_name
  in_amount
  out_amount
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;By using datetimes instead of separate dates and times, this code will also deal correctly with trips crossing the day boundary.&lt;/P&gt;</description>
    <pubDate>Wed, 03 Jun 2020 12:16:39 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-06-03T12:16:39Z</dc:date>
    <item>
      <title>How to bring all data in one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652134#M195756</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I have a dataset in which I am trying to bring rows into columns from multiple rows eg:-&lt;/SPAN&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;date&lt;/TD&gt;&lt;TD&gt;Time&lt;/TD&gt;&lt;TD&gt;AVD_ID&lt;/TD&gt;&lt;TD&gt;device_NO&lt;/TD&gt;&lt;TD&gt;device__No_id&lt;/TD&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Check_In&lt;/TD&gt;&lt;TD&gt;Check_out&lt;/TD&gt;&lt;TD&gt;Route&lt;/TD&gt;&lt;TD&gt;Stop_name&lt;/TD&gt;&lt;TD&gt;Amount&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01/03/2019&lt;/TD&gt;&lt;TD&gt;5:06:00 PM&lt;/TD&gt;&lt;TD&gt;B1910_EVD_04&lt;/TD&gt;&lt;TD&gt;166450&lt;/TD&gt;&lt;TD&gt;23442&lt;/TD&gt;&lt;TD&gt;602803314916 :&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;22&lt;/TD&gt;&lt;TD&gt;Canada Water bus station&lt;/TD&gt;&lt;TD&gt;7.5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01/03/2019&lt;/TD&gt;&lt;TD&gt;5:38:00 PM&lt;/TD&gt;&lt;TD&gt;B1910_EVD_05&lt;/TD&gt;&lt;TD&gt;166451&lt;/TD&gt;&lt;TD&gt;23443&lt;/TD&gt;&lt;TD&gt;602803314916 :&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;22&lt;/TD&gt;&lt;TD&gt;Tottenham Court Road station&lt;/TD&gt;&lt;TD&gt;-4.5&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;now I want to create In_time, out_time on the basis of time and bring Amount, check_in and out in one row for eg:-&lt;/SPAN&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;date&lt;/TD&gt;&lt;TD&gt;In time&lt;/TD&gt;&lt;TD&gt;Out time&lt;/TD&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Check_In&lt;/TD&gt;&lt;TD&gt;Check_out&lt;/TD&gt;&lt;TD&gt;Route&lt;/TD&gt;&lt;TD&gt;In Stop_name&lt;/TD&gt;&lt;TD&gt;Out Stop_name&lt;/TD&gt;&lt;TD&gt;In Amount&lt;/TD&gt;&lt;TD&gt;Out Amount&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01/03/2019&lt;/TD&gt;&lt;TD&gt;5:06:00 PM&lt;/TD&gt;&lt;TD&gt;5:38:00 PM&lt;/TD&gt;&lt;TD&gt;1130602803314816 :&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;22&lt;/TD&gt;&lt;TD&gt;Canada Water bus station&lt;/TD&gt;&lt;TD&gt;Tottenham Court Road station&lt;/TD&gt;&lt;TD&gt;7.5&lt;/TD&gt;&lt;TD&gt;-4.5&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried using partition by, lag, row_no function but it not giving me the result.&lt;/P&gt;&lt;P&gt;Please help me on this.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jun 2020 04:46:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652134#M195756</guid>
      <dc:creator>umeshgiri48</dc:creator>
      <dc:date>2020-06-01T04:46:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to bring all data in one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652144#M195761</link>
      <description>&lt;P&gt;What is identified by ID? A single trip, or something like a person or travel card?&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jun 2020 06:22:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652144#M195761</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-01T06:22:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to bring all data in one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652145#M195762</link>
      <description>A person travel card</description>
      <pubDate>Mon, 01 Jun 2020 06:23:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652145#M195762</guid>
      <dc:creator>umeshgiri48</dc:creator>
      <dc:date>2020-06-01T06:23:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to bring all data in one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652147#M195764</link>
      <description>&lt;P&gt;Try&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=AVD_ID device_NO device__No_id Route);
merge 
have (where=(Check_In=1) drop=Check_Out rename=(Time=In_Time Stop_Name=In_Stop_Name Amount=In_Amount)) 
have (where=(Check_Out=1) drop=Check_In rename=(Time=Out_Time Stop_Name=Out_Stop_Name Amount=Out_Amount))&lt;BR /&gt;;
by Date ID;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Jun 2020 06:34:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652147#M195764</guid>
      <dc:creator>sustagens</dc:creator>
      <dc:date>2020-06-01T06:34:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to bring all data in one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652156#M195771</link>
      <description>can we also do it by using proc sql</description>
      <pubDate>Mon, 01 Jun 2020 07:22:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652156#M195771</guid>
      <dc:creator>umeshgiri48</dc:creator>
      <dc:date>2020-06-01T07:22:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to bring all data in one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652192#M195783</link>
      <description>&lt;P&gt;I will make some assumptions:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;a "trip" can be identified by id and route&lt;/LI&gt;
&lt;LI&gt;it begins with a check_in and ends with a check_out&lt;/LI&gt;
&lt;LI&gt;there are always two observations (1 check_in and 1 check_out) for a trip&lt;/LI&gt;
&lt;LI&gt;check_in happens before check_out&lt;/LI&gt;
&lt;LI&gt;there cannot be overlapping time spans for two trips for a single id on the same route&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;are these assumptions correct?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For working with sequences of data, the data step is better suited than SQL.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jun 2020 10:59:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652192#M195783</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-01T10:59:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to bring all data in one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652254#M195795</link>
      <description>yes, all assumptions are correct</description>
      <pubDate>Mon, 01 Jun 2020 14:29:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652254#M195795</guid>
      <dc:creator>umeshgiri48</dc:creator>
      <dc:date>2020-06-01T14:29:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to bring all data in one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652415#M195861</link>
      <description>&lt;P&gt;Yes, but if your data are already sorted by DATE/ID, the DATA step solution provided by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;will be MUCH faster, since unlike SQL, it won't be obligated to do a Cartesian comparison of all checkin ID's against all checkout ID's while attempting to find all matches. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jun 2020 03:06:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652415#M195861</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-06-02T03:06:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to bring all data in one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652840#M196070</link>
      <description>&lt;P&gt;Then I suggest this data step solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm='09'x;
input
  date :mmddyy10.
  Time :time11.
  AVD_ID :$12.
  device_NO
  device__No_id
  ID :$12.
  Check_In
  Check_out
  Route
  Stop_name :$30.
  Amount
;
dt = dhms(date,0,0,time);
format dt e8601dt19.;
datalines;
01/03/2019	5:06:00 PM	B1910_EVD_04	166450	23442	602803314916:	1	0	22	Canada Water bus station	7.5
01/03/2019	5:38:00 PM	B1910_EVD_05	166451	23443	602803314916:	0	1	22	Tottenham Court Road station	-4.5
;

proc sort data=have;
by id dt;
run;

data trips;
retain /* for column order also */
  id
  in_time
  out_time
  check_in
  check_out
  in_stop_name
  out_stop_name
  in_amount
  out_amount
;
format
  in_time
  out_time e8601dt19.
;
set have (rename=(
  check_in=_check_in
  amount=out_amount
  dt=out_time
  stop_name=out_stop_name
));
by id route notsorted;
if first.route
then do;
  in_time = out_time;
  check_in = _check_in;
  in_stop_name = out_stop_name;
  in_amount = out_amount;
end;
if last.route;
keep
  id
  in_time
  out_time
  check_in
  check_out
  in_stop_name
  out_stop_name
  in_amount
  out_amount
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;By using datetimes instead of separate dates and times, this code will also deal correctly with trips crossing the day boundary.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jun 2020 12:16:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-bring-all-data-in-one-row/m-p/652840#M196070</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-03T12:16:39Z</dc:date>
    </item>
  </channel>
</rss>

