Announcement

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

  • Creating variable for other events in same region/year

    Hi everyone!

    I am dealing with a worldwide data set on conflict events with an unbalanced panel structure, where I am given the variable "id", "year", as well as "region" and "location".

    I want to create a binary variable "other_conflicts_region" that identifies whether there were other conflicts in the same region in the same year. I also want to create a binary variable "other_conflicts_world" that identifies whether there were any other conflicts in the world in the same year.

    Here is a subset of my data:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str8 id double year str8 region str28 location
    "100_1945" 1945 "Asia"     "Vietnam"                     
    "101_1945" 1945 "Asia"     "Indonesia"                   
    "238_1945" 1945 "Europe"   "USSR"                        
    "129_1946" 1946 "Americas" "Colombia"                    
    "376_1946" 1946 "Europe"   "Estonia"                     
    "380_1946" 1946 "Europe"   "Latvia"                      
    "381_1946" 1946 "Europe"   "Lithuania"                   
    "153_1946" 1946 "Africa"   "Nigeria"                     
    "199_1946" 1946 "Africa"   "Eritrea"                     
    "378_1946" 1946 "Asia"     "Iran"                        
    "109_1946" 1946 "Asia"     "Palestine"                   
    "425_1946" 1946 "Asia"     "China"                       
    "100_1946" 1946 "Asia"     "Vietnam"                     
    "91_1946"  1946 "Asia"     "Philippines"                 
    "101_1946" 1946 "Asia"     "Indonesia"                   
    "238_1946" 1946 "Europe"   "USSR"                        
    "432_1946" 1946 "Americas" "Bolivia"                     
    "371_1946" 1946 "Asia"     "Cambodia (Kampuchea)"        
    "368_1946" 1946 "Asia"     "Princely State of Travencore"
    "364_1946" 1946 "Asia"     "Laos"                        
    "401_1946" 1946 "Europe"   "Greece"                      
    "129_1947" 1947 "Americas" "Colombia"                    
    "166_1947" 1947 "Americas" "Paraguay"
                                       
    end
    I suspect I need to build my code with by and sort, but I am pretty new to this, so I would really appreciate your help and thoughts to get me started!

    Kind regards,
    Birte
    (Stata 17.0 SE)

  • #2
    The number of other people in my group is (the number of people in my group) MINUS 1 (that's me). Similarly,

    .
    Code:
     bysort region year : gen others = _N - 1
    
    . gen wanted = others > 0  
    
    . list if year == 1946, sepby(region)
    
         +-----------------------------------------------------------------------------+
         |       id   year     region                       location   others   wanted |
         |-----------------------------------------------------------------------------|
      1. | 199_1946   1946     Africa                        Eritrea        1        1 |
      2. | 153_1946   1946     Africa                        Nigeria        1        1 |
         |-----------------------------------------------------------------------------|
      3. | 129_1946   1946   Americas                       Colombia        1        1 |
      4. | 432_1946   1946   Americas                        Bolivia        1        1 |
         |-----------------------------------------------------------------------------|
      9. | 109_1946   1946       Asia                      Palestine        8        1 |
     10. | 368_1946   1946       Asia   Princely State of Travencore        8        1 |
     11. | 101_1946   1946       Asia                      Indonesia        8        1 |
     12. | 378_1946   1946       Asia                           Iran        8        1 |
     13. | 100_1946   1946       Asia                        Vietnam        8        1 |
     14. |  91_1946   1946       Asia                    Philippines        8        1 |
     15. | 364_1946   1946       Asia                           Laos        8        1 |
     16. | 425_1946   1946       Asia                          China        8        1 |
     17. | 371_1946   1946       Asia           Cambodia (Kampuchea)        8        1 |
         |-----------------------------------------------------------------------------|
     19. | 381_1946   1946     Europe                      Lithuania        4        1 |
     20. | 376_1946   1946     Europe                        Estonia        4        1 |
     21. | 401_1946   1946     Europe                         Greece        4        1 |
     22. | 238_1946   1946     Europe                           USSR        4        1 |
     23. | 380_1946   1946     Europe                         Latvia        4        1 |
         +-----------------------------------------------------------------------------+
    I went around the block with some other command until I re-discovered this simpler way to think about it.

    Comment


    • #3
      Thank you Nick - that worked perfectly and was very intuitive to understand!

      Comment

      Working...
      X