Announcement

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

  • Stack problems

    Good morning, I have j individuals in a single year, each working at n companies. The companies are deployed horizontally and the variables follow the same order for each company.
    Here is an example of the dataset I have:

    id comp1_id comp1_roa comp1_roe comp1_name comp2_id comp2_roa comp2_roe comp2_name comp3_id comp3_roa comp3_roe comp3_name
    1 11501 0.60 0.30 Ealza 28282 0.12 0.10 Thasleof
    2 83910 0.50 0.20 Werdar 48402 0.32 0.12 Cascan
    3 10485 0.40 0.40 Aldea 29101 0.19 0.05 Raham
    4 93759 0.60 0.10 Clapa 19323 0.28 0.25 Rongifu 82739 0.32 0.12 Burgam

    where:
    id: person id
    comp_id: company id
    comp_roa: company n's roa
    comp_roe: company n's roe
    comp_name: company n's name

    Please note that each variable is chanracterized by a number indicating the n company the individual works at.
    I would like to stack the companies' variables "vertically" and to do this for every individual.
    Ideally, starting from the table above, I would like to get something as follows:

    id comp_id comp_roa comp_roe comp_name
    1 11501 0.60 0.30 Ealza
    1 28282 0.12 0.10 Thasleof
    2 83910 0.50 0.20 Werdar
    2 48402 0.32 0.12 Cascan
    3 10485 0.40 0.40 Aldea
    3 29101 0.19 0.05 Raham
    4 93759 0.60 0.10 Clapa
    4 19323 0.28 0.25 Rongifu
    4 82739 0.32 0.12 Burgam

    How can I go with this? Can you please provide any assistance on this matter?
    I would greatly appreciate.
    Thank you!

  • #2
    Starting data:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte id long comp1_id float(comp1_roa comp1_roe) str6 comp1_name long comp2_id float(comp2_roa comp2_roe) str8 comp2_name long comp3_id float(comp3_roa comp3_roe) str6 comp3_name
    1 11501 .6 .3 "Ealza"  28282 .12  .1 "Thasleof"     .   .   . ""      
    2 83910 .5 .2 "Werdar" 48402 .32 .12 "Cascan"       .   .   . ""      
    3 10485 .4 .4 "Aldea"  29101 .19 .05 "Raham"        .   .   . ""      
    4 93759 .6 .1 "Clapa"  19323 .28 .25 "Rongifu"  82739 .32 .12 "Burgam"
    end
    followed by:
    Code:
    stack comp1_id-comp3_name, into(compid roa roe name)
    Also, in the future, please use dataex for posting your data, as I did above here. It makes it a lot easier for people to copy your data, and gives us more inof about the way your data is stored. For more on how and why of dataex, please see the FAQ: https://www.statalist.org/forums/help#stata

    Comment


    • #3
      Thank you for your suggestion about the dataex command.
      It seems that the code you suggest
      stack comp1_id-comp3_name, into(compid roa roe name) does not quite reproduce what I expected. For instance, individual 1 should be reported in only two lines since he/she follows only two firms. Given the example above, I would expect individual 1, 2, and 3 follow 2 companies each (first 6 observations). while individual 4 follows 3 companies.
      Can you please help me further on that.
      Thank you very much!

      Comment


      • #4
        Youre right, I was too quick to answer.
        Try:
        Code:
        stack id comp1_id-comp1_name id comp2_id-comp2_name id comp3_id-comp3_name, into (id compid roa roe name)
        drop _stack
        sort id compid
        drop if compid==.

        Comment


        • #5
          Code:
          reshape long comp@_id comp@_roa comp@_roe comp@_name, i(id)
          drop if comp_id==.
          drop _j

          Comment


          • #6
            This was super very helpful Romalpa. Thank you so much!

            Comment

            Working...
            X