Announcement

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

  • Generate separate columns in STATA

    Hello, everyone. I'm trying to generate new columns based on the value of another column. For example, my data has just ID and NUMBER and I want to generate 3 new columns based on ID in which NUM1 contains all the values of ID corresponding to the column NUMBERS including "1", NUM2 including "2" and NUM3 including "3".
    ID NUMBER NUM1 NUM2 NUM3
    123 165 123 . .
    124 28 . 124 .
    125 34 . . 125
    126 175 126 . .
    127 165 127 . .
    128 344 . . 128
    129 299 . 129 .
    130 289 . 130 .
    Any idea how to generate these new columns?

    Thank you.
    Last edited by Niloufar Eslah; 04 Apr 2023, 05:53.

  • #2
    Uhhhhhhhhhhh............ I'm not at my commuter, but make ID into a string and then just loop over it with this solution, unless someone else much better at strings than me has other thoughts.
    Code:
    forv i = 1/3 {
    
    g num`i' = substr(ID,1,`i')
    }

    Comment

    Working...
    X