Announcement

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

  • I need some help in reshaping the data

    Hi,
    I am currently have the data in the following shape.
    id fyear cik
    1 2003 37996
    1 2003 924613
    1 2004 924613
    1 2004 1306965
    1 2005 37996
    1 2005 1306965
    2 2003 9856112
    2 2003 14896
    2 2004 14896
    I want to convert the data into the shape below (id & fyear can uniquely identify an obs)
    id fyear cik1 cik2 cik3
    1 2003 37996 924613
    1 2004 924613 1306965
    1 2005 37996 1306965
    2 2003 9856112 14896
    2 2004 14896
    I was trying to use
    reshape wide cik, i(id fyear) j(cik) => doesn't work
    or
    reshape wide cik, i(id) j(fyear) => id and fyear cannot uniquely identify an obs => failed

    How can I convert it?
    Thank you!

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte id int fyear long cik
    1 2003   37996
    1 2003  924613
    1 2004  924613
    1 2004 1306965
    1 2005   37996
    1 2005 1306965
    2 2003 9856112
    2 2003   14896
    2 2004   14896
    end
    
    gen id2 = 1 if id != id[_n-1] | fyear != fyear[_n-1]
    replace id2 = id2[_n-1] + 1 if missing(id2)
    
    reshape wide cik, i(id fyear) j(id2)
    
    list 
    
         +--------------------------------+
         | id   fyear      cik1      cik2 |
         |--------------------------------|
      1. |  1    2003     37996    924613 |
      2. |  1    2004    924613   1306965 |
      3. |  1    2005     37996   1306965 |
      4. |  2    2003   9856112     14896 |
      5. |  2    2004     14896         . |
         +--------------------------------+

    Comment


    • #3
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte id int fyear long cik
      1 2003   37996
      1 2003  924613
      1 2004  924613
      1 2004 1306965
      1 2005   37996
      1 2005 1306965
      2 2003 9856112
      2 2003   14896
      2 2004   14896
      end
      
      gen seq=_n
      bys id fyear (seq): replace seq=_n
      reshape wide cik, i(id fyear) j(seq)

      Res.:

      Code:
      . l
      
           +--------------------------------+
           | id   fyear      cik1      cik2 |
           |--------------------------------|
        1. |  1    2003     37996    924613 |
        2. |  1    2004    924613   1.3e+06 |
        3. |  1    2005     37996   1.3e+06 |
        4. |  2    2003   9.9e+06     14896 |
        5. |  2    2004     14896         . |
           +--------------------------------+

      Comment


      • #4
        Thank you guys! The codes work very well! Thank you so much!

        Comment

        Working...
        X