结合来自不同变量的观察结果

时间:2019-06-19 17:06:34

标签: stata

我有以下变量:

* Example generated by -dataex-. To install: ssc install dataex
clear
input long id int year str20 word
4971 2005 "dividend"      
4971 2005 "seed"          
4971 2006 "circle"        
4971 2008 "shy"           
4971 2008 "old"           
4971 2009 "stop"          
4971 2010 "image"         
4971 2010 "mutation"      
4971 2011 "coffin"        
4971 2011 "commemorate"   
4971 2011 "congress"      
4971 2011 "publication"   
4971 2012 "economics"     
4971 2012 "lung"          
4971 2013 "pyramid"       
4971 2014 "continental"   
4971 2015 "environmental" 
4971 2015 "battlefield"   
4995 2016 "siege"         
5051 2007 "confine"       
5051 2008 "reject"        
5051 2009 "engagement"    
5051 2013 "identification"
5051 2013 "root"          
5786 2005 "rational"      
5786 2008 "jury"          
5786 2008 "tidy"          
5786 2009 "presence"      
5786 2009 "try"           
5786 2013 "pocket"        
5786 2014 "summary"       
5786 2016 "train"         
6287 2005 "fish"          
6287 2009 "forward"       
6287 2011 "prejudice"     
6287 2012 "horizon"       
6287 2012 "constituency"  
6287 2012 "sail"          
6287 2012 "assessment"    
end

如何在Stata中按年份组合给定ID的所有单词?

例如,对于id == 4971year == 2011,可以在下面找到预期的输出:

4971 2011 publication coffin commemorate congress publication

1 个答案:

答案 0 :(得分:2)

以下对我有用:

bysort id year: generate wanted = word if _n == 1
bysort id year: replace wanted = wanted[_n-1] + " " + word if _n > 1
bysort id year: keep if _n == _N

list id year wanted, sepby(id)

     +-------------------------------------------------------+
     |   id   year                                    wanted |
     |-------------------------------------------------------|
  1. | 4971   2005                             dividend seed |
  2. | 4971   2006                                    circle |
  3. | 4971   2008                                   shy old |
  4. | 4971   2009                                      stop |
  5. | 4971   2010                            image mutation |
  6. | 4971   2011   coffin commemorate congress publication |
  7. | 4971   2012                            economics lung |
  8. | 4971   2013                                   pyramid |
  9. | 4971   2014                               continental |
 10. | 4971   2015                 environmental battlefield |
     |-------------------------------------------------------|
 11. | 4995   2016                                     siege |
     |-------------------------------------------------------|
 12. | 5051   2007                                   confine |
 13. | 5051   2008                                    reject |
 14. | 5051   2009                                engagement |
 15. | 5051   2013                       identification root |
     |-------------------------------------------------------|
 16. | 5786   2005                                  rational |
 17. | 5786   2008                                 jury tidy |
 18. | 5786   2009                              presence try |
 19. | 5786   2013                                    pocket |
 20. | 5786   2014                                   summary |
 21. | 5786   2016                                     train |
     |-------------------------------------------------------|
 22. | 6287   2005                                      fish |
 23. | 6287   2009                                   forward |
 24. | 6287   2011                                 prejudice |
 25. | 6287   2012      horizon constituency sail assessment |
     +-------------------------------------------------------+