Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

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

From   Jeph Herrin <>
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.


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?


var1 var2
1 A
2 A
3 A
3 B

tab var1 if var2=A
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,


Never miss an email again!
Yahoo! Toolbar alerts you the instant new Mail arrives.
* For searches and help try:

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index