<?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 get yhe difference of the first and last observation in a group? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-yhe-difference-of-the-first-and-last-observation-in-a/m-p/714674#M220666</link>
    <description>&lt;P&gt;Use BY group processing with FIRST and LAST and RETAIN.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;

by Student; *may require presorting to have sorted by student;

*holds value across the rows as you go down;
retain start_time;
*assigns the time to a new variable, start_time;
if first.Student then start_time = time;

*if last record for a student then calculate the duration and output the record;
if last.student then do;
      duration = time - start_time;
      output;
end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Alternatively, if you know that the smallest time is always the start and the largest time is always the end then you can use the RANGE option from PROC MEANS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=have RANGE noprint;
class Student;
var time;
output out=want range = duration;
run;

proc print data=want;
var student duration;
format duration time.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/361528"&gt;@Shradha1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I have a data set for students and various time points.&lt;BR /&gt;Student time&lt;BR /&gt;A 12:10&lt;BR /&gt;A 12:11&lt;BR /&gt;A 12:20&lt;BR /&gt;A 12:45&lt;BR /&gt;B 3:10&lt;BR /&gt;B 3:16&lt;BR /&gt;B 3:32&lt;BR /&gt;C 8:05&lt;BR /&gt;C 8:54&lt;BR /&gt;C 9:10&lt;BR /&gt;I want the difference between the first and last observation for a particular student. I want the result as&lt;BR /&gt;A 00:35&lt;BR /&gt;B 00:22&lt;BR /&gt;C 00:55&lt;BR /&gt;&lt;BR /&gt;How do I do this?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 27 Jan 2021 17:14:43 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-01-27T17:14:43Z</dc:date>
    <item>
      <title>How to get yhe difference of the first and last observation in a group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-yhe-difference-of-the-first-and-last-observation-in-a/m-p/714667#M220659</link>
      <description>I have a data set for students and various time points.&lt;BR /&gt;Student time&lt;BR /&gt;A 12:10&lt;BR /&gt;A 12:11&lt;BR /&gt;A 12:20&lt;BR /&gt;A 12:45&lt;BR /&gt;B 3:10&lt;BR /&gt;B 3:16&lt;BR /&gt;B 3:32&lt;BR /&gt;C 8:05&lt;BR /&gt;C 8:54&lt;BR /&gt;C 9:10&lt;BR /&gt;I want the difference between the first and last observation for a particular student. I want the result as&lt;BR /&gt;A  00:35&lt;BR /&gt;B  00:22&lt;BR /&gt;C  00:55&lt;BR /&gt;&lt;BR /&gt;How do I do this?&lt;BR /&gt;</description>
      <pubDate>Wed, 27 Jan 2021 16:58:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-yhe-difference-of-the-first-and-last-observation-in-a/m-p/714667#M220659</guid>
      <dc:creator>Shradha1</dc:creator>
      <dc:date>2021-01-27T16:58:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to get yhe difference of the first and last observation in a group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-yhe-difference-of-the-first-and-last-observation-in-a/m-p/714671#M220663</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input Student $ time :time.;
format time time.;
cards;
A 12:10
A 12:11
A 12:20
A 12:45
B 3:10
B 3:16
B 3:32
C 8:05
C 8:54
C 9:10
;

proc sql;
 create table want as
 select student, range(time) as diff format=time5.
 from have
 group by student;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Jan 2021 17:12:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-yhe-difference-of-the-first-and-last-observation-in-a/m-p/714671#M220663</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-01-27T17:12:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to get yhe difference of the first and last observation in a group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-yhe-difference-of-the-first-and-last-observation-in-a/m-p/714673#M220665</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc summary data=have nway;
 class student;
 var time;
 output out=want(drop=_:) range=Diff;
 format time time5.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Jan 2021 17:14:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-yhe-difference-of-the-first-and-last-observation-in-a/m-p/714673#M220665</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-01-27T17:14:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to get yhe difference of the first and last observation in a group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-yhe-difference-of-the-first-and-last-observation-in-a/m-p/714674#M220666</link>
      <description>&lt;P&gt;Use BY group processing with FIRST and LAST and RETAIN.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;

by Student; *may require presorting to have sorted by student;

*holds value across the rows as you go down;
retain start_time;
*assigns the time to a new variable, start_time;
if first.Student then start_time = time;

*if last record for a student then calculate the duration and output the record;
if last.student then do;
      duration = time - start_time;
      output;
end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Alternatively, if you know that the smallest time is always the start and the largest time is always the end then you can use the RANGE option from PROC MEANS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=have RANGE noprint;
class Student;
var time;
output out=want range = duration;
run;

proc print data=want;
var student duration;
format duration time.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/361528"&gt;@Shradha1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I have a data set for students and various time points.&lt;BR /&gt;Student time&lt;BR /&gt;A 12:10&lt;BR /&gt;A 12:11&lt;BR /&gt;A 12:20&lt;BR /&gt;A 12:45&lt;BR /&gt;B 3:10&lt;BR /&gt;B 3:16&lt;BR /&gt;B 3:32&lt;BR /&gt;C 8:05&lt;BR /&gt;C 8:54&lt;BR /&gt;C 9:10&lt;BR /&gt;I want the difference between the first and last observation for a particular student. I want the result as&lt;BR /&gt;A 00:35&lt;BR /&gt;B 00:22&lt;BR /&gt;C 00:55&lt;BR /&gt;&lt;BR /&gt;How do I do this?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2021 17:14:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-yhe-difference-of-the-first-and-last-observation-in-a/m-p/714674#M220666</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-01-27T17:14:43Z</dc:date>
    </item>
  </channel>
</rss>

