Announcement

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

  • Generate New Variable for Each Value of Another Variable

    Is there a command that would generate a new variable for each value of another variable? I've looked through the egen command manual but I'm not finding what I need.
    I've received a raw dataset that only has two variables, besides the identifier and date. One called "code" is the variable name, and one called "value" has the actual value for that variable.
    Note that there are repeated observations for each identifier, so I need a command that allows me to create a "long" file for each id and date combination, and then I may convert to wide format as needed.

    The structure currently looks something like this, with both string and numeric entries in values:
    id code value
    a var1 1
    b var1 2
    a var2 1
    b var2 4
    a var3 unknown
    b var3 5

    And I'm aiming for this:
    id var1 var2 var3
    a 1 1 unknown
    b 2 4 5

  • #2
    What you want is -- contrary to your statement -- a wide layout compared with a long layout. See

    Code:
    help reshape
    which gives the big picture. Here is some token code.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str1 id str4 code byte value
    "a" "var1" 1
    "b" "var1" 2
    "a" "var2" 1
    "b" "var2" 4
    "a" "var3" .
    "b" "var3" 5
    end
    . reshape wide value, i(id) j(code) string
    (j = var1 var2 var3)
    
    Data                               Long   ->   Wide
    -----------------------------------------------------------------------------
    Number of observations                6   ->   2          
    Number of variables                   3   ->   4          
    j variable (3 values)              code   ->   (dropped)
    xij variables:
                                      value   ->   valuevar1 valuevar2 valuevar3
    -----------------------------------------------------------------------------
    
    . rename (value*) (*)
    
    . l
    
         +-------------------------+
         | id   var1   var2   var3 |
         |-------------------------|
      1. |  a      1      1      . |
      2. |  b      2      4      5 |
         +-------------------------+
    .

    Comment


    • #3
      Very helpful, thank you!

      Comment

      Working...
      X