Announcement

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

  • Generating sequence of observations.

    Hi, I have the "have" variable and based on that "have" variable, I want to generate the two variables, i.e., "want1" & "want2".

    Please see the example below.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double(id have want1 want2)
    1 0 5 0
    1 0 4 0
    1 0 3 0
    1 0 2 0
    1 1 1 1
    1 0 0 2
    1 0 0 3
    1 0 0 4
    1 0 0 5
    1 0 0 6
    2 0 3 0
    2 0 2 0
    2 1 1 1
    2 0 0 2
    2 0 0 3
    2 0 0 4
    2 0 0 5
    2 0 0 6
    end
    the two "want" variables are basically sequence of numbers that are based on value of "have" variable.

    Thanks.

  • #2
    Thanks for the data example.

    For this to make sense, or at least to put into practice, you should have or at least do need a sequence variable too. That's because at some point for reversing time temporarily it helps to create a count down variable.

    I am interpreting this as implying that an event occurs at most once for each individual.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double(id have want1 want2)
    1 0 5 0
    1 0 4 0
    1 0 3 0
    1 0 2 0
    1 1 1 1
    1 0 0 2
    1 0 0 3
    1 0 0 4
    1 0 0 5
    1 0 0 6
    2 0 3 0
    2 0 2 0
    2 1 1 1
    2 0 0 2
    2 0 0 3
    2 0 0 4
    2 0 0 5
    2 0 0 6
    end
    
    gen time = 1 if id != id[_n-1]
    replace time = time[_n-1] + 1 if missing(time)
    
    bysort id (time): gen wanted2 = sum(have == 1) == 1
    by id: replace wanted2 = wanted2[_n-1] + 1 if wanted2
    
    gen negtime = -time
    
    bysort id (negtime): gen wanted1 = sum(have == 1) == 1
    by id: replace wanted1 = wanted1[_n-1]  + 1 if wanted1
    
    sort id time
    
    list id time want1 wanted1 want2 wanted2, sepby(id)
         +-----------------------------------------------+
         | id   time   want1   wanted1   want2   wanted2 |
         |-----------------------------------------------|
      1. |  1      1       5         5       0         0 |
      2. |  1      2       4         4       0         0 |
      3. |  1      3       3         3       0         0 |
      4. |  1      4       2         2       0         0 |
      5. |  1      5       1         1       1         1 |
      6. |  1      6       0         0       2         2 |
      7. |  1      7       0         0       3         3 |
      8. |  1      8       0         0       4         4 |
      9. |  1      9       0         0       5         5 |
     10. |  1     10       0         0       6         6 |
         |-----------------------------------------------|
     11. |  2      1       3         3       0         0 |
     12. |  2      2       2         2       0         0 |
     13. |  2      3       1         1       1         1 |
     14. |  2      4       0         0       2         2 |
     15. |  2      5       0         0       3         3 |
     16. |  2      6       0         0       4         4 |
     17. |  2      7       0         0       5         5 |
     18. |  2      8       0         0       6         6 |
         +-----------------------------------------------+
    There is no obvious objection to a tsset here but you would need to change that when reversing time.
    Last edited by Nick Cox; 30 Jul 2021, 06:37.

    Comment


    • #3
      Hi Nick,

      Thanks for the prompt and perfect reply. Worked like a cham. I do have a sequence variable, however, I forgot to mention it here.

      Once again, thanks.

      Comment

      Working...
      X