Announcement

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

  • Reshape

    Hello

    I would appreciate if someone can tell me the command that would reshape my data from
    2012 2013 2014 2015
    1
    2
    3
    4
    5
    to something like
    1 2012
    1 2013
    1 2014
    1 2015
    2 2012
    2 2013
    where the empty cells would just contain the data. As you can see, I basically want to go from the first shape to a panel data shape.
    I would appreciate any input !

    Regards,
    Last edited by Yousef Kaddoura; 16 Feb 2019, 18:22. Reason: Reshape

  • #2
    can you offer some subsamples if it is possible using dataex
    Here is an example for your reference:
    Code:
    clear all
    set obs 5
    gen id=_n
    forvalues i=2012/2015{
    gen v`i'=uniform()
    }
    reshape long v,i(id) j(year)
    Last edited by Liu Qiang; 16 Feb 2019, 18:44.
    2B or not 2B, that's a question!

    Comment


    • #3

      I am not sure what dataex means. but i'll try to elaborate more.

      Click image for larger version

Name:	pic 1.JPG
Views:	1
Size:	39.0 KB
ID:	1484091


      I want this kinda of data to be reshaped into a panel setting. so each i.d. should probably be duplicated 4 times or how according to how many years i have. Again, to a panel setting.

      Comment


      • #4
        What you are showing is not a possible Stata data set. Neither the empty string in the first column of your tableau nor the numbers 2012 through 2015 are legal variable names in Stata. It is pointless to ask for help coding a Stata data management problem when you don't even have a Stata data set in hand. Once you do have one, the -dataex- command will enable you to provide a useful sample of it in this Forum.

        If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

        When asking for help with code, always show example data. When showing example data, always use -dataex-.

        Please read the entire Forum FAQ for excellent advice on how to pose your questions in the most helpful and informative way, thereby maximizing your chance of getting timely and helpful responses. Pay particular attention to #12.

        Comment


        • #5
          HI Yousef, dataex is a Stata command created specifically to share data on Statalist (help dataex). If you're not familiar with dataex (and most Stata users aren't) I created a Youtube tutorial here. (I made it too long--feel free to watch at 2x speed, and you may only need the first 6 minutes) .

          You essentially are going to use reshape long. See two very similar posts here and here

          Also, as Clyde mentioned, Stata doesn't allow variables to begin with a number, so you will need to rename them to y2012, y2013, etc or sales2012, sales2013 (whatever they are) before you import them.

          Code:
          dataex firm_id sales2012 sales2013 sales2014 sales2015  //  data shared via  -dataex-. To install: ssc install dataex
          clear
          input byte firm_id int(sales2012 sales2013 sales2014 sales2015)
          1  8499  8651 11946 10055
          2  9208 12351 12438 10482
          3 10129  9588 11787  9830
          4  8517 11493  8847 11394
          end
          ------------------ copy up to and including the previous line ------------------
          
          format sales* %10.0fc   // just formatting to comma format
          reshape long sales, i(firm_id) j(year)  // help reshape
          * sales is the "stub" (ie sales2012 sales2013 sales2014)
          * Think of 2012, 2013, and 2014 as "tails".  j(year) is how you tell Stata what the "tail" represents (in this case years)
          
          . list, sepby(firm_id) noobs
          
            +-------------------------+
            | firm_id   year    sales |
            |-------------------------|
            |       1   2012    8,499 |
            |       1   2013    8,651 |
            |       1   2014   11,946 |
            |       1   2015   10,055 |
            |-------------------------|
            |       2   2012    9,208 |
            |       2   2013   12,351 |
            |       2   2014   12,438 |
            |       2   2015   10,482 |
            |-------------------------|
            |       3   2012   10,129 |
            |       3   2013    9,588 |
            |       3   2014   11,787 |
            |       3   2015    9,830 |
            |-------------------------|
            |       4   2012    8,517 |
            |       4   2013   11,493 |
            |       4   2014    8,847 |
            |       4   2015   11,394 |
            +-------------------------+
          Last edited by David Benson; 16 Feb 2019, 20:03.

          Comment

          Working...
          X