Announcement

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

  • Creating a time-varying variable with Panel data

    Hi - I am hoping someone will be able to help me with panel data! I am trying to create a time-varying variable (i.e., time between assessment points).
    I am able to generate 0 for assessment #1 using: replace Time between assessment = 0 if Assessment#==1, but am not sure how to get Stata to calculate the difference in ages (the cells in bold & italics).

    ID Score Assessment # Age (months) Time between assessment
    1 20 1 18 0
    2 30 1 20 0
    2 35 2 26 *Should be 6 (26-20 mos.)
    3 15 1 24 0
    4 7 1 30 0
    5 10 1 26 0
    5 16 2 30 *Should be 4 (30-26 mos.)
    5 18 3 37 *Should be 7 (37-30 mos.)
    6 40 1 21 0
    Thanks in advance for your suggestions!

  • #2
    You can try
    Code:
    bys ID: gen dif = Age - Age[_n-1] if Assessment !=Assessment[_n-1]
    I do not think the # string in the assessment variable is possible
    Last edited by Attaullah Shah; 07 Feb 2017, 12:45.
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #3
      Thanks for the data table. It is usable but an example created with dataex (SSC) would need less engineering.

      See http://www.statalist.org/forums/help#stata for more guidance.

      Code:
      clear
      input ID    Score    Assessment Age Wanted 
      1    20    1    18    0    
      2    30    1    20    0    
      2    35    2    26    6 
      3    15    1    24    0    
      4    7    1    30    0    
      5    10    1    26    0    
      5    16    2    30    4 
      5    18    3    37    7 
      6    40    1    21    0
      end 
      
      bysort ID (Age) : gen Suggested = cond(Assess != Assess[_n-1] & _n > 1, Age - Age[_n-1], 0) 
      
      assert Wanted == Suggested 
      
      list, sepby(ID) 
      
           +-------------------------------------------------+
           | ID   Score   Assess~t   Age   Wanted   Sugges~d |
           |-------------------------------------------------|
        1. |  1      20          1    18        0          0 |
           |-------------------------------------------------|
        2. |  2      30          1    20        0          0 |
        3. |  2      35          2    26        6          6 |
           |-------------------------------------------------|
        4. |  3      15          1    24        0          0 |
           |-------------------------------------------------|
        5. |  4       7          1    30        0          0 |
           |-------------------------------------------------|
        6. |  5      10          1    26        0          0 |
        7. |  5      16          2    30        4          4 |
        8. |  5      18          3    37        7          7 |
           |-------------------------------------------------|
        9. |  6      40          1    21        0          0 |
           +-------------------------------------------------+

      Comment


      • #4
        Thank you both so much. I appreciate the guidance!

        Comment

        Working...
        X