Announcement

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

  • long/wide - reshape confusion

    I have a data set that looks like (see below), where x1-x4 are observations for 4 different individuals. I want to stack these into a panel, including a date column, and a column with the specific individual identifier (in this case simply x1, x2, x3, x4). I know how to do this with "stack date x1 date x2 date x3 date x4" (but I don't know how to "simply" get the individual identifiers). Now, my "real" question is: suppose I have 20,000 individuals (with not nice identifiers, e.g., slkijs1, sliiily, 889098, 1234145....). Clearly, my stack example above would be exceedingly tedious. I end up having to do this a lot, so any suggestions would be MOST appreciated. I'm sure I've overlooked some very basic information, but... thanks.
    date x1 x2 x3 x4
    8-Jan-08 44.267 71.24366 7.064299 53.01147
    9-Jan-08 21.462 12.1015 46.61863 20.77969
    10-Jan-08 75.883 35.21949 2.601562 57.79749
    11-Jan-08 83.377 66.84127 25.44249 90.14868
    14-Jan-08 67.533 17.24804 67.00962 11.29083
    15-Jan-08 67.819 68.5128 14.26676 76.55122
    16-Jan-08 15.461 15.63645 95.98777 75.82653
    17-Jan-08 19.519 40.68843 19.62687 86.06653
    18-Jan-08 87.716 63.43431 81.71249 90.69047
    21-Jan-08 16.507 64.96755 40.14056 0.718889

  • #2
    You should be using the -reshape- command for this.

    Code:
    reshape long x, i(date) j(person_number)
    If the real variable names are not a simple pattern like x1, x2, ... etc. then you should rename them so they are. So, if every variable except date corresponds to data for an individual, you could do something like this:

    Code:
    ds date, not
    local individuals `r(varlist)'
    
    rename (`individuals') x_=
    reshape long x_, i(date) j(person) string
    -reshape- is one of the most basic data management commands in Stata. You would be well advised to acquaint yourself with it by reading the corresponding chapter in the [D] volume of the PDF manuals that come with your Stata installation. (Select PDF Documentation on the Help menu.) It takes a bit of practice to get used to figuring out what goes in i() and what goes in j(), but after enough tries, it will suddenly "click" in your brain and you will just automatically know what to do.

    Comment


    • #3
      The example can be easily reshaped with the aptly named "reshape" command. The not-so-nice identifiers might be more tricky, but "reshape ..., ... string" might get you there.

      Comment


      • #4
        Clyde and Jesse rightly point to the string option of reshape.

        As the identifiers are already part of variable names, there won't be a problem. They are going to end up in a string variable, and no rules will bite. You will need to map that string variable to a numeric variable to tsset or xtset, but in turn see e.g.

        https://www.stata.com/support/faqs/d...p-identifiers/

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          You should be using the -reshape- command for this.

          Code:
          reshape long x, i(date) j(person_number)
          If the real variable names are not a simple pattern like x1, x2, ... etc. then you should rename them so they are. So, if every variable except date corresponds to data for an individual, you could do something like this:

          Code:
          ds date, not
          local individuals `r(varlist)'
          
          rename (`individuals') x_=
          reshape long x_, i(date) j(person) string
          -reshape- is one of the most basic data management commands in Stata. You would be well advised to acquaint yourself with it by reading the corresponding chapter in the [D] volume of the PDF manuals that come with your Stata installation. (Select PDF Documentation on the Help menu.) It takes a bit of practice to get used to figuring out what goes in i() and what goes in j(), but after enough tries, it will suddenly "click" in your brain and you will just automatically know what to do.
          Thanks, the -ds- command, which put all of my thousands of variable names into a neat variable list was the key, and I was unfamiliar with -ds-. On a side note, I haven't had the -reshape- epiphany yet despite repeated 'acquaintances' with the manuals. I find that most help examples are limited only to the very basic.

          Comment


          • #6
            Several suggestions can be found with search reshape. Here's a selection:

            Code:
            Book    . . . . . An Introduction to Stata for Health Researchers, 4th Edition
                    . . . . . . . . . . . . . . . . . . . Svend Juul and Morten Frydenberg
                    http://www.stata.com/bookstore/introduction-stata-health-researchers/
            
            Book    . . . . . . . . . .  Data Management Using Stata: A Practical Handbook
                    . . . . . . . . . . . . . . . . . . . . . . . . .  Michael N. Mitchell
                    http://www.stata.com/bookstore/data-management-using-stata/
            
            Video   . Data management: How to reshape data from long format to wide format
                    4/17    http://www.youtube.com/watch?v=gkcYpw8CtCw
                            This video demonstrates how to reshape data from long
                            format to wide format.
            
            Video   . Data management: How to reshape data from wide format to long format
                    4/17    http://www.youtube.com/watch?v=Bx9kVdkr9oY
                            This video demonstrates how to reshape data from wide
                            format to long format.
            
            FAQ     . . . . . . . . . . . . . . . . . . . . . . . .  Problems with reshape
                    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                    2/14    I am having problems with the reshape command. Can
                            you give further guidance?
                            http://www.stata.com/support/faqs/data-management/
                            problems-with-reshape/
            
            Example . . . . . . . . . Stata learning module: Reshaping data:  long to wide
                    . . . . . . . . . . . . . . . . . .  UCLA Academic Technology Services
                    7/08    https://stats.idre.ucla.edu/stata/modules/reshaping-data-long-
                            to-wide/
            
            Example . . . . . . . . . Stata learning module:  Reshaping data: wide to long
                    . . . . . . . . . . . . . . . . . .  UCLA Academic Technology Services
                    7/08    https://stats.idre.ucla.edu/stata/modules/reshaping-data-wide-
                            to-long/

            Comment

            Working...
            X