<?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 check if the last observation of a group is different from the one prior to it? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-last-observation-of-a-group-is-different/m-p/714906#M220768</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by student; /* add notsorted option if necessary */
ldev = lag(device); /* keep previous value of device */
if
  last.student /* test only at last observation of student */
  and device ne ldev /* compare devices */
  and not first.student /* students with only one entry are never flagged */
then flag = 1;
else flag = 0;
drop ldev;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 28 Jan 2021 08:26:43 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-01-28T08:26:43Z</dc:date>
    <item>
      <title>How to check if the last observation of a group is different from the one prior to it?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-last-observation-of-a-group-is-different/m-p/714904#M220766</link>
      <description>I have a list of students and a list of devices they have used for various sessions, all in a sorted form:&lt;BR /&gt;Student Device&lt;BR /&gt;A.            Mac&lt;BR /&gt;A.             Tablet&lt;BR /&gt;A.              Laptop&lt;BR /&gt;B.              PC&lt;BR /&gt;B.              ANDROID&lt;BR /&gt;B.              ANDROID&lt;BR /&gt;C.              TABLET&lt;BR /&gt;C.               TABLET&lt;BR /&gt;C.                IPHONE&lt;BR /&gt;&lt;BR /&gt;I need to create a flag for those students for whom the device in their last session doesn't match with the device in the previous session.&lt;BR /&gt;So for the above data, the flag will be created for student A and C. How do I proceed? Please help</description>
      <pubDate>Thu, 28 Jan 2021 08:09:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-last-observation-of-a-group-is-different/m-p/714904#M220766</guid>
      <dc:creator>Shradha1</dc:creator>
      <dc:date>2021-01-28T08:09:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to check if the last observation of a group is different from the one prior to it?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-last-observation-of-a-group-is-different/m-p/714905#M220767</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Student $ Device $;
datalines;
A Mac    
A Tablet 
A Laptop 
B PC     
B ANDROID
B ANDROID
C TABLET 
C TABLET 
C IPHONE 
;

data want(drop = d);
   do until (last.Student);
      set have;
      by Student;
      d = lag(Device);
      flag = (last.Student and Device ne d);
   end;
   do until (last.Student);
      set have;
      by Student;
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Student  Device   flag 
A        Mac      1 
A        Tablet   1 
A        Laptop   1 
B        PC       0 
B        ANDROID  0 
B        ANDROID  0 
C        TABLET   1 
C        TABLET   1 
C        IPHONE   1 

&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 Jan 2021 08:16:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-last-observation-of-a-group-is-different/m-p/714905#M220767</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-28T08:16:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to check if the last observation of a group is different from the one prior to it?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-last-observation-of-a-group-is-different/m-p/714906#M220768</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by student; /* add notsorted option if necessary */
ldev = lag(device); /* keep previous value of device */
if
  last.student /* test only at last observation of student */
  and device ne ldev /* compare devices */
  and not first.student /* students with only one entry are never flagged */
then flag = 1;
else flag = 0;
drop ldev;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 Jan 2021 08:26:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-last-observation-of-a-group-is-different/m-p/714906#M220768</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-28T08:26:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to check if the last observation of a group is different from the one prior to it?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-last-observation-of-a-group-is-different/m-p/714918#M220773</link>
      <description>Let's simplify:&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;by student device notsorted;&lt;BR /&gt;if last.student and first.device then flag=1;&lt;BR /&gt;run;</description>
      <pubDate>Thu, 28 Jan 2021 09:33:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-last-observation-of-a-group-is-different/m-p/714918#M220773</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-01-28T09:33:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to check if the last observation of a group is different from the one prior to it?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-last-observation-of-a-group-is-different/m-p/714931#M220775</link>
      <description>&lt;P&gt;That would flag all students who have only one entry. The condition should be&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if last.student ne first.student and first.device then flag=1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jan 2021 10:00:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-last-observation-of-a-group-is-different/m-p/714931#M220775</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-28T10:00:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to check if the last observation of a group is different from the one prior to it?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-last-observation-of-a-group-is-different/m-p/714969#M220788</link>
      <description>&lt;P&gt;Good catch,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;.&amp;nbsp; I could have been more careful.&amp;nbsp; However, the suggestion you made won't do the job.&amp;nbsp; It will flag the first observation for each student (as long as the student has more than one observation).&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a more careful variation:&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 device notsorted;
if last.student and first.device and not first.student then flag=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jan 2021 13:15:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-last-observation-of-a-group-is-different/m-p/714969#M220788</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-01-28T13:15:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to check if the last observation of a group is different from the one prior to it?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-last-observation-of-a-group-is-different/m-p/714976#M220791</link>
      <description>&lt;P&gt;You're right. Yours is the first solution I also came up with, but then I obviously wanted to be overly cute &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jan 2021 13:29:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-if-the-last-observation-of-a-group-is-different/m-p/714976#M220791</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-28T13:29:38Z</dc:date>
    </item>
  </channel>
</rss>

