# Re: st: writing a program that uses TAB results for every observation

 From Jeph Herrin To statalist@hsphsun2.harvard.edu Subject Re: st: writing a program that uses TAB results for every observation Date Mon, 26 Mar 2007 11:57:30 -0400

```Not sure how many values -var2- has, but whether a list of
them will fit in the largest macro allowed by your flavor of
Stata you may be able to use -levelsof- to get a list of
-var2- values and loop over them. But this alternative will
surely work, if less efficiently:

bys var2 var1: gen n=_N
bys var2:      gen N=_N
gen sqpct = (n*100/N)^2
bys var2:      egen var3 = sum(sqpct)

Note that this assumes you want missing values tabulated
as well, if not then -drop- them first.

hth,
Jeph

T.J. Volant wrote:
```
Hello all,

I'm needing to TAB for every observation (of several
million) and pick out only certain results from the
TAB and assign it for the relevant observation. How
would I do that?

Example:

var1 var2
1 A
2 A
3 A
3 B

tab var1 if var2=A
results:
1 33%
2 33%
3 33%

For each of observation where var2=A, I would like to
assign sum of the squares of the percentages in the
tab results table in a new variable (say var3). So,
final result would look like:

var1 var2 var3
1 A 3267 (=33^2+33^2+33^2)
2 A 3267
3 A 3267
3 B -

How can I do this? Thank you very much in advance,

Tj

____________________________________________________________________________________
Never miss an email again!
Yahoo! Toolbar alerts you the instant new Mail arrives.
http://tools.search.yahoo.com/toolbar/features/mail/
*
* 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/

```*
*   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/
```