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

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: RE: Modified Question: Manipulating an Unbalanced Panel |

Date |
Mon, 24 Nov 2008 12:37:50 -0000 |

==== solution 1 A direct answer to your question is that you could exploit the results of your -count- command by using its result saved in r(N). For example, gen byte nOK = 0 egen group = group(PanelMember) if consumption > 0 & consumption < . su group, meanonly qui forval i = 1/`r(N)' { count if group == `i' replace nOK = r(N) if group == `i' } followed by ... if nOK >= 2 ==== But there are other and better ways to do that. ==== solution 2 One is egen nOK = total(consumption > 0 & consumption < .) , by(PanelNumber) followed by ... if nOK >= 2 & consumption > 0 & consumption < . As -consumption > 0- is true, and evaluates to 1, whenever -consumption- is positive, the -egen- statement counts suitable observations within each panel. The extra condition -consumption < .- has been added to exclude any missings, which also count as positive. That does no harm and may catch some problems. (The -count- statement above does the same.) ==== ==== solution 3 Another is closer to your original code: gen byte OK = consumption > 0 & consumption < . bysort OK PanelMember : gen nOK = cond(OK == 0, 0, _N) ... if nOK >= 2 ==== At this moment, the last is in my view the best way to do what you want. Nick n.j.cox@durham.ac.uk P.S. In your example, the variable counts observations in each panel, not the number of panels. ippab ippab I just realized that I can generate a variable with the number of panels with the following command: by PanelNumber: gen CountSessions = _N . But, in my data, there is another variable which indicates if that observation has any positive consumption. I actually need to count the number of positive consumptions for each panel. There are sessions without any consumption. For example, I used "by PanelNumber: gen CountSessions = _N if consumption>0". This is wrong because this gives CountSessions =2 even if a panel has one session (observation) with positive consumption and one session (observation) without any consumption. I found that the following command gives me the right output on the screen: "by PanelNumber: count if consumption>0". However, I don't know how to generate a variable to capture the output for the command: "by PanelNumber: count if consumption>0". I would really appreciate some help with this. * * 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/

**References**:**st: Modified Question: Manipulating an Unbalanced Panel***From:*"ippab ippab" <ippab.statalist@gmail.com>

- Prev by Date:
**st: RE: significance of mean and median** - Next by Date:
**st: R: significance of mean and median** - Previous by thread:
**st: Re: Modified Question: Manipulating an Unbalanced Panel** - Next by thread:
**st: Combining and generating dummy variables** - Index(es):

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