Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Generating variable that counts number of appearances

From   Timothy Malacarne <>
Subject   st: Generating variable that counts number of appearances
Date   Sat, 31 Mar 2012 01:29:34 -0400


I'm attempting to create a variable the counts the number of times a unique id value for existing variable 1 appears across all observations variable two.  In this case, individuals have unique IDs.  They are then asked to name their friends, who appear by their id number in a series of friend variables.  Each value is unique in the "ID" variable, but 3 people might nominate the same ID value under the Friend variable.  I want to count up how often each individual is named a friend by others.

To do so, I wrote this code:

gen NumberFriends=.;

forval k =   90316050/99798989 {;  *the range of ID values
		egen tot`k' = total(Friend1ID==`k');
		replace NumberFriends=tot`k' if `k'==ID;
		drop toad`k';

Unfortunately, it needs to loop over all values in the ID range for it to work and I don't have the computational power to do this in a reasonable amount of time.  Is there a computationally more efficient way to count up ID cross references in Stata?

Thanks a lot,

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index