Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • looping over observations - retaining the values from previous observations

    Hello all
    I am trying to find out the length of wait for each individual for a service given we know the average length of wait in a queue. I hope the example data will make it clear what I am trying to find out.
    Code:
    . version
    version 13.1
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(start_time los_in_queue end_time)
    4 2 .
    . 5 .
    . 6 .
    . 7 .
    . 8 .
    end

    I want the data to look like:
    obs. start_time los_in_queue end_time
    1 4 2 6
    2 6 5 11
    3 11 6 17
    4 17 7 24
    5 24 8 32

    Basically, I want to retain the end_time of the first observation as a start_time for the second observation an so on.

    My attempt overrides all previous values by the last value. Thanks in advance for pointing out where I went wrong.
    Code:
    . local N = _N
    . local counter=1
    . forvalues i = 1/`N' {
      2.      replace end_time = start_time[`i']+los_in_queue[`i']
      3.                 list start_time-end_time
      4.                 local i=`i'+1
      5.                 replace start_time = end_time[`i'-`counter']
      6.                 }
    (5 real changes made)
    
    . clist
    
              start_time     los_in_queue         end_time
      1.              32                2               32
      2.              32                5               32
      3.              32                6               32
      4.              32                7               32
      5.              32                8               32

  • #2
    Code:
    forvalues i = 1/`=_N' {
        replace end_time = start_time + los_in_queue in `i'
        if `i' < _N {
            replace start_time = end_time[`i'] in `=`i'+1'
        }
    }

    Comment


    • #3
      While Clyde has shown you the correct construction of your loop, I'll show you that a loop is unnecessary.
      Code:
      . replace end_time = sum(los_in_queue)+start_time[1]
      (5 real changes made)
      
      . replace start_time = end_time[_n-1] if _n>1
      (4 real changes made)
      
      . list, clean abbreviate(12)
      
             start_time   los_in_queue   end_time  
        1.            4              2          6  
        2.            6              5         11  
        3.           11              6         17  
        4.           17              7         24  
        5.           24              8         32

      Comment


      • #4
        Thanks Clyde and William for the solutions.

        Comment


        • #5
          Or like this.

          Code:
          . gen end = los
          
          . replace end = end + end[_n-1] in 2/l
          (4 real changes made)
          
          . replace end = end + start[1]
          (5 real changes made)
          
          . replace start = start[_n-1] + los[_n-1] in 2/l
          (4 real changes made)
          
          . list, clean
          
                 start_~e   los_in~e   end_time   end  
            1.          4          2          .     6  
            2.          6          5          .    11  
            3.         11          6          .    17  
            4.         17          7          .    24  
            5.         24          8          .    32

          Comment

          Working...
          X