Announcement

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

  • Panel Data: Need variable that calcs shares by variable type and another dimension. Egen?

    Hi all,

    I have a panel database as shown below and need to calculate the levels of type 1 as % of levels in type 3 for the same countries. What command allows me to get the sample in brackets below?I couldn't get this with egen and by. Thanks.


    year type level country [I want Type 1 level as % of Type 3 level]
    2000 1 20 A 10
    2001 1 20 A 10
    2002 1 30 A 30
    .
    .
    .
    2000 2 100 A
    2001 2 100 A
    2002 2 100 A
    .
    .
    ,
    2000 3 200 A
    2001 3 200 A
    2002 3 200 A
    .
    .
    .
    Last edited by jvgeconomics; 22 Jul 2014, 15:44.

  • #2
    Code:
    clear
    use originaldata.dta
    preserve
    drop if type==2
    sort country year type
    gen pct_1_of_3=(level/level[_n+1])*100 if country==country[_n+1] & year ==year[_n+1] & type==1 & level[_n+1] !=0
    drop level
    save temp1.dta, replace
    restore
    merge 1:1 country year type using temp1.dta
    Should do it. Might use egen whatever=max(pct_1_of_3), by (country year) to fill in on other types if you wanted to do so.
    Last edited by ben earnhart; 22 Jul 2014, 16:18.

    Comment


    • #3
      Shorter:
      Code:
       sort country year type
      gen pct_1_of_3=(level/level[_n+2])*100 if country==country[_n+2] & year ==year[_n+2] & type==1 & level[_n+2] !=0
      egen  trash=max(pct_1_of_3), by (country year)
      replace pct_1_of_3=trash if pct_1_of_3==.
      drop trash

      Comment


      • #4
        Thanks! This worked.

        But what if I also want type 2 as % of type 3 and type 3 as % of type 3 (obv would be 100%!) in that same variable? so type 1 + type 2 = type 3 for every country/year combination and I want a variable that has, for each observation, the share of type 1 as % of type 3, type 2 as % of type 3 and type 3 as % of type 3.
        Last edited by jvgeconomics; 23 Jul 2014, 08:06.

        Comment


        • #5
          If there are no duplicates and no missing observations, then this can be done with a single bysort command

          Code:
          clear
          input year type level str1 country
          2000 1 20 A
          2001 1 20 A
          2002 1 30 A
          2000 2 100 A
          2001 2 100 A
          2002 2 100 A
          2000 3 200 A
          2001 3 200 A
          2002 3 200 A
          end
          
          * make sure that there are no duplicates and reorder
          isid country year type, sort
          
          * if there are no missing obs
          by country year: gen pc1 = level / level[3] * 100
          sort year type country
          list , sepby(country year) noobs
          
          * otherwise use egen to target type 3
          sort country year type
          drop if type == 2 & year == 2001
          by country year: egen level3 = total(level * (type == 3))
          gen pc2 = level / level3 * 100
          sort year type country
          list , sepby(country year) noobs

          Comment


          • #6
            Hi everyone,
            I have 12.000 firms in 5 years ( same of them with cooperation agrements and other without cooperation) , I need to create a variable to see the number of years a firm had participated in collaborative agreements from 2008 to 2012
            Thank you

            Comment


            • #7
              Hi Hana. Since this is a new question it would probably be better placed in a new thread. But I think you want something like

              Code:
              bysort firmid (year): egen ncollab = total(collab == 1)
              This would work if a variable called collab = 1 in years when there was a collaborative agreement. If collab is already a 0/1 dummy, then you could just say total(collab). See the help for egen. A really great source for problems like this is Nick Cox's "Counting Groups, Especially Panels" at http://www.stata-journal.com/sjpdf.h...iclenum=dm0033
              -------------------------------------------
              Richard Williams, Notre Dame Dept of Sociology
              Stata Version: 17.0 MP (2 processor)

              EMAIL: [email protected]
              WWW: https://www3.nd.edu/~rwilliam

              Comment


              • #8
                Hana and jvg, welcome to Statalist. Please note that Statalist users are strongly encouraged to use their real names rather than handles. We feel it promotes a more professional atmosphere, and some people (including some really smart ones!) prefer to deal with people using their real names when asked for help. If you would like to do so, you can either add a signature like I have, or else click on "Contact Us" on the lower right and ask the administrators to change your user name.
                -------------------------------------------
                Richard Williams, Notre Dame Dept of Sociology
                Stata Version: 17.0 MP (2 processor)

                EMAIL: [email protected]
                WWW: https://www3.nd.edu/~rwilliam

                Comment

                Working...
                X