<?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: Data Set / IF-THEN statement between specific lines in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/568631#M160101</link>
    <description>&lt;P&gt;Do you need the information in the log or in a dataset? If you need a dataset, please post some details on the expected structure.&lt;/P&gt;
&lt;P&gt;Is the variable "code" numeric or alphanumeric? The value of "code" seems to be related to the value of "transaction", is this the case or just a coincidence?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is one possible solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   attrib 
      time length=8 format=time8. informat=time.
      code length=8
      transaction length=$30
   ;
   infile datalines dlmstr='  ';
   input time code transaction;

datalines;
7:00:30  801  Login
7:00:35  405  Transaction X
7:00:40  404  Transaction Y
7:02:30  801  Login 
7:02:35  404  Transaction Y
7:02:40  404  Transaction Y
;
run;


data _null_;
   set have end=jobDone;

   length x_cycles y_cycles current_cycle 8;
   retain x_cycles y_cycles 0 current_cycle .;

   array cycles[404:405] y_cycles x_cycles;

   if code = 801 then do;
      if not missing(current_cycle) then do;
         cycles[current_cycle] = cycles[current_cycle] + 1;
         current_cycle = .;
      end;
   end;
   else do;
      current_cycle = max(current_cycle, code);
   end;

   
   if jobDone then do;
      cycles[current_cycle] = cycles[current_cycle] + 1;
      put x_cycles "X cycle and " y_cycles "Y cycle";
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 25 Jun 2019 05:25:01 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2019-06-25T05:25:01Z</dc:date>
    <item>
      <title>Data Set / IF-THEN statement between specific lines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/568625#M160096</link>
      <description>&lt;P&gt;Hi guys!&lt;/P&gt;&lt;P&gt;So, basically what I want to do is to identify certain patterns (cycles actually) of users using a machine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to use a IF-THEN statement between two specific transactions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code needs to run from one Login to the next one and use the code of transactions between to identify the cycles.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TIME&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;CODE&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TRANSACTION&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;7:00:30&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;801&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Login&lt;/P&gt;&lt;P&gt;7:00:35&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;405&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Transaction X&lt;/P&gt;&lt;P&gt;7:00:40&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;404&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Transaction Y&lt;/P&gt;&lt;P&gt;7:02:30&amp;nbsp; &amp;nbsp; &amp;nbsp; 801&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Login&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;7:02:35&amp;nbsp; &amp;nbsp; &amp;nbsp;404&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Transaction Y&lt;/P&gt;&lt;P&gt;7:02:40&amp;nbsp; &amp;nbsp; &amp;nbsp;404&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Transaction Y&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output should be: 1 X Cycle and 1 Y Cycle.&lt;/P&gt;&lt;P&gt;Also, Transaction X is stronger than Transaction Y, then, if X occurs, the cycle is identified as a ''X Cycle''&lt;/P&gt;&lt;P&gt;If not, we have a ''Y Cycle''.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas? Thank you so much!&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jun 2019 02:58:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/568625#M160096</guid>
      <dc:creator>feliperczx</dc:creator>
      <dc:date>2019-06-25T02:58:08Z</dc:date>
    </item>
    <item>
      <title>Re: Data Set / IF-THEN statement between specific lines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/568626#M160097</link>
      <description>&lt;P&gt;Why not use login auditing tools to deterring the amount of time the user is logged in?&lt;/P&gt;
&lt;P&gt;take the logged in time and create spans until the next login.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jun 2019 03:13:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/568626#M160097</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2019-06-25T03:13:16Z</dc:date>
    </item>
    <item>
      <title>Re: Data Set / IF-THEN statement between specific lines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/568627#M160098</link>
      <description>&lt;P&gt;The amount of time the user is logged in it is not important here.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The LOGIN transaction is just a flag to let me know that another cycle began/ended&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I really need to do is to identify the transactions between those LOGIN transactions&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;not sure If i get your suggestion...could you give me an example of what you thought?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jun 2019 03:23:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/568627#M160098</guid>
      <dc:creator>feliperczx</dc:creator>
      <dc:date>2019-06-25T03:23:23Z</dc:date>
    </item>
    <item>
      <title>Re: Data Set / IF-THEN statement between specific lines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/568630#M160100</link>
      <description>&lt;P&gt;Does the following help? It&amp;nbsp;assumes that 'Login' or end-of-file mark the end of a cycle.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
   input Time : hhmmss. Code : $3. Transaction &amp;amp; $20. ;
   format Time time8. ;
cards ;
7:00:30 801 Login
7:00:35 405 Transaction X
7:00:40 404 Transaction Y
7:02:30 801 Login  
7:02:35 404 Transaction Y
7:02:40 404 Transaction Y
;
run ;

proc sort data=have;
   by time ;
run ;

data want ;
   set have end=eof;
   retain _XFound 
             _YFound ;

   if _n_&amp;gt;1 and (Transaction='Login' or eof) then do ;  
       if        _XFound then XCycleCount+1 ;   
       else if _YFound then YCycleCount+1 ;

       _XFound=0 ;
       _YFound=0 ;
   end ;
   
   if        Transaction='Transaction X' then _XFound=1 ;      
   else if Transaction='Transaction Y' then _YFound=1 ;      

   if eof then output ;

   keep XCycleCount YCycleCount ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Jun 2019 05:23:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/568630#M160100</guid>
      <dc:creator>seemiyah</dc:creator>
      <dc:date>2019-06-25T05:23:35Z</dc:date>
    </item>
    <item>
      <title>Re: Data Set / IF-THEN statement between specific lines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/568631#M160101</link>
      <description>&lt;P&gt;Do you need the information in the log or in a dataset? If you need a dataset, please post some details on the expected structure.&lt;/P&gt;
&lt;P&gt;Is the variable "code" numeric or alphanumeric? The value of "code" seems to be related to the value of "transaction", is this the case or just a coincidence?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is one possible solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   attrib 
      time length=8 format=time8. informat=time.
      code length=8
      transaction length=$30
   ;
   infile datalines dlmstr='  ';
   input time code transaction;

datalines;
7:00:30  801  Login
7:00:35  405  Transaction X
7:00:40  404  Transaction Y
7:02:30  801  Login 
7:02:35  404  Transaction Y
7:02:40  404  Transaction Y
;
run;


data _null_;
   set have end=jobDone;

   length x_cycles y_cycles current_cycle 8;
   retain x_cycles y_cycles 0 current_cycle .;

   array cycles[404:405] y_cycles x_cycles;

   if code = 801 then do;
      if not missing(current_cycle) then do;
         cycles[current_cycle] = cycles[current_cycle] + 1;
         current_cycle = .;
      end;
   end;
   else do;
      current_cycle = max(current_cycle, code);
   end;

   
   if jobDone then do;
      cycles[current_cycle] = cycles[current_cycle] + 1;
      put x_cycles "X cycle and " y_cycles "Y cycle";
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Jun 2019 05:25:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/568631#M160101</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-06-25T05:25:01Z</dc:date>
    </item>
    <item>
      <title>Re: Data Set / IF-THEN statement between specific lines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/568720#M160155</link>
      <description>&lt;P&gt;I need the information on a dataset.&lt;/P&gt;&lt;P&gt;Would be great If I had a new column/variable (CYCLE) and at the end of each cycle, a classification for the prior one.&amp;nbsp;&lt;BR /&gt;For example, expected structure:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;TIME     CODE TRANSACTION       CYCLE&lt;BR /&gt;7:00:30  801  Login
7:00:35  405  Transaction X
7:00:40  404  Transaction Y     X Cycle
7:02:30  801  Login 
7:02:35  404  Transaction Y
7:02:40  404  Transaction Y&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Y&amp;nbsp;Cycle&lt;BR /&gt;7:03:60&amp;nbsp;&amp;nbsp;801&amp;nbsp;&amp;nbsp;Login&lt;BR /&gt;7:04:15&amp;nbsp;&amp;nbsp;405&amp;nbsp;&amp;nbsp;Transaction&amp;nbsp;X&lt;BR /&gt;7:04:20&amp;nbsp;&amp;nbsp;404&amp;nbsp;&amp;nbsp;Transaction&amp;nbsp;Y&lt;BR /&gt;7:05:01&amp;nbsp;&amp;nbsp;405&amp;nbsp;&amp;nbsp;Transaction&amp;nbsp;X&lt;BR /&gt;7:06:03&amp;nbsp;&amp;nbsp;409&amp;nbsp;&amp;nbsp;Transaction&amp;nbsp;X&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;X&amp;nbsp;Cycle&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;The variable CODE is a character variable. The value of 'CODE' it is indeed related to the value of 'TRANSACTION', but I do that classification in a prior step, for example:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;IF CODE = '801' THEN TRANSACTION = 'Login';&lt;BR /&gt;ELSE IF CODE IN ('405','409') THEN TRANSACTION = 'Transaction X';&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;ELSE TRANSACTION = 'Transaction Y';&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class="  language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jun 2019 12:30:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/568720#M160155</guid>
      <dc:creator>feliperczx</dc:creator>
      <dc:date>2019-06-25T12:30:40Z</dc:date>
    </item>
    <item>
      <title>Re: Data Set / IF-THEN statement between specific lines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/568944#M160255</link>
      <description>&lt;P&gt;If you want a dataset like the example you have provided it may require a two pass solution. First two identify the cycle boundaries and then apply the priority rules.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a possible solution below. You could possibly eliminate the derivation of the Transaction variable and substitute this logic into the datasteps. eg if Code='801' then CycleCount+1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
   input Time : hhmmss. Code : $3. Transaction &amp;amp; $20. ;
   format Time time8. ;
cards ;
7:00:30 801 Login
7:00:35 405 Transaction X
7:00:40 404 Transaction Y
7:02:30 801 Login 
7:02:35 404 Transaction Y
7:02:40 404 Transaction Y
7:03:60 801 Login
7:04:15 405 Transaction X
7:04:20 404 Transaction Y
7:05:01 405 Transaction X
7:06:03 409 Transaction X
run ;

proc sort data=have ;
   by Time ;
run ;

data have_cycle ;
   set have ;
   if Transaction='Login' then CycleCount+1 ;
run ;

data want ;
   set have_cycle;
   by CycleCount ;

   retain _XFound _YFound ;
   length Cycle $8 ;

   if first.CycleCount then do ;
       _XFound=0 ;
       _YFound=0 ;
   end ;

   if       Transaction='Transaction X' then _XFound=1 ;      
   else if  Transaction='Transaction Y' then _YFound=1 ;

  if last.CycleCount then do ;
       if      _XFound then Cycle='X Cycle' ;   
       else if _YFound then Cycle='Y Cycle' ;
   end ;

   drop _: CycleCount ;
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>Tue, 25 Jun 2019 22:41:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/568944#M160255</guid>
      <dc:creator>seemiyah</dc:creator>
      <dc:date>2019-06-25T22:41:25Z</dc:date>
    </item>
    <item>
      <title>Re: Data Set / IF-THEN statement between specific lines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/568991#M160274</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279179"&gt;@feliperczx&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;The peculiarity of your task basically lies in the fact that you need to flag each "end-of-group" record whose end-of-group marker (Login) is stored in the next record. It means that some look-ahead method has to be deployed, which is why the fine solutions by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/157852"&gt;@seemiyah&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;require extra steps and rather involved logic. Methinks it's simpler to address the look-ahead need by using the shifted (offset) merge tactic, for example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                              
   input time: hhmmss. code: $3. trans: $1. ;                            
   format Time time8. ;                                                  
cards ;                                                                  
7:00:30 801 L                                                            
7:00:35 405 X                                                            
7:00:40 404 Y                                                            
7:02:30 801 L                                                            
7:02:35 404 Y                                                            
7:02:40 404 Y                                                            
7:03:60 801 L                                                            
7:04:15 405 X                                                            
7:04:20 404 Y                                                            
7:05:01 405 X                                                            
7:06:03 409 X                                                            
run ;                                                                    
                                                                         
data want (drop = _:) ;                                                  
  do until (_break) ;                                                    
    merge have have (firstobs=2 keep=trans rename=(trans=_t)) end = lr ;
    if trans = "X" then _X = 1 ;                                         
    _break = (_t = "L" or lr) ;                                         
    if _break then cycle = put (ifC (_X, "X", "Y"), $1.) ;               
    output ;                                                             
  end ;                                                                  
run ;                                                                    
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jun 2019 04:40:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/568991#M160274</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-06-26T04:40:28Z</dc:date>
    </item>
    <item>
      <title>Re: Data Set / IF-THEN statement between specific lines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/569067#M160310</link>
      <description>Perfect, dude! Thank you so much!</description>
      <pubDate>Wed, 26 Jun 2019 12:38:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/569067#M160310</guid>
      <dc:creator>feliperczx</dc:creator>
      <dc:date>2019-06-26T12:38:58Z</dc:date>
    </item>
    <item>
      <title>Re: Data Set / IF-THEN statement between specific lines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/569935#M160667</link>
      <description>hashman, how would you count the time between Logins? to determine the duration of the cycle in minutes</description>
      <pubDate>Fri, 28 Jun 2019 18:36:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/569935#M160667</guid>
      <dc:creator>feliperczx</dc:creator>
      <dc:date>2019-06-28T18:36:14Z</dc:date>
    </item>
    <item>
      <title>Re: Data Set / IF-THEN statement between specific lines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/569996#M160699</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279179"&gt;@feliperczx&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;One way would be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                            
   input time: hhmmss. code: $3. trans: $1. ;                                          
   format Time time8. ;                                                                
cards ;                                                                                
7:00:30 801 L                                                                          
7:00:35 405 X                                                                          
7:00:40 404 Y                                                                          
7:02:30 801 L                                                                          
7:02:35 404 Y                                                                          
7:02:40 404 Y                                                                          
7:03:60 801 L                                                                          
7:04:15 405 X                                                                          
7:04:20 404 Y                                                                          
7:05:01 405 X                                                                          
7:06:03 409 X                                                                          
run ;                                                                                  
                                                                                       
data want (drop = _:) ;                                                                
  do _q = 1 by 1 until (_break) ;                                                      
    merge have have (firstobs=2 keep=time trans rename=(time=_tm trans=_t)) end = lr ; 
    if trans = "X" then _X = 1 ;                                                       
    if _q = 1 then _tm1 = time ;                                                       
    _break = (_t = "L" or lr) ;                                                        
    if _break then do ;                                                                
      cycle = put (ifC (_X, "X", "Y"), $1.) ;                                          
      if lr then cycle_min = divide (time - _tm1, 60) ;                                
      else       cycle_min = divide (_tm  - _tm1, 60) ;                                
    end ;                                                                              
    output ;                                                                           
  end ;                                                                                
run ;                                                                                  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It assumes that for the last record, you want the difference between the time on this record and the time on the preceding record where trans="L". However, truth be told, I'd rather set cycle_min for the last record to a special missing value (for example, .U) to indicate that since for the last record the beginning of the next cycle is unknown, the cycle time is also undermined. If so, just replace the IF LR condition with:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;      if lr then cycle_min = .U ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jun 2019 21:10:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Set-IF-THEN-statement-between-specific-lines/m-p/569996#M160699</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-06-28T21:10:35Z</dc:date>
    </item>
  </channel>
</rss>

