Announcement

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

  • looping by a specific variable

    Hi everyone.

    I generated the following

    Code:
    egen firmlevel=group(CompanyID AnnualReportYear)
    And then I tried to use the loop command as follows:

    Code:
    foreach v of varlist Main* {
    gen sum`v'=sum(`v'), by (firmlevel)
    However, I received the following message:

    Code:
    option by() not allowed
    Does anyone has an idea how I can do it?

  • #2
    Mo:
    try, instead:
    Code:
    foreach v of varlist Main* {
    bysort firmlevel: gen sum`v'=sum(`v')
     }
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      I have to wonder whether you really want egen, total()

      A cumulative or running sum doesn't make obvious sense here.

      Comment


      • #4
        Originally posted by Carlo Lazzaro View Post
        Mo:
        try, instead:
        Code:
        foreach v of varlist Main* {
        bysort firmlevel: gen sum`v'=sum(`v')
        }
        Thanks. It worked.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          I have to wonder whether you really want egen, total()

          A cumulative or running sum doesn't make obvious sense here.
          I have a dummy variable for each variable of the Main* (e.g. Main_gender=1 if female, 0 otherwise) and I would like to generate the sum of each variable for each firm-year observation (the sum of 1s in my case). Therefore, I will have the number of, for example, females for each fimr-year observation.

          In short: I would do the following code for each variable:

          egen totalvar=sum(var), by (firmlevel)
          Last edited by Mo Hos; 21 Nov 2019, 10:35.

          Comment


          • #6
            Code:
            egen firmlevel=group(CompanyID AnnualReportYear)
            I realised that there are different results generated from the following two methods:

            For the first method, I would do this for each variable:
            Code:
            egen totalvar=sum(var), by (firmlevel)
            For the second one, I would use the following command:
            Code:
            foreach v of varlist Main* {
            bysort firmlevel: gen Sum`v'=sum(`v')
            }
            The results of the first method are the one I want. However, I would like to do it like the second method.
            Last edited by Mo Hos; 21 Nov 2019, 10:47.

            Comment


            • #7
              Code:
               
               egen totalvar = sum(var), by (firmlevel)
              is perfectly legal, but as from Stata 9 the sum() function of egen is undocumented. The name total() for sums, meaning totals, is meant to insert a slim difference given that the Stata function sum() returns a cumulative or running sum. Indeed Mata disambiguates thoroughly by using the name runningsum().

              A bridge between the two is


              Code:
               
               bysort firmlevel: gen Sum`v' = sum(`v')  by firmlevel: replace Sum`v' = Sum`v'[_N]
              as the last cumulative sum is the total concerned. Example, the cumulative sum of 1, 2, 3 runs 1, 3, 6 and 6 is also the total.

              Comment


              • #8
                @Nick Cox Worked perfectly. Thanks.

                Comment


                • #9
                  For anyone puzzled by #7 the second code block contains two statements

                  Code:
                  bysort firmlevel: gen Sum`v' = sum(`v')
                  
                  by firmlevel: replace Sum`v' = Sum`v'[_N]

                  Comment

                  Working...
                  X