<?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: Compare current to previous entry (paradata) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550598#M152888</link>
    <description>&lt;P&gt;Hi Shumel,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;since "Landing_test_entry" is always followed by "Landing_test_exit" after sorting ( "Landing_test_exit"&amp;nbsp; always after "Landing_test_entry" ) :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; if activity = "landing_test_entry" and value = "1" then do; /* what about screen change ??? */&lt;BR /&gt;     if pre_activity = "Landing_test_exit" and value = "2" &lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;The statement above will treat &lt;CODE class=" language-sas"&gt;"Landing_test_exit"&lt;/CODE&gt; as the previous entry, which is not correct. We should treat it as the following entry?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you give some more hint on coding for the second part of your statement?&lt;/P&gt;</description>
    <pubDate>Fri, 12 Apr 2019 12:18:01 GMT</pubDate>
    <dc:creator>lydiawawa</dc:creator>
    <dc:date>2019-04-12T12:18:01Z</dc:date>
    <item>
      <title>Compare current to previous entry (paradata)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550510#M152854</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I'm hoping to determine user activity by how often they switch from one screen to another. I have the known columns Name, Activity, Screen, Value and is having trouble code for Switch:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&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&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Switch_within&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Tom&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Home_test_exist&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Home&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Switch_within&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Tom&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Landing_test_entry&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Landing&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Two_One&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Tom&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Landing_test_exit&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Landing&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Two_One&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Condition for Switch:&lt;/P&gt;&lt;P&gt;After sorting, Activity "Landing_test_entry" is not always followed by "Landing_test_exit"&amp;nbsp;&amp;nbsp; on screen "Landing", thus switch is determined by any screen visited by the same user with activity followed by "text_exit". If "xxx_text_exit", xxx = screen, value = 2 and "Landing_test_entry" = 1, then switch = one_two, and vice versa, switch = two_one. For any other screen visited by that user, if value does not change within a screen, then Switch = No, else Switch = Switch_within. If value equal a time value, then Switch = switch_time. An user can visit many possible screens, the table is only a briefed version of the actual dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Following is my attempted code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have; by name activity; run;

data want;
set have;
length pre_name $62. pre_value $250. switch $10.; by name activity;
 retain pre_activity pre_value; 
     if first.name then do; pre_activity = ""; 
       pre_value = ""; pre_screen = ""; 
        end; 
  pre_activity= lag(activity); 
  pre_value = lag(value); 
  pre_screen = lag(screen); 
      if activity = "Landing_test_entry" and value = "1" then do; 
         /*do not know how to code "Landing_test_exit" as the following item */ 
        activity = "Landing_test_exit" and value = "2" then do; 
         Switch = "One_Two";
       retain pre_screen;
         if screen_name = pre_screen then switch = "One_Two"; ...... 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>Fri, 12 Apr 2019 15:10:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550510#M152854</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2019-04-12T15:10:47Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current to previous entry (paradata)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550529#M152860</link>
      <description>&lt;P&gt;Please post test-data in usable form.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also note:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;String-comparison is case-sensitive, so "&lt;FONT face="courier new,courier"&gt;Landing_test_entry&lt;/FONT&gt;" is not the same as &lt;FONT face="courier new,courier"&gt;"landing_test_entry&lt;/FONT&gt;".&lt;/LI&gt;
&lt;LI&gt;Conditionally executing the retain-statement is not possible.&lt;/LI&gt;
&lt;LI&gt;Use lag or retain&lt;/LI&gt;
&lt;LI&gt;You reset the lagged variables on first name, but afterwards set the variables to the lagged values.&lt;/LI&gt;
&lt;LI&gt;To test if a value is a valid time-value, you can use isTime = not missing(input(Value, ?? time.));&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Apr 2019 07:56:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550529#M152860</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-04-12T07:56:06Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current to previous entry (paradata)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550532#M152861</link>
      <description>&lt;P&gt;Change your code to start as:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have; by name activity; run;  /* to be run if sort is needed */
data want;
 set have;
       by name;  
           retain pre_activity pre_value;&lt;BR /&gt;           pre_activity= lag(activity);
           pre_value = lag(value);
           pre_screen = lag(screen);
        
           if first.name then do;
              pre_activity = "";
              pre_value = "";
              pre_screen = "";
           end;&lt;BR /&gt;
          if activity = "landing_test_entry" and value = "1" then do;  /* what about screen change ??? */
             if pre_activity =  "Landing_test_exit" and value = "2" 
                then switch = "One-Two";
                else switch = ...
          end;
       .....
run;      &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you need to examine all possible permutations of &lt;STRONG&gt;Activity*Screen*Value&lt;/STRONG&gt; for a Name and their previous values,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;by filling next table:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;STRONG&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;current&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;activity&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; screen&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;value&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;vs &lt;U&gt;previous&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;activity&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;screen&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;NOTE: each row contains multiple lines !&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; value&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In case some combination is not valid assign the cell by &lt;STRONG&gt;N/A.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;When you finish to fulfill that table convert it into code.&lt;/P&gt;
&lt;P&gt;Run the code and come back if any issue.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Apr 2019 08:04:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550532#M152861</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-04-12T08:04:37Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current to previous entry (paradata)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550589#M152884</link>
      <description>&lt;P&gt;Hi Andreas,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That is a typo, it should be "&lt;FONT face="courier new,courier"&gt;Landing_test_entry&lt;/FONT&gt;", "L" is always capitalized. I will edit the original post.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Apr 2019 11:57:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550589#M152884</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2019-04-12T11:57:59Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current to previous entry (paradata)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550598#M152888</link>
      <description>&lt;P&gt;Hi Shumel,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;since "Landing_test_entry" is always followed by "Landing_test_exit" after sorting ( "Landing_test_exit"&amp;nbsp; always after "Landing_test_entry" ) :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; if activity = "landing_test_entry" and value = "1" then do; /* what about screen change ??? */&lt;BR /&gt;     if pre_activity = "Landing_test_exit" and value = "2" &lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;The statement above will treat &lt;CODE class=" language-sas"&gt;"Landing_test_exit"&lt;/CODE&gt; as the previous entry, which is not correct. We should treat it as the following entry?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you give some more hint on coding for the second part of your statement?&lt;/P&gt;</description>
      <pubDate>Fri, 12 Apr 2019 12:18:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550598#M152888</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2019-04-12T12:18:01Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current to previous entry (paradata)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550660#M152910</link>
      <description>&lt;P&gt;1.&lt;/P&gt;
&lt;P&gt;Looking at both lines:&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;P&gt;Mary&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;Landing_test_entry&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;Landing&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;&lt;STRONG&gt;One_Two&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;P&gt;Mary&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;Landing_test_exit&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;Landing&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;2&lt;/P&gt;
&lt;/TD&gt;
&lt;TD&gt;
&lt;P&gt;&lt;STRONG&gt;One_Two&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;it seems that at first line you compare the value to the &lt;U&gt;following&lt;/U&gt; line,&lt;/P&gt;
&lt;P&gt;while&amp;nbsp;at second line to compare the level to &lt;U&gt;previous&lt;/U&gt; line.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It can't be done in one step. There is no function to get value from the next observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there any other variable, like timestamp, to use for sorting the data.&lt;/P&gt;
&lt;P&gt;Otherwise I shall add a dummy variable to save original order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2.&lt;/P&gt;
&lt;P&gt;Your code contains&amp;nbsp;&lt;STRONG&gt;by name activity;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;You'll get error when reading &lt;STRONG&gt;Mary Candy&lt;/STRONG&gt; after&amp;nbsp;&lt;STRONG&gt;Mary Login&lt;/STRONG&gt; - as data is not sorted properly.&lt;/P&gt;
&lt;P&gt;Can&amp;nbsp;&lt;STRONG&gt;Candy&lt;/STRONG&gt; be treated before&amp;nbsp;&lt;STRONG&gt;Login,&amp;nbsp;&lt;/STRONG&gt;&amp;nbsp;even if it happen later ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. Is screen always first word of activity ? (screen = scan(activity,1,'_');&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Apr 2019 14:37:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550660#M152910</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-04-12T14:37:45Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current to previous entry (paradata)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550673#M152911</link>
      <description>&lt;P&gt;Hi Shumel,&lt;/P&gt;&lt;P&gt;The following are answers to your questions:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. There is no variable to sort the order, I think a dummy variable can be added within each name group to sort out the order.&lt;/P&gt;&lt;P&gt;2. I did not include it in my code when I posted this thread, the data was sorted prior and I have edited my post. Sort has been included.&lt;/P&gt;&lt;P&gt;3. No, screen name position should be determined by index() since it is not always the first word.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Apr 2019 15:00:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550673#M152911</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2019-04-12T15:00:47Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current to previous entry (paradata)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550676#M152912</link>
      <description>&lt;P&gt;Hi Shumel,&lt;BR /&gt;I ran into couple cases just now, and I revised the original condition for Switch in my first post. It turns out Activity "Landing_test_entry" is not always followed by "Landing_test_exit" . Some users do not have "Landing_test_exit". Sorry about the confusion.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Apr 2019 15:13:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550676#M152912</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2019-04-12T15:13:40Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current to previous entry (paradata)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550681#M152914</link>
      <description>&lt;P&gt;Next code is not complete, trying to deal with landing only, Check it and try to complete it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;step 1 reads row data and adds dummy varible named TIMESTAMP assigned withe the _N_&lt;/P&gt;
&lt;P&gt;to save original order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;step 2 sort the data (ascending TIMESTAMP)&lt;/P&gt;
&lt;P&gt;step 3&amp;nbsp;deals with second line in a pair.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;step 4 sort the data (descending TIMESTAMP)&lt;/P&gt;
&lt;P&gt;step 5 deals with second line, which is originally first line in a pair.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   length name $6 activity $20 screen $8 value $8;
   infile cards;
   input name $ activity $ screen $ valuex $ ;
   if strip(valuex) in ("1" "2") 
      then value = input(strip(valuex),1.);
      else value = input(valuex,hhmmss.); 
   timestamp = _N_; /* dummy variable to save original order */
cards;
Mary Home_test_entry Home 2
Mary Home_test_exit  Home 2
Mary Landing_test_entry Landing 1
Mary Landing_test_exit  Landing 2
Mary Login_test_entry Login 1
Mary Login_test_entry Login 2
Mary Login_test_exit Login 1
Mary Login_test_exit Login 2
Mary Login_test_time Login 08:09:12
Mary Candy_test_entry Candy 2
Mary Candy_test_exit Candy 2
Tom Home_test_entry Home 2
Tom Home_test_exit  Home 1
Mary Landing_test_entry Landing 2
Mary Landing_test_exit  Landing 1
;
run;

proc sort data=have; by name timestamp; run;  
data temp;
 set have;
  by name;  
     retain pre_activity pre_value;           
     pre_activity= lag(activity);
     pre_value = lag(value);
     pre_screen = lag(screen);
    
     if first.name then do;
        pre_activity = "";
        pre_value = "";
        pre_screen = "";
     end;
     
     if screen = "Landing" then do;
        if activity = "Landing_test_exit" then do;
           if value ne pre_value then do;
              if value=2 then switch = "One-Two";
              else switch = "Two-One";
           end;
        /* else treat while value = pre_value ??? */
       end;
     end;
        
run;  

proc sort data=have; by name descending timestamp; run;  

data want;
 set temp;
  by name;  
     retain pre_activity pre_value;           
     pre_activity= lag(activity);
     pre_value = lag(value);
     pre_screen = lag(screen);
     drop pre_activity pre_value pre_screen;
    
     if first.name then do;
        pre_activity = "";
        pre_value = "";
        pre_screen = "";
     end;
     
     if screen = "Landing" then do;
        if activity = "Landing_test_entry" then do;
           if value ne pre_value then do;
              if value=1 then switch = "One-Two";
              else switch = "Two-One";
           end;
        /* else treat while value = pre_value ??? */ 
       end;
     end;
        
run;  &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 12 Apr 2019 15:18:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550681#M152914</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-04-12T15:18:02Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current to previous entry (paradata)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550685#M152916</link>
      <description>my last posy was created and sent before reading your last remarks</description>
      <pubDate>Fri, 12 Apr 2019 15:20:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550685#M152916</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-04-12T15:20:51Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current to previous entry (paradata)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550690#M152918</link>
      <description>&lt;P&gt;On my first post I noticed:&lt;/P&gt;
&lt;PRE&gt;examine all possible permutations of Activity*Screen*Value for a Name and their previous values,

 by filling next table:

         current           activity          screen       value

     vs previous

         activity

         screen             NOTE: each row contains multiple lines !

          value&lt;/PRE&gt;
&lt;P&gt;try to check your data and complete that table before coding.&lt;/P&gt;
&lt;P&gt;It is called a "Decisions Table".&lt;/P&gt;</description>
      <pubDate>Fri, 12 Apr 2019 15:25:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550690#M152918</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-04-12T15:25:02Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current to previous entry (paradata)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550691#M152919</link>
      <description>Yes, I'm aware of that. Thank you for the reminder.</description>
      <pubDate>Fri, 12 Apr 2019 15:26:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550691#M152919</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2019-04-12T15:26:45Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current to previous entry (paradata)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550783#M152947</link>
      <description>Hi Shmuel, could you give some suggestions on how to populate the "Switch_within" and "No" values for Switch. Your code only populate part of my question. And this is based on before my most recent revision.&lt;BR /&gt;</description>
      <pubDate>Fri, 12 Apr 2019 20:54:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550783#M152947</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2019-04-12T20:54:44Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current to previous entry (paradata)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550812#M152960</link>
      <description>&lt;P&gt;Based on your requirement:&lt;/P&gt;
&lt;PRE&gt;For any other screen visited by that user, if value does not change within a screen, then Switch = No, else Switch = Switch_within. If value equal a time value, then Switch = switch_time.&lt;/PRE&gt;
&lt;P&gt;I would add to step-3 (ascending "timestamp" keeps &lt;STRONG&gt;sort by name activity&lt;/STRONG&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if screen ne "Landing" and 
   screen = pre_screen then do;
    if value &amp;gt; 2  then switch = "Switch_time"; else
    if value = pre_value then switch = "No";
                         else switch = "Switch_within";
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;that deals within screen (exclude Landing) for second line on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next code add to step-4 (descending "timestamp"):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if screen ne "Landing" and
   screen ne pre_screen and
   switch = " " 
then switch = pre_switch; &amp;nbsp;&amp;nbsp;&amp;nbsp;/*&amp;nbsp;add&amp;nbsp;PRE_SWITCH&amp;nbsp;to&amp;nbsp;RETAIN&amp;nbsp;and&amp;nbsp;assign&amp;nbsp;LAG_SWITCH&amp;nbsp;on top of the step*/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I did not test that added code. Try it. I hope it will give you what you want.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Apr 2019 23:22:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550812#M152960</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-04-12T23:22:22Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current to previous entry (paradata)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550813#M152961</link>
      <description>If time is 00:00:01 or 00:00:02 above code may be erroneous.</description>
      <pubDate>Fri, 12 Apr 2019 23:26:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550813#M152961</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-04-12T23:26:03Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current to previous entry (paradata)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550849#M152977</link>
      <description>&lt;P&gt;Next is my final tested code.&lt;/P&gt;
&lt;P&gt;I have added a test line for landing entry without exit.&lt;/P&gt;
&lt;P&gt;Results fits your posted output.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   length name $6 activity $20 screen $8 value $8;
   infile cards;
   input name $ activity $ screen $ valuex $ ;
   value_time = 0; /* flag */
   if strip(valuex) in ("1" "2") 
      then value = input(strip(valuex),1.);
      else do;
           value = input(valuex,hhmmss.); 
           value_time = 1;
      end;
   timestamp = _N_; /* dummy variable to save original order */
cards;
Mary Home_test_entry Home 2
Mary Home_test_exit  Home 2
Mary Landing_test_entry Landing 1
Mary Landing_test_exit  Landing 2
Mary Login_test_entry Login 1
Mary Login_test_entry Login 2
Mary Login_test_exit Login 1
Mary Login_test_exit Login 2
Mary Login_test_time Login 08:09:12
Mary Candy_test_entry Candy 2
Mary Candy_test_exit Candy 2
Tom Home_test_entry Home 2
Tom Home_test_exit  Home 1
Mary Landing_test_entry Landing 2
Mary Landing_test_exit  Landing 1
Xxxx Landing_test_entry Landing 1
;
run;

proc sort data=have; by name timestamp; run;  
data temp;
 set have;
  by name;  
     length switch $15;
     retain pre_activity pre_value;           
     pre_activity= lag(activity);
     pre_value = lag(value);
     pre_screen = lag(screen);
     drop pre_activity pre_value pre_screen ;
     
     if first.name then do;
        pre_activity = "";
        pre_value = "";
        pre_screen = "";
     end;
     
     if screen = "Landing" then do;
        if activity = "Landing_test_exit" then do;
           if value ne pre_value then do;
              if value=2 
                 then switch = "One-Two";
                 else switch = "Two-One";
           end;
        /* else treat while value = pre_value ??? */
       end;
     end;
	 
	 if screen ne "Landing" and 
        screen = pre_screen then do;
        if value_time = 1
           then switch = "Switch_time"; else
        if value = pre_value 
		   then switch = "No";
           else switch = "Switch_within";
end;
        
run;  

proc sort data=temp; 
  by name descending timestamp; 
run;  

data want;
 set temp;
  by name; 
     length pre_switch $15;
     retain pre_activity pre_value;           
     pre_activity= lag(activity);
     pre_value = lag(value);
     pre_screen = lag(screen);
	 pre_switch = lag(switch);
     drop pre_activity pre_value pre_screen pre_switch;
    
     if first.name then do;
        pre_activity = "";
        pre_value = "";
        pre_screen = "";
     end;
     
     if screen = "Landing" then do;
        if activity = "Landing_test_entry" then do;
           if value ne pre_value then do;
              if value=1 then switch = "One-Two";
              else switch = "Two-One";
           end;
        /* else treat while value = pre_value ??? */ 
       end;
     end;
    
	if screen ne "Landing" and
       /*screen ne pre_screen and*/
       switch = " " 
    then switch = pre_switch;    
run;  
proc sort data=want; by name timestamp; run;  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 13 Apr 2019 16:26:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550849#M152977</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-04-13T16:26:46Z</dc:date>
    </item>
    <item>
      <title>Re: Compare current to previous entry (paradata)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550867#M152982</link>
      <description>Thank you Shmuel, I will be able to check the code on Monday, currently, have no access to the data.</description>
      <pubDate>Sun, 14 Apr 2019 01:36:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-current-to-previous-entry-paradata/m-p/550867#M152982</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2019-04-14T01:36:19Z</dc:date>
    </item>
  </channel>
</rss>

