# st: Re: data management - loop?

 From "Michael Blasnik" To Subject st: Re: data management - loop? Date Mon, 21 May 2007 13:19:08 -0400

...
There's a much easier way -- without creating the 250 dummies or any looping:

bysort household var: gen count=_N
bysort household (count): gen byte dummy=count==count[_N]

In the event of ties (two values of var with equal frequency), then both values will be included by the dummy.

Michael Blasnik

----- Original Message ----- From: "Alexander Staus" <Alexander.Staus@uni-hohenheim.de>
To: <statalist@hsphsun2.harvard.edu>
Sent: Monday, May 21, 2007 11:48 AM
Subject: st: data management - loop?

Dear Stata users,

in my panel dataset I want a dummy for the most occurred value in a variable.

e.g. for a household a variable can take values from 1 to 250, value 15 is the most named
value in one household, so I want a dummy which is 1 when the household named 15
otherwise 0.

I have some idea but I'm lost in getting the proper loop:

tab var, gen(var)

forvalues i=1(1)250 {

bysort household: gen N_`i'=sum(var`i')
bysort household: replace N_`i'=N_`i'[_N]
}

* Now some loop for:

bysort hh: gen dummy=1 if N_1 > N_2 & N_1 > N_3 & N_1 > N_4 ********more
possible values from 1 to 250 but not every number is named************* & var1 ==1

* here all other possibilities...

Some idea or an easier way?
```*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```