Announcement

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

  • Trying to reshape data from wide to long: Stata writes that a variable doesn't exist while it does

    I have dozens of different stubs each with four different suffix denoting the round of the survey my variables are from. Kindly examine this example of my data:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long id byte(COVa20m_r1 COVa20m_r2 COVb3a_r1 COVb3a_r2)
    652001 6 . 100 .
    652341 . .   . .
    652240 6 . 100 .
    652037 6 . 100 .
    652243 . .   . .
    652320 6 . 100 .
    652204 6 . 100 .
    652157 6 . 100 .
    652163 6 . 100 .
    652351 6 . 100 .
    end
    label values COVa20m_r1 COVA20M
    label values COVa20m_r2 COVA20M
    label def COVA20M 6 "June", modify
    label values COVb3a_r1 COVB3A
    label values COVb3a_r2 COVB3A
    You can see that in this specific example, suffixes are _r1 and _r2 and that the stubs are COVa20m and COVb3a. When running the code:

    Code:
    forvalues x = 1/4 {
        unab vars_r`x': *_r`x'
        local stubs_r`x': subinstr local vars_r`x' "_r`x'" "", all
    }
    
    reshape long `stubs_r1' `stubs_r2' `stubs_r3' `stubs_r4', i(id)  j(round_number _r1 _r2 _r3 _r4) s
    I get the following output:

    variable COVa20m_r1 not found

    Which is quite puzzling given that i am perfectly able to look for this variable in my list. I can even use tab COVa20m_r1.

    What did I do wrong?

    Thank you for your help
    Julia

  • #2
    Each local holds the same set of stubnames, you need just one. So it is the repetition of stubnames that is causing the error. You can do the following:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long id byte(COVa20m_r1 COVa20m_r2 COVb3a_r1 COVb3a_r2)
    652001 6 . 100 .
    652341 . .   . .
    652240 6 . 100 .
    652037 6 . 100 .
    652243 . .   . .
    652320 6 . 100 .
    652204 6 . 100 .
    652157 6 . 100 .
    652163 6 . 100 .
    652351 6 . 100 .
    end
    label values COVa20m_r1 COVA20M
    label values COVa20m_r2 COVA20M
    label def COVA20M 6 "June", modify
    label values COVb3a_r1 COVB3A
    label values COVb3a_r2 COVB3A
    
    forvalues x = 1/4 {
        unab vars_r`x': *_r`x'
        local stubs_r`x': subinstr local vars_r`x' "_r`x'" "", all
    }
    
    local stubnames "`stubs_r1' `stubs_r2' `stubs_r3' `stubs_r4'"
    local stubnames: list uniq stubnames
    
    reshape long `stubnames', i(id)  j(round_number) s
    Res.:

    Code:
    . l, sep(0)
    
         +--------------------------------------+
         |     id   round_~r   COVa20m   COVb3a |
         |--------------------------------------|
      1. | 652001        _r1      June      100 |
      2. | 652001        _r2         .        . |
      3. | 652037        _r1      June      100 |
      4. | 652037        _r2         .        . |
      5. | 652157        _r1      June      100 |
      6. | 652157        _r2         .        . |
      7. | 652163        _r1      June      100 |
      8. | 652163        _r2         .        . |
      9. | 652204        _r1      June      100 |
     10. | 652204        _r2         .        . |
     11. | 652240        _r1      June      100 |
     12. | 652240        _r2         .        . |
     13. | 652243        _r1         .        . |
     14. | 652243        _r2         .        . |
     15. | 652320        _r1      June      100 |
     16. | 652320        _r2         .        . |
     17. | 652341        _r1         .        . |
     18. | 652341        _r2         .        . |
     19. | 652351        _r1      June      100 |
     20. | 652351        _r2         .        . |
         +--------------------------------------+
    Last edited by Andrew Musau; 05 Sep 2022, 07:52.

    Comment


    • #3
      Dear Andrew:

      A thousand thanks for your help. It worked!

      Comment

      Working...
      X