Announcement

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

  • data management/manipulation question

    Hello,

    In my dataset I have sets of variables that belong to different instruments. Currently, each observation contains data from each variable. However, I want each observation to contain only data from one instrument. Thus, for n instruments and m observations, I need nm observations in my new dataset. To illustrate:

    I have:
    obs id var1 var2 var3 var4
    1 1 red blue dog cat
    2 2 yellow green mouse bird
    I want:
    obs id instrument var1 var2 var3 var4
    1 1 colors red blue
    2 1 animals dog cat
    3 2 colors yellow green
    4 2 animals mouse bird
    Whats a nice way to go about this?

    Thanks so much,
    Reese

  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(obs id) str6 var1 str5(var2 var3) str4 var4
    1 1 "red"    "blue"  "dog"   "cat" 
    2 2 "yellow" "green" "mouse" "bird"
    end
    
    reshape long var, i(id) j(which)
    gen which2= inlist(which, 1, 2)
    egen id2= group(id which2)
    reshape wide var, i(id2) j(which)
    Res.:

    Code:
    . l
    
         +---------------------------------------------------------+
         | id2     var1    var2    var3   var4   id   obs   which2 |
         |---------------------------------------------------------|
      1. |   1                      dog    cat    1     1        0 |
      2. |   2      red    blue                   1     1        1 |
      3. |   3                    mouse   bird    2     2        0 |
      4. |   4   yellow   green                   2     2        1 |
         +---------------------------------------------------------+

    Comment


    • #3
      Beautiful code Andrew. Thank you!

      Comment

      Working...
      X