Announcement

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

  • reshape

    Hello,

    I'm reshaping my data from long to wide as follows:

    Code:
    reshape wide x, i(id) j(yearMonth)
    The names of the j variables do not come as x_2001m1 x_2001m2, and so on. Ho can I get the names in this format? An example dataset:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year byte month float(x id temp)
    2000  1  -1.4952 1 480
    2000  2  -1.5653 1 481
    2000  3    -1.53 1 482
    2000  4  -1.5038 1 483
    2000  5  -1.5184 1 484
    2000  6  -1.8732 1 485
    2000  7  -.53273 1 486
    2000  8  -.33476 1 487
    2000  9  -.38694 1 488
    2000 10  -.60296 1 489
    2000 11  -.61594 1 490
    2000 12  -.61234 1 491
    2001  1  -.59847 1 492
    2001  2    -.644 1 493
    2001  3  -.61931 1 494
    2001  4  -.60525 1 495
    2001  5  -.63979 1 496
    2001  6 -.024683 1 497
    2001  7  -.40757 1 498
    2001  8  -.30469 1 499
    2001  9  -.30957 1 500
    2001 10  -.27522 1 501
    2001 11    -.284 1 502
    2001 12  -.25536 1 503
    2002  1  -.24324 2 504
    2002  2  -.22428 2 505
    2002  3  -.26123 2 506
    2002  4  -.27012 2 507
    2002  5   -.3093 2 508
    2002  6 -.060188 2 509
    2002  7  -1.2304 2 510
    2002  8  -1.3403 2 511
    2002  9  -1.1992 2 512
    2002 10  -1.2654 2 513
    2002 11  -1.2676 2 514
    2002 12  -1.2278 2 515
    2003  1  -1.2483 2 516
    2003  2  -1.2374 2 517
    2003  3  -1.2214 2 518
    2003  4   -1.205 2 519
    2003  5  -1.2098 2 520
    2003  6  -1.7511 2 521
    2003  7   .12341 2 522
    2003  8   .73705 2 523
    2003  9   .75044 2 524
    2003 10   .77955 2 525
    2003 11   .79881 2 526
    2003 12   .81725 2 527
    2004  1   .83258 3 528
    2004  2   .81351 3 529
    2004  3   .74627 3 530
    2004  4   .76489 3 531
    2004  5   .84338 3 532
    2004  6   .95598 3 533
    2004  7  -.20344 3 534
    2004  8  -.12863 3 535
    2004  9  -.16902 3 536
    2004 10  -.10092 3 537
    2004 11  -.11268 3 538
    2004 12  -.10501 3 539
    end
    format %tm temp
    Thank you.

  • #2
    It's unclear what is in your variable yearMonth as it's not part of the data example -- or indeed why you would want to do this. Long layout is preferable for almost all Stata purposes.

    This works with your data example.

    Code:
    gen index = "_" + strofreal(ym(year, month), "%tm")
    
    drop year month temp 
    
    reshape wide x, i(id) j(index) string

    Comment


    • #3
      Thank you, Nick. And sorry, I forgot to rename temp as yearMonth.

      Comment


      • #4
        I see. Your code would just produce variables like x480 x481 ... or so I imagine. reshape doesn't know that you want the display format to be used and there's no way to tell it that directly. Indeed, as you want _ and m to be part of the variable name, you need the string option as well as a variable constructed for the purpose.

        Comment

        Working...
        X