Announcement

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

  • How do I do two j for reshape command? Use reshape twice?

    Hello,
    I have a wide data set that contains variables:
    PUBID (individuals ID)
    startdate__njob_year

    Ideally, I would want to have two j (year Njob)
    so that the data could look like:
    Click image for larger version

Name:	IMG_3685.JPG
Views:	1
Size:	627.1 KB
ID:	1534613

    what should I do? My professor suggests that I could do reshape twice.
    Thanks in advance!

  • #2
    Well, you can do this with two -reshape-s, but you can also do it in one fell swoop given that the variable names are so completely regular:

    Code:
    clear*
    //  GENERATE A TOY DATA SET TO ILLUSTRATE THE CODE
    set obs 100
    set seed 1234
    gen int id = _n
    
    forvalues year = 1997/2000 {
        forvalues njob = 1/3 {
            gen int startdate_`njob'_`year' = runiformint(1, 365)
        }
    }
    
    //  SHOW HOW TO DO THE TWO RESHAPES IN ONE STEP
    reshape long startdate_, i(id) j(njob_year) string
    split njob_year, gen(j) destring parse("_")
    rename j1 njob
    rename j2 year
    rename startdate_ startdate
    In the future, when asking for advice on code, always show example data, and always use the -dataex- command to do that.
    If you are running version 16 or a fully updated version 15.1 or 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-

    Comment


    • #3
      Thank you for mentioning the -dataex- command Clyde!
      I'm pretty new to this forum but will use that next time. Could I ask you how do you type the code so nicely on here?

      **Also I've read your answers to other questions! They are very helpful! Thank you!
      Last edited by Sierra Gu; 02 Feb 2020, 02:43.

      Comment


      • #4
        See https://www.statalist.org/forums/help#stata and more generally in the FAQ Advice.

        Comment

        Working...
        X