Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <n.j.cox@durham.ac.uk> |
To | "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu> |
Subject | st: RE: creating new variable under certain conditions |
Date | Wed, 28 Sep 2011 15:19:14 +0100 |
Look at -egen-. You want to calculate in groups, something like the following bysort gvkey appyear : egen NEW1 = total(n_int_cites) by gvkey appyear : egen NEW2 = mean(n_cites) Nick n.j.cox@durham.ac.uk S.H. Former I have a problem concerning the composition of my database. The database looks as follows: gvkey appyear patent n_int_cites n_cites n_patentsyr 1004 1985 4751021 1 6 1 1004 2001 6622640 1 2 1 1010 1974 4178953 1 2 1 1010 1975 4017058 1 2 1 1010 1977 4113233 1 3 7 1010 1977 4121614 4 8 7 1010 1977 4137937 1 4 7 1010 1977 4158453 3 3 7 1010 1977 4160460 3 5 7 1010 1977 4178755 3 6 7 1010 1977 4184663 2 11 7 I want to create a new variable where the n_int_cites is added up but only when gvkey is similar and appyear is similar. Another variable should be created where the average is taken from n_cites but again only when gvkey is the same and appyear is the same. So it should look as follows: gvkey appyear patent n_int_cites n_cites n_patentsyr NEW1 NEW2 1004 2001 4751021 3 6 2 4 5 1004 2001 6622640 1 4 1 4 5 1010 1974 4178953 6 2 1 6 2 1010 1975 4017058 1 2 1 1 2 1010 1977 4113233 1 3 7 17 5.71 1010 1977 4121614 4 8 7 17 5.71 1010 1977 4137937 1 4 7 17 5.71 1010 1977 4158453 3 3 7 17 5.71 1010 1977 4160460 3 5 7 17 5.71 1010 1977 4178755 3 6 7 17 5.71 1010 1977 4184663 2 11 7 17 5.71 So the important thing is that only observations should be considered in a calculation when gvkey and appyear are the same! * * 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/