Announcement

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

  • Data manipulation!

    Dear All, I found this question here (http://bbs.pinggu.org/forum.php?mod=...=1#pid51949480). Suppose that the data is
    Code:
    clear
    input str8 name str4 year str12 gender str10 origins
    "A,B,C" "2015" "M,F,M" "AL,CA,FL"
    "D,E" "2016" "M,F" "HI,IL"
    end
    and wish to re-formulate the data to be
    Code:
    clear
    input str8 name str4 year str12 gender str10 origins
    "A" "2015" "M" "AL"
    "B" "2015" "F" "CA"
    "C" "2015" "M" "FL"
    "D" "2016" "M" "HI"
    "E" "2016" "F" "HI"
    end
    Any suggestion is highly appreciated.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    Code:
    clear
    input str8 name str4 year str12 gender str10 origins
    "A,B,C" "2015" "M,F,M" "AL,CA,FL"
    "D,E" "2016" "M,F" "HI,IL"
    end
    
    split name, p(,) 
    split gender, p(,) 
    split origins, p(,) 
    gen long id = _n
    drop name origins gender 
    
    reshape long name origins gender , i(id) j(whatever) 
    drop if missing(name) & missing(gender) & missing(origins) 
    list, sepby(id) 
    
         +------------------------------------------------+
         | id   whatever   year   name   gender   origins |
         |------------------------------------------------|
      1. |  1          1   2015      A        M        AL |
      2. |  1          2   2015      B        F        CA |
      3. |  1          3   2015      C        M        FL |
         |------------------------------------------------|
      4. |  2          1   2016      D        M        HI |
      5. |  2          2   2016      E        F        IL |
         +------------------------------------------------+

    Comment


    • #3
      Dear Nick, Many thanks for the answer.
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment

      Working...
      X