Statalist


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

AW: st: AW: AW: Something wrong with the -count- option in the -egen-?


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   AW: st: AW: AW: Something wrong with the -count- option in the -egen-?
Date   Thu, 13 Aug 2009 11:26:43 +0200

<> 

I would say that the "mistake" that you made is an obvious hazard, as the
-sum- -total- and -count- functions for -egen- are not easily separated in
users` heads, and the added complication of an -expression- being allowed as
the argument compounds the problems.

Admittedly, I had to read the definition in the -h egen, count()- to solve
your puzzle. Still, you can combine the -count()- function with the -by-
prefix, and it can be handy then to see how many non-missing entries there
are for a certain variable...


*************
clear*
set obs 5
gen id=_n
expand 10

set seed 43987
gen x=rnormal()

//25 % artificially missing
replace x=. if  /* 
 */ runiform()<0.25

//How many non-missing per group?
bys id: egen mycount=/* 
 */ count(x)

list, noobs sepby(id)
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von gjhxmu@sina.com
Gesendet: Donnerstag, 13. August 2009 11:16
An: statalist
Betreff: Re: st: AW: AW: Something wrong with the -count- option in the
-egen-?

Martin,

Thank you for your help, and now I know the inner rationale for -count- in
the -egen-.

The -total- option is helpful.

BTW, what is the most value for -count- option in the -egen- ? 

I can get r(N) quickly after stand alone -count- .


Best regards,

Rose.


----- Original Message -----
From: Martin Weiss <martin.weiss1@gmx.de>
To: <statalist@hsphsun2.harvard.edu>
Subject: st: AW: AW: Something wrong with the -count- option in the -egen-?
Date: 2009-8-13 17:02:02


<> 

BTW, the standalone -count- command leaves "r(N)" behind, so you can process
the number further. If you want it in your dataset, you can use -egen,
total()-


*************
clear
input x
1
2
3
5
.
end

egen yyy=/* 
*/ total(x==2)
list, noobs

*************



HTH
Martin


-----Urspr?gliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Martin Weiss
Gesendet: Donnerstag, 13. August 2009 10:49
An: statalist@hsphsun2.harvard.edu
Betreff: st: AW: Something wrong with the -count- option in the -egen-?


<> 

Nothing is wrong, luckily... As -h egen- says about the -count- option: "
creates a constant ... containing the number of nonmissing observations of
exp."

So the surprising result of your last line is easily explained: The
-expression- fed to -count- evaluates to nonmissing -either 0 or 1- for
every one of those five rows of your dataset, and -count- picks up this
number. All it cares about is the fact that the expression is non-missing,
not its content. If you want to -count-, use the standalone -count-...

*************
clear
input x
1
2
3
5
.
end
egen y=count(x)
egen yy=count(x>2)
egen yyy=count(x==2)
//another xmpl, 
//20 is not even in the list of values
egen yyyy=count(x==20)
//the following dummy is counted
//it has 5 non-missing entries...
gen byte dummy=x==20
l,noobs
//stand alone -count-
cou if x==2
*************



HTH
Martin


-----Urspr?gliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von gjhxmu@sina.com
Gesendet: Donnerstag, 13. August 2009 10:37
An: statalist
Betreff: st: Something wrong with the -count- option in the -egen-?

I typed the following in the stata and found -count- option didn't work
well.

Anything wrong?

clear
input x
1
2
3
5
.
end
egen y=count(x)
egen yy=count(x>2)
egen yyy=count(x==2)
l,noobs

+------------------+
| x y yy yyy |
|------------------|
| 1 4 5 5 |
| 2 4 5 5 |
| 3 4 5 5 |
| 5 4 5 5 |
| . 4 5 5 |
+------------------+


Thank you for any help!

Best regards,
Rose

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


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


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

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


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



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