Announcement

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

  • how to generate sequence integer starting from 0 (by group)

    Hi, I have a panel data set and try to generate sequence numbers by group but the numbers starting from 0 using a if condition.
    To be specific, I'll go straight to an example data set.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id time change) byte period float(nmissing desired_period)
    100 1 0 -1 2 -2
    100 2 0  0 2 -1
    100 3 1  1 2  0
    100 4 1  2 2  1
    200 1 0  0 1 -1
    200 2 1  1 1  0
    200 3 1  2 1  1
    200 4 1  3 1  2
    200 5 1  4 1  3
    300 2 0 -3 4 -4
    300 3 0 -2 4 -3
    300 4 0 -1 4 -2
    300 5 0  0 4 -1
    end
    This is a (unbalanced) panel data set.
    I want to generate desired_period variable which looks like that: when the value of change variable becomes from 0 to 1 for id 100 (which is time=3), I want the value of desired period variable to be 0. After that the value increases by 1 (e.g. 0,1,2,3,...). In a similar way, the values of desired period before time=3 go backward (-1, -2,-3,...).

    I was searching on the internet and found a code that gave me somewhat similar output but the only difference is the value doesn't start from 0 but from 1 (e.g. 1,2,3,...).
    The code I've found is below:
    Code:
    bys id, sort: egen period = seq() if change >= 1
    by id, sort: egen nmissing = total(missing(period))
    by id, sort: replace period = -nmissing + _n if missing(period)
    I searched how I can start the sequence integers from 0 but failed to find a good answer.
    I found that I can use option f() with seq() which I can specify the starting point.
    I tried something like this but had an error message, which I assume that options within seq() and if condition can't go together?
    Code:
    bys id, sort: egen desired_period= seq() f(0) if change >= 1
    bys id, sort: egen desired_period= seq(), f(0) if change >= 1
    I would appreciate if someone could help me fixing the code to get the 'desired_period' variable.
    Thank you.

  • #2
    Code:
    bys id (time): gen temp = time if change==1 & change[_n-1]==0
    bys id (time): egen temp2 = total(temp)
    gen wanted = time-temp2
    bys id (time): replace wanted = _n-_N if temp2==0
    With the last line needed only if your rules mean to include that ids without any observed change would be counted down form zero from the last observation, as in your example.

    Comment


    • #3
      Hi Jorrit,

      thank you for your code. I tried it and it worked very well.
      A slight modification I want to make for the last line of you code.

      Code:
      bys id (time): gen temp = time if change==1 & change[_n-1]==0
      bys id (time): egen temp2 = total(temp)
      gen wanted = time-temp2
      bys id (time): replace wanted = [_n-1]-_N if temp2==0
      Thank you!

      Comment

      Working...
      X