Announcement

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

  • Reshape Long to Wide to create dummy variables

    I currently have data that looks like this:

    a X 0
    a Y 0
    a Y 1
    b X 0
    b Y 0
    b Z 1
    c X 1
    c Y 1
    c Z 0

    Where abc is the individual, XYZ is the method, and 01 is whether that individual uses that method.
    How do I reshape my data so that I have dummy variables for each method that take the value 1 if that individual uses that method?

    Thank You

  • #2
    Your last variable appears to be exactly what you're asking for.

    That is, your example says to me that you have a variable "uses method" which already is (0, 1).

    What isn't clear is whether your example is to be taken concretely, as you first three observations are for the same individual and some method yet you have a mix of 0 and 1 values.

    In mathematics, abstraction removes irrelevant content and exposes structure. In statistics, abstraction removes vivid detail and often does not help.

    There are many good reasons why we ask for real data examples using dataex (FAQ Advice #12).

    Comment


    • #3
      Just as a side note: I wonder whether there is a good reason to reshape from long to wide, since most estimations of this sort are usually to be done under the long form.
      Best regards,

      Marcos

      Comment


      • #4
        I think I understand what you want. It was rendered confusing by what I assume was your typographical error on the third observation for id "a" which should have been for method "Z" rather than a second observation for method "Y". If that is correct - if each combination of id and method appears at most once in your data - the following code - which presents your example data in a useable form with the typographical error corrected - may point you in a useful direction.
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str1(id method) byte use
        "a" "X" 0
        "a" "Y" 0
        "a" "Z" 1
        "b" "X" 0
        "b" "Y" 0
        "b" "Z" 1
        "c" "X" 1
        "c" "Y" 1
        "c" "Z" 0
        end
        reshape wide use, i(id) j(method) string
        list, clean
        Code:
        . list, clean
        
               id   useX   useY   useZ  
          1.    a      0      0      1  
          2.    b      0      0      1  
          3.    c      1      1      0
        Having sorted out both of your posts today, and now seeing that you are a new member, let me add some advice on how to make more effective use of Statalist.

        Take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

        The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

        Comment

        Working...
        X