<?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 do I flag a &amp;quot;new&amp;quot; situation within the same id? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-flag-a-quot-new-quot-situation-within-the-same-id/m-p/708776#M217862</link>
    <description>&lt;P&gt;It is not clear to me what your logic is but your example makes it look like you want to flag the LAST observation for a department except when it is also the last observation for the ID.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input id datetime :$16. department :$10. count flag ;
cards;
1 01-01-2020.10:00 aaa 1 1
1 02-01-2020.10:00 bbb 2 1
1 03-01-2020.10:00 ccc 3 1
1 04-01-2020.10:00 aaa 4 0
1 05-01-2020.10:00 aaa 5 1
1 06-01-2020.10:00 ccc 6 0
2 01-01-2020.10:00 bbb 1 0
2 02-01-2020.10:00 bbb 2 0
2 03-01-2020.10:00 bbb 3 0
2 04-01-2020.10:00 bbb 4 0
2 05-01-2020.10:00 bbb 5 0
3 01-01-2020.10:00 aaa 1 1
3 02-01-2020.10:00 ddd 2 0
3 03-01-2020.10:00 ddd 3 0
3 04-01-2020.10:00 ddd 4 1
3 05-01-2020.10:00 aaa 5 0
;

data want;
  set have ;
  by id department notsorted;
  want=last.department and not last.id;
run;

proc print;
run;

proc compare data=want;
  var flag;
  with want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                                      The COMPARE Procedure
                              Comparisons of variables in WORK.WANT
                                          (Method=EXACT)

                                        Data Set Summary

                   Dataset             Created          Modified  NVar    NObs

                   WORK.WANT  30DEC20:10:38:27  30DEC20:10:38:27     6      16

NOTE: No unequal values were found. All values compared are exactly equal.
&lt;/PRE&gt;</description>
    <pubDate>Wed, 30 Dec 2020 15:40:02 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-12-30T15:40:02Z</dc:date>
    <item>
      <title>How do I flag a "new" situation within the same id?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-flag-a-quot-new-quot-situation-within-the-same-id/m-p/708564#M217772</link>
      <description>&lt;P&gt;I have a dataset where I want to "flag" every time a call is forwarded. It can be forwarded back and forth between the same departments, but have to count every time it occurs. A call can also be forwarded within the same department but then it shall not be flaged. When a call is received it gets an id that is "locked" until the call has ended.&lt;/P&gt;&lt;P&gt;I have tried to give the example below. The first four columns is what I already have.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;TD&gt;datetime&lt;/TD&gt;&lt;TD&gt;department&lt;/TD&gt;&lt;TD&gt;count&lt;/TD&gt;&lt;TD&gt;flag&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;01-01-2020 10:00&lt;/TD&gt;&lt;TD&gt;aaa&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;02-01-2020 10:00&lt;/TD&gt;&lt;TD&gt;bbb&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;03-01-2020 10:00&lt;/TD&gt;&lt;TD&gt;ccc&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;04-01-2020 10:00&lt;/TD&gt;&lt;TD&gt;aaa&lt;/TD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TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;01-01-2020 10:00&lt;/TD&gt;&lt;TD&gt;aaa&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;02-01-2020 10:00&lt;/TD&gt;&lt;TD&gt;ddd&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;03-01-2020 10:00&lt;/TD&gt;&lt;TD&gt;ddd&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;04-01-2020 10:00&lt;/TD&gt;&lt;TD&gt;ddd&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;05-01-2020 10:00&lt;/TD&gt;&lt;TD&gt;aaa&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Tue, 29 Dec 2020 13:56:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-flag-a-quot-new-quot-situation-within-the-same-id/m-p/708564#M217772</guid>
      <dc:creator>tlianee</dc:creator>
      <dc:date>2020-12-29T13:56:08Z</dc:date>
    </item>
    <item>
      <title>Re: How do I flag a "new" situation within the same id?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-flag-a-quot-new-quot-situation-within-the-same-id/m-p/708574#M217774</link>
      <description>&lt;P&gt;LOL each transfer takes a day.&amp;nbsp; Poor customers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is your "Want" table accurate? I think this problem will use a LAG() function on DEPARTMENT, at least.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Dec 2020 14:54:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-flag-a-quot-new-quot-situation-within-the-same-id/m-p/708574#M217774</guid>
      <dc:creator>PhilC</dc:creator>
      <dc:date>2020-12-29T14:54:12Z</dc:date>
    </item>
    <item>
      <title>Re: How do I flag a "new" situation within the same id?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-flag-a-quot-new-quot-situation-within-the-same-id/m-p/708580#M217778</link>
      <description>&lt;P&gt;You will have to describe in some considerable detail a couple of things. First, how to identify a "call forward". Second, why would the first entry for ID 1 and 3, assuming the time is supposed to be sequential, be flagged when it is not for the first entry for ID 2?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any rule should describe how to use the values shown.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Dec 2020 16:02:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-flag-a-quot-new-quot-situation-within-the-same-id/m-p/708580#M217778</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-12-29T16:02:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do I flag a "new" situation within the same id?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-flag-a-quot-new-quot-situation-within-the-same-id/m-p/708583#M217780</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    infile cards dlm=',';
    input id dt department $ count;

    informat dt anydtdtm20. department $3.;
    format dt datetime20. department $3.;
    cards;
1,01-01-2020:10:00:00,aaa,1
1,02-01-2020:10:00:00,bbb,2
1,03-01-2020:10:00:00,ccc,3
1,04-01-2020:10:00:00,aaa,4
1,05-01-2020:10:00:00,aaa,5
1,06-01-2020:10:00:00,ccc,6
2,01-01-2020:10:00:00,bbb,1
2,02-01-2020:10:00:00,bbb,2
2,03-01-2020:10:00:00,bbb,3
2,04-01-2020:10:00:00,bbb,4
2,05-01-2020:10:00:00,bbb,5
3,01-01-2020:10:00:00,aaa,1
3,02-01-2020:10:00:00,ddd,2
3,03-01-2020:10:00:00,ddd,3
3,04-01-2020:10:00:00,ddd,4
3,05-01-2020:10:00:00,aaa,5
;
run;

data temp;
    merge have have(firstobs=2 keep=department rename=(department=dept2));
run;

data want;
    set temp;
    by Id;

    if not last.id and dept2 ne department then flag=1;
    else flag=0;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Dec 2020 16:59:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-flag-a-quot-new-quot-situation-within-the-same-id/m-p/708583#M217780</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-12-29T16:59:21Z</dc:date>
    </item>
    <item>
      <title>Re: How do I flag a "new" situation within the same id?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-flag-a-quot-new-quot-situation-within-the-same-id/m-p/708720#M217842</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15329"&gt;@PhilC&lt;/a&gt;&amp;nbsp;the datetimes are only examples. I have not had success with getting lag to work as the same department can be on the first line in the next id.&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;it is what happens to the line if the call that should trigger a flag. Since the next line in ID1 has another deperatment the first line get a flag as this call is transfered. ID 2 all the lines are within the same department and should therefore not get a flag, which is the same with observation 2 and 3 in ID 3 where these are within the same department but the fourth should get a flag as the next line is a diffenrent department. This also means that the last observation per ID never can have a flag.&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30622"&gt;@gamotte&lt;/a&gt;&amp;nbsp;Thanks...I'll look into your solution.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Dec 2020 07:43:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-flag-a-quot-new-quot-situation-within-the-same-id/m-p/708720#M217842</guid>
      <dc:creator>tlianee</dc:creator>
      <dc:date>2020-12-30T07:43:02Z</dc:date>
    </item>
    <item>
      <title>Re: How do I flag a "new" situation within the same id?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-flag-a-quot-new-quot-situation-within-the-same-id/m-p/708776#M217862</link>
      <description>&lt;P&gt;It is not clear to me what your logic is but your example makes it look like you want to flag the LAST observation for a department except when it is also the last observation for the ID.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input id datetime :$16. department :$10. count flag ;
cards;
1 01-01-2020.10:00 aaa 1 1
1 02-01-2020.10:00 bbb 2 1
1 03-01-2020.10:00 ccc 3 1
1 04-01-2020.10:00 aaa 4 0
1 05-01-2020.10:00 aaa 5 1
1 06-01-2020.10:00 ccc 6 0
2 01-01-2020.10:00 bbb 1 0
2 02-01-2020.10:00 bbb 2 0
2 03-01-2020.10:00 bbb 3 0
2 04-01-2020.10:00 bbb 4 0
2 05-01-2020.10:00 bbb 5 0
3 01-01-2020.10:00 aaa 1 1
3 02-01-2020.10:00 ddd 2 0
3 03-01-2020.10:00 ddd 3 0
3 04-01-2020.10:00 ddd 4 1
3 05-01-2020.10:00 aaa 5 0
;

data want;
  set have ;
  by id department notsorted;
  want=last.department and not last.id;
run;

proc print;
run;

proc compare data=want;
  var flag;
  with want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                                      The COMPARE Procedure
                              Comparisons of variables in WORK.WANT
                                          (Method=EXACT)

                                        Data Set Summary

                   Dataset             Created          Modified  NVar    NObs

                   WORK.WANT  30DEC20:10:38:27  30DEC20:10:38:27     6      16

NOTE: No unequal values were found. All values compared are exactly equal.
&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Dec 2020 15:40:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-flag-a-quot-new-quot-situation-within-the-same-id/m-p/708776#M217862</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-30T15:40:02Z</dc:date>
    </item>
  </channel>
</rss>

