Announcement

Announcement Module
Collapse
No announcement yet.
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Data manipulation question

    Hey,

    I have a small question with regard to some data I am analyzing.
    They are in the form of:
    x y
    0,2 4 6 7
    0,3 2 5 6 9 10
    0,4 3 2 12 4
    So for x = 0,2, I have 4 observations for y: 4,6 and 7.
    I would like to get them like this:
    x y
    0,2 4
    0,2 6
    0,2 7
    0,3 2
    0,3 5
    0,3 6
    .. ..
    Is there a easy way to do this in Stata?

    Thanks,

    Willem

  • #2
    Yes, Stata excels at this! See the -reshape- command.

    Comment


    • #3
      Lets assume the other variables (in Stata each "column" represents a variable) are called y2, y3 etc., you could use stack:

      stack x y x y2 x y3 x y4 ... , into (x y)

      sort x
      And then, to get rid of the cases where y# was missing:

      drop if y==.

      This assumes that it is feasible to list all variables by hand in the stack command; if there's too many, I guess the varlist for the stack command can be created by a loop.

      Comment


      • #4

        Originally posted by David Poensgen View Post
        Lets assume the other variables (in Stata each "column" represents a variable) are called y2, y3 etc., you could use stack:

        stack x y x y2 x y3 x y4 ... , into (x y)

        sort x
        And then, to get rid of the cases where y# was missing:

        drop if y==.

        This assumes that it is feasible to list all variables by hand in the stack command; if there's too many, I guess the varlist for the stack command can be created by a loop.
        I have max 30 "y-variables", so this should be feasible.

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          Yes, Stata excels at this! See the -reshape- command.

          I'll have a look at this first and see whether this is easier than the solution proposed above.

          Thanks for the feedback, both!

          Comment

          Working...
          X