Announcement

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

  • Reshape from wide to long

    I have data in this format
    Code:
    date     comp1_asset    comp1_ profit    comp2_asset    comp2_profit
    1/2018    10000        2000        10500        18000
    2/2018    12000        2200        11000        19000
    The format in which I want is
    Code:
    date       comp    assets    profit
    1/2018    comp1    10000    2000
    2/2018    comp1    12000    2200
    1/2018    comp2    10500    18000
    2/2018    comp2    11000    19000
    Here is my example data set

    Code:
    clear
    input double(ALUMINIUMBAHRAIN ALUMINIUMBAHRAINMARKETVALUE) long ALUMINIUMBAHRAITOTALASSETS double ALUMINIUMBAHRAIBOOKVALUEPE long ALUMINIUMBAHRAICOMMONSHARES double(BAHRAINCARPARKCO BAHRAINCARPARKCOMARKETVA) int BAHRAINCARPARKCOTOTALASSET
    .5700000000000001             809.4 1229103 .587 1412731 .16 11.25 12905
    .5700000000000001             809.4 1229103 .587 1412731 .16 11.25 12905
                  .56 802.3000000000001 1229103 .587 1412731 .16 11.25 12905
                  .56 802.3000000000001 1229103 .587 1412731 .16 11.25 12905
                  .56 802.3000000000001 1229103 .587 1412731 .16 11.25 12905
                  .56 802.3000000000001 1229103 .587 1412731 .16 11.25 12905
    .5700000000000001             809.4 1229103 .587 1412731 .16 11.25 12905
    .5700000000000001             809.4 1229103 .587 1412731 .16 11.25 12905
    .5700000000000001             816.5 1229103 .587 1412731 .16 11.25 12905
                  .59             844.9 1229103 .587 1412731 .16 11.25 12905
                  .59             844.9 1229103 .587 1412731 .16 11.25 12905
    end

  • #2
    This is how far I got.
    Note your data, especially the variable names, are quite a mess. I used abbreviated versions of e.g., MARKETVA for market value because some variables contain the full combination MARKETVALUE but others do not.
    If other variables in your dataset contain e.g., only the letters MARKETV to indicate market avlue, the code has to be adjusted accordingly.
    Note also I assume ALUMINIUMBAHRAI and ALUMINIUMBAHRAIN are the same company, but other than just replacing the name of ALUMINIUMBAHRAI with ALUMINIUMBAHRAIN I have no solution that is more generically applicable.
    Also note one variable has no clear indication of what it represents (I named it WHATSTHIS below)
    Lastly, note no date was present in you example data so I created a fake one, going on your description.




    Code:
    gen date =_n
    ren * v*
    ren vdate date
    order date, first
    reshape long v, i(date) j(somestring) string
    gen name=somestring
    gen var="bookvalue" if strpos(name, "BOOKVAL")
    replace var="marketvalue" if strpos(name, "MARKETVA")
    replace var="totalassets" if strpos(name, "TOTALAS")
    replace var="commonshares" if strpos(name, "COMMONSH")
    replace var="WHATSTHIS" if var==""
    
    replace name = substr(name, 1, strpos(name, "BOOKVAL")-1) if strpos(name, "BOOKVAL")
    replace name = substr(name, 1, strpos(name, "MARKETVA")-1) if strpos(name, "MARKETVA")
    replace name = substr(name, 1, strpos(name, "TOTALAS")-1) if strpos(name, "TOTALAS")
    replace name = substr(name, 1, strpos(name, "COMMONSH")-1) if strpos(name, "COMMONSH")
    
    drop somestring
    reshape wide v, i(name date) j(var) string

    Comment

    Working...
    X