Announcement

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

  • How to calculate total per country and year?

    Hi,

    I am posting a sample from my main data. I want to perform calculation of variable 1 and variable 2 per country per year. So the dataset looks like this:


    Code:
     
    year code country Variable 1 Variable 2
    1990 11 Argentina 1 0
    1990 11 Argentina 2 1
    1991 11 Argentina 3 2
    1991 11 Argentina 4 1
    1990 12 Bangladesh 1 5
    1990 12 Bangladesh 2 2
    1991 12 Bangladesh 3 1
    1991 12 Bangladesh 4 2
    1990 13 Cameroon 0 0
    1990 13 Cameroon 0 0
    1991 13 Cameroon 0 1
    1991 13 Cameroon 1 0


    How I want my data:


    Code:
     
    year code country Variable 1 Variable 2
    1990 11 Argentina 3 1
    1991 11 Argentina 7 3
    1990 12 Bangladesh 3 7
    1991 12 Bangladesh 7 3
    1990 13 Cameroon 0 0
    1991 13 Cameroon 1 1


    SO as you can see now I have data per year and per country. Instead of repeating years for each observation. Can you help me with the code. And I also want to retain year code and country.


    Best Regards,

    Abdullah


  • #2
    You can use collapse for this.

    At Stata's prompt, type
    Code:
    help collapse
    for how.

    Comment


    • #3
      Abdullah:
      welcome to this forum.
      See -collapse-.

      ETA: Joseph and myself have reached the party together. 😀
      Last edited by Carlo Lazzaro; 07 May 2022, 05:07.
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        Thank you so much both of you. When I try using collapse commad:

        collapse (count) Variable1 Variable2, by(year)

        It gives me total and country and code get eliminated.

        Maybe I am using the wrong code.

        Comment


        • #5
          collapse (sum) Variable1 Variable2, by( code year country)


          This one worked. Many thanks !!!

          Comment

          Working...
          X