Announcement

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

  • Keeping the position of my variables after a -reshape long-

    Hello,

    My data before reshaping :

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str10(varid vartime varA1 varA2) byte obs_num
    "identifyer" "time" "email" "phone numb" 1
    end
    After this command : reshape long var, i(obs_num) j(label) string

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte obs_num str4 label str10 var
    1 "A1"   "email"     
    1 "A2"   "phone numb"
    1 "id"   "identifyer"
    1 "time" "time"      
    end
    Please, how can I make sure that the horizontal order from the first dataex example is kept vertically after the reshape ? i.e. it should be id, time, A1, A2 and not an alphabetical order. I tried before the reshape:

    Code:
    rename * var#*, addnumber
    But it's not a sequential order since I have string variables, so it's going to be 1, 10, 11, ... 2, 20...

    Can someone help me please?

  • #2
    This should be a start in a useful direction.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str10(varid vartime varA1 varA2) byte obs_num
    "identifyer" "time" "email" "phone numb" 1
    "dog"        "cat"  "bird"  "fish"       2
    end
    local i 0
    foreach v of varlist varid vartime varA1 varA2 {
        local sfx = substr("`v'",4,.)
        generate seq`sfx' = `++i'
    }
    reshape long var seq, i(obs_num) j(label) string
    order obs_num seq, last
    sort obs_num seq
    list, clean
    Code:
    . list, clean
    
           label          var   obs_num   seq  
      1.      id   identifyer         1     1  
      2.    time         time         1     2  
      3.      A1        email         1     3  
      4.      A2   phone numb         1     4  
      5.      id          dog         2     1  
      6.    time          cat         2     2  
      7.      A1         bird         2     3  
      8.      A2         fish         2     4

    Comment

    Working...
    X