Announcement

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

  • Replace command not working...

    Hi guys

    First post here, so please bear with me if I'm doing something wrong :-)

    I am trying to generate a variable (revenueTotal) that takes the value of the total sum of another variable (revenue) based on the year (either 2013 or 2019). I use 2013 for my egen command. I then want to do the same for the year 2019 and use the replace command for this. The code is as follows:

    Code:
    // We generate a variable that takes the value of the sum of the revenue of all firms in the data set (dependent on year):
    
    egen revenueTotal = total(revenue) if year==2013
    
    replace revenueTotal = total(revenue) if year==2019
    However, Stata does not recognise the total function in the replace command and I get the following output:

    . // We generate a variable that takes the value of the sum of the revenue of all firms in the data set (dependent on year):
    .
    . egen revenueTotal = total(revenue) if year==2013
    (498 missing values generated)

    .
    . replace revenueTotal = total(revenue) if year==2019
    unknown function total()
    r(133);


    What am I doing wrong?
    Hope someone can help me :-)

    Cheers,
    Rasmus

  • #2
    try this if you're just looking at two years.
    Code:
    egen revenueTotal= total(revenue), by(year)

    Comment


    • #3
      I think I know the problem, but I want to be sure. So please give an example of your dataset using dataex

      Comment


      • #4
        That worked! Thank you very much :-)

        Any idea why my code doesn't work though? I have installed the egenmore package.

        Comment


        • #5
          Code:
          clear
          set obs 30
          g N = _n
          g year = N>15
          egen rT = total(N), by(year)
          egen x = total(cond(year==0,N,.))
          egen y = total(cond(year==1,N,.))
          replace x = y if year==1

          Comment


          • #6
            You can't use egen commands in replace.

            Comment


            • #7
              Thanks for your replies George! Regarding your reply Jared, here is an example of the data with the variables idstd, year and revenue;

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input long idstd double revenue float year
              540503   1.080e+09 2013
              540504   1.500e+09 2013
              540508   1.230e+08 2013
              540509   2.500e+09 2013
              540512   4.800e+09 2013
              540513   3.600e+08 2013
              540514   3.000e+10 2013
              540515    67900875 2013
              end
              label values revenue D2

              Comment

              Working...
              X