Announcement

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

  • Generating the sum for all variables in panel

    Dear Community,

    I'm currently working with a panel. Every variable var1 has the indicator var2 which doesn't change over time. I would like to calculate the sum of these indicators without taking into accoun the fact that var1 and var2 repeat over time. The problem is when I type

    gen wanted=sum(var2) it takes repeated values into account

    When I type:
    bysort var1: egen wanted=total(var2) I have the sum for every category and it is not what I want to achieve.

    Please help me. I'm out of the ideas.

    Last edited by sladmin; 28 Aug 2023, 09:39. Reason: anonymize original poster

  • #2
    Code:
    bysort var1: gen x = var2 if _n==1
    egen wanted = total(x)

    Comment


    • #3
      Thank you for your answer. Unfortunately I see "invalid" syntax. Has it something to do with the fact that my var1 is string and var2 is double?

      Comment


      • #4
        that shouldn't matter. can you show example data?

        Comment


        • #5
          Unfortunately not because data is sensitive. It comes from the social survey. May it be the case that I didn't create the panel actually because I didn't need a regression. I just wanted to see and calculate how some measures relate to each others. Make graphs etc. If I create ID and set it up as the panel should it work?

          Comment


          • #6
            I understand. please do one or both of the following: 1) say exactly what you typed and exactly what Stata typed in response, 2) demonstrate the problem using simulated data.

            Comment


            • #7
              Shame on me. I had a typo at if _n==1. Instead "==" I put" =". It ofc works!
              Thank you for your help!

              Comment

              Working...
              X