Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Eric Booth <ebooth@ppri.tamu.edu> |
To | "<statalist@hsphsun2.harvard.edu>" <statalist@hsphsun2.harvard.edu> |
Subject | Re: st: merging same variables within a dataset |
Date | Fri, 12 Aug 2011 20:10:23 +0000 |
<> I'd use -reshape- + -collapse-, not -merge-. You'll need to get carryforward for this example (-findit carryforward-): **************! clear inp str12(Name Position) Salary Bonus /// Compensation str10(Company) John Manager 10 10 20 a Jack Manager 20 20 30 a Brian Director 10 10 40 c Amy Manager 20 20 30 d John Director 10 10 20 a Amy Director 20 30 20 d Amy Other 10 12 30 f end **Position and Companies in one String: g i = _n bys Name: g j = _n qui su j loc max `r(max)' reshape wide Position Company, i(i) j(j) foreach p in Position Company { forval n = 1/`max' { gsort Name -`p'`n' by Name: carryforward `p'`n', replace } ** egen `p'_all = concat(`p'*) , punct(" ") replace `p'_all = trim(`p'_all) drop `p'? } **keep totals by Name: collapse (sum) Salary Bonus Compensation /// (first) Position_all Company_all, by(Name) **************! - Eric __ Eric A. Booth Public Policy Research Institute Texas A&M University ebooth@ppri.tamu.edu On Aug 12, 2011, at 1:33 PM, Dmitriy Glumov wrote: > Hello, > > I have just started using Stata and have ran into a problem. I would > like Stata to identify observations within a dataset that have the > same name, and add their respective values. To give an example, the > sample of the dataset looks something like this: > > Name Position Salary Bonus Compensation Company > John Manager 10 10 20 a > Jack Manager 20 20 30 a > Brian Director 10 10 40 c > Amy Manager 20 20 30 d > John Director 10 10 20 a > Amy Director 20 30 20 d > > > And I want it to end up looking like this: > > Name Position Salary Bonus Compensation Company > > John Manager/Director 20 20 40 a > Jack Manager 20 20 30 a > Brian Director 10 10 40 c > Amy Manager/Director 40 50 50 d > > In short, I would like Stata to identify same names and then add > salary, bonus, and compensation together, while also keeping the > company name and position. It is best if the company displays only one > label during the merge ("a" instead of "aa" or "a-a", etc) but, as far > as position, either merging or appending or just displaying either > name would be fine (so ManagerDirector or just one of the names would > also work), I just don't want it to create an error during the > transformation process. > > Thank you for your consideration and any help with this would be > greatly appreciated. > > Dmitri > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/