Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | <richgold@ix.netcom.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: Droping rows in the other data set |
Date | Sun, 9 Feb 2014 16:01:16 -0500 (GMT-05:00) |
if I understand you correctly the following appears to be a much easier strategy use data1 keep id duplicates drop sort id save id1 us data2 sort id merge m:1 is using id1 drop if _merge==1 save data2a of course you need to substitute your own data set names, etc. also, you were not clear about what you want to do with any id that is in data1 but not in data2 (these will have _merge=2) Rich -----Original Message----- >From: Eilya Torshizian <e.torshizian@auckland.ac.nz> >Sent: Feb 9, 2014 3:38 PM >To: "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> >Subject: st: Droping rows in the other data set > >Dear Statalisters, > >I have two datasets. I would like to delete the rows in the Second data set that are not included in the First data set. The "ID" variable is included in both datasets with repeating values. However, some rows of the "ID" are dropped in the First data set. Let's assume that the "ID" variable in the First dataset is as follows, >First: > ID >11 >11 >13 >15 >While the "ID" variable does not include 12 and 14 values in the First data set, in the Second dataset 12 and 14 are included: >Second: >ID >11 >12 >12 >13 >14 >14 >15 > >I need to delete the incompatible rows from the Second data set. > >I was thinking of using the 'egen - anymatch' command. To do so, I need the list of values from the First data set, which is derived from the following command, > >- use "First.dta", clear >- levelsof ID >- global LIST `r(levels)' >- clear > >Then I use the LIST scalar in the second data set: > >- use "Second.dta" >- egen KEEP = anymatch(ID), values(LIST) >- drop if KEEP == 0 > >However, as the LIST is scalar, I am not able to do so. I appreciate your comments. > >Kind regards, >Eilya. > > > >* >* For searches and help try: >* http://www.stata.com/help.cgi?search >* http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/