Announcement

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

  • Generating variable based on two groups

    Hello,

    I'm working with a dataset from Comtrade, which include information about import, export and their respective trade values for 3 different countries: Italy, Belgium and Israel.
    The data looks like this:

    tradeflow | reporter | partner | tradevalues

    import | belgium | israel | 1891976735
    export | belgium | israel | 2663650495
    import | belgium | italy | 14696719360
    export | belgium | italy | 21122317754
    import | italy | belgium | 20047180815
    export | italy | belgium | 15239360972
    import | italy | israel | 927106045
    export | italy | israel | 2942813832


    Based on this, I'd like to generate a variable which shows the sum of import and export tradevalues by each combination of countries. For example, a variable which shows the sum of tradevalues for Belgium and Israel in the first two rows (say, 1891976735+2663650495) . Then, another sum for Belgium and Italy in the row 3 and row 4 (14696719360+21122317754).

    Again, the idea is to generate a variable which shows import+export for each combination of reporter and partner.

    Thanks!

  • #2
    Javier:
    you may want to try something along the following lines:
    Code:
    . egen flag=group( reporter partner)
    
    . bysort flag: egen overall=total( tradevalues)
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      If I understood right, you may as well type:

      Code:
      by reporter partner, sort: egen mytot = total(tradevalues)
      Best regards,

      Marcos

      Comment


      • #4
        If #2 or #3 is what you want, fine. But be aware of the extra trickery discussed at https://www.stata-journal.com/articl...article=dm0043

        dm0043 is thus an otherwise unpredictable search term for mentions here on Statalist.

        Comment


        • #5
          Both Carlo's code and Marcos' Code worked!
          Thanks a lot
          Best,

          Comment

          Working...
          X