Announcement

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

  • Rangestat with panel

    Good mornign everyone,
    I need help with my panel dataset on trade. I have 5726 dyads over the period 1960 - 2016 (unbalanced). For every dyad-year I have multiple observations and I would like to sum these observations into one single value. That is why I am using
    Code:
    rangestat
    . However I do not understand how to interpret the option interval. In my case the interval is given by year, therefore I have to tell STATA to count in the interval 1960 - 2016.
    To make it clear I give you an example of my dataset:
    Country A Country B year value id
    Italy Germany 1960 1 1
    Italy Germany 1960 1 1
    Italy Germany 2016 1 2
    Germany Italy 1960 1 3
    Germany Italy 1960 1 3
    So what I need is to create a new variable for each dyad (id). In this case I would like to have:
    id value_sum
    1 2
    2 1
    3 2
    My attempt is
    Code:
    rangestat (sum) value, interval(year -1 .) by(id)
    but it is wrong. I tried many combinations but I cannot find a solution.
    Please, any help is much appreciated!

  • #2
    Collapse is a useful tool here

    Code:
    *Some example data:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7(countrya countryb) int year byte(value id)
    "Italy"   "Germany" 1960 1 1
    "Italy"   "Germany" 1960 1 1
    "Italy"   "Germany" 2016 1 2
    "Germany" "Italy"   1960 1 3
    "Germany" "Italy"   1960 1 3
    end
    
    collapse (sum) value (firstnm) countrya countryb, by(id year)
    results in:
    Code:
    . list
    
         +-----------------------------------------+
         | year   id   value   countrya   countryb |
         |-----------------------------------------|
      1. | 1960    1       2      Italy    Germany |
      2. | 2016    2       1      Italy    Germany |
      3. | 1960    3       2    Germany      Italy |
         +-----------------------------------------+
    In the future, please do use dataex to supply an example of your data. See section 12 of the FAQ: https://www.statalist.org/forums/help#stata

    Comment


    • #3
      Jorrit is right.

      To use rangestat (SSC, as you are asked to explain) to get the sums alongside the original data, you would go:


      Code:
      rangestat (sum) value, interval(year 0 0) by(id)
      but for that matter

      Code:
      egen sum = total(value), by(id year)
      does the same thing.

      Comment


      • #4
        Good morning and thank you very much for your precious answers. Indeed yesterday I have finally managed with
        Code:
         
         rangestat (sum) value, interval(year 0 0) by(id)
        Thanks again for your support!

        Comment

        Working...
        X