Announcement

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

  • invalid variable name

    Hi! I am trying to reshape my data.

    id Co base ID3
    103 0 0 103_6_6
    104 0 1 104_0_0
    104 0 0 104_1_1
    104 0 0 104_3_3
    104 0 0 104_6_6
    104 1 0 104_12_12
    106 1 0 106_1_1
    106 1 0 106_3_3
    106 1 0 106_6_6
    106 1 0 106_12_12
    106 1 0 106_99_18
    106 1 0 106_24_24
    106 1 0 106_36_36
    107 0 1 107_0_-1
    107 0 0 107_99_4
    108 0 1 108_0_0
    108 0 0 108_1_1

    Here is my code:
    reshape wide Co base, i(id) j(ID3) string

    But I got this error message:
    Co101_0_-1 invalid variable name

    Why is it invalid?
    Anyway, I drop this observation and run the code again. But I got an error message again:
    Co107_0_-1 invalid variable name

    Why is it invalid?
    How to detect all the possible invalid names?
    How can I reshape my data?
    Thank you very much!

  • #2
    Minus signs aren't allowed in variable names. Here's a work-around:

    Code:
    clear 
    input id Co base str9 ID3
    103 0 0 103_6_6
    104 0 1 104_0_0
    104 0 0 104_1_1
    104 0 0 104_3_3
    104 0 0 104_6_6
    104 1 0 104_12_12
    106 1 0 106_1_1
    106 1 0 106_3_3
    106 1 0 106_6_6
    106 1 0 106_12_12
    106 1 0 106_99_18
    106 1 0 106_24_24
    106 1 0 106_36_36
    107 0 1 107_0_-1
    107 0 0 107_99_4
    108 0 1 108_0_0
    108 0 0 108_1_1
    end 
    
    replace ID3 = subinstr(ID3, "-", "M", .) 
    
    reshape wide Co base, i(id) j(ID3) string
    
    foreach v of var *_*_* { 
        local label : var label `v'
        local label = subinstr("`label'", "M", "-", .)
        label var `v' "`label'"
    } 
        
    describe

    Comment


    • #3
      Thank you so much! Indeed! It works now!

      Comment

      Working...
      X