Announcement

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

  • keeping values of a variable based on the values of another variable

    I have panel data. Each ID in different years(7 waves) has different English, math or science grades; in some waves, they haven't reported it and it's missing. Now I want to keep variable English_grade for each ID only in waves 6 or 7 and get rid of English grades in other waves(They can be reported as missing).
    I know the command is not correct but maybe I need sth like that( keep English_grade if Wave==6|7 else drop English_grade). Would you please recommend me a code to solve it?
    ID Wave English_grade Math_grade Sceinec_grade
    1 1 75
    1 2 .
    1 3 .
    1 4 84
    1 5 .
    1 6 .
    1 7 88
    2 1 .
    2 2 .
    2 3 55
    2 4 70
    2 5 .
    2 6 69
    2 7 .
    Thanks,
    Mahdi

  • #2
    Maahdi:
    you may want to consider:
    Code:
    . bysort ID: replace English_grade=. if Wave<6
    (4 real changes made, 4 to missing)
    
    . list
    
         +--------------------------------------------+
         | ID   Wave   Englis~e   Math_g~e   Sceine~e |
         |--------------------------------------------|
      1. |  1      1          .          .          . |
      2. |  1      2          .          .          . |
      3. |  1      3          .          .          . |
      4. |  1      4          .          .          . |
      5. |  1      5          .          .          . |
         |--------------------------------------------|
      6. |  1      6          .          .          . |
      7. |  1      7         88          .          . |
      8. |  2      1          .          .          . |
      9. |  2      2          .          .          . |
     10. |  2      3          .          .          . |
         |--------------------------------------------|
     11. |  2      4          .          .          . |
     12. |  2      5          .          .          . |
     13. |  2      6         69          .          . |
     14. |  2      7          .          .          . |
         +--------------------------------------------+
    
    .
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      keep and drop work on entire observations or variables, so as you say can't be used for your purpose.

      What you're asking for is

      Code:
      replace english_grade = . if Wave < 6
      Also

      Code:
      if Wave==6|7
      is never a way to say

      Code:
       if Wave==6|Wave == 7
      as it means

      Code:
      if (Wave==6)|7
      which is not at all what you want.

      Comment


      • #4
        Thank you Carlo and Nick for the support!

        Comment

        Working...
        X