I too am fond of -levelsof-. For the problem mentioned,
this would need to be embedded in a loop over groups,
somewhat as follows:
gen nvals = .
egen group = group(Gvkey year)
su group, meanonly
qui forval i = 1/`r(max)' {
levelsof psic if group == `i', local(p)
levelsof ssic if group == `i', local(s)
local total: list s | p
local total:list uniq total
local count:list sizeof total
replace gen nvals = `count' if group == `i'
}
Nick
[email protected]
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]]On Behalf Of
> Scott Merryman
> Sent: 19 June 2005 12:30
> To: [email protected]
> Subject: st: RE: RE: unique value count in several variables
>
>
> In addition to Nick's suggestion of using -reshape-, another
> possibility is
> to use -levelsof- and the macro extended functions (assuming
> your cross
> sections are not too large):
>
>
> . l, noobs
>
> +------------------------------------+
> | gvkey psic ssic year subno |
> |------------------------------------|
> | 1223 4767 4743 1999 1 |
> | 1223 4767 4763 1999 2 |
> | 1223 4757 4767 1999 3 |
> | 1223 4767 4753 1999 4 |
> | 1223 4777 4787 1999 5 |
> |------------------------------------|
> | 1223 4767 4743 1999 6 |
> +------------------------------------+
>
> . levelsof psic, local(p)
> 4757 4767 4777
>
> . levelsof ssic, local(s)
> 4743 4753 4763 4767 4787
>
> . local total: list s | p
>
> . local total:list uniq total
>
> . local count:list sizeof total
>
> . gen nvals = `count'
>
> . l, noobs
>
> +--------------------------------------------+
> | gvkey psic ssic year subno nvals |
> |--------------------------------------------|
> | 1223 4767 4743 1999 1 7 |
> | 1223 4767 4763 1999 2 7 |
> | 1223 4757 4767 1999 3 7 |
> | 1223 4767 4753 1999 4 7 |
> | 1223 4777 4787 1999 5 7 |
> |--------------------------------------------|
> | 1223 4767 4743 1999 6 7 |
> +--------------------------------------------+
>
>
> Scott
>
>
> > -----Original Message-----
> > From: [email protected] [mailto:owner-
> > [email protected]] On Behalf Of Wanli Zhao
> > Sent: Saturday, June 18, 2005 3:17 PM
> > To: [email protected]
> > Subject: st: RE: unique value count in several variables
> >
> > Thanks, Nick. I looked into the suggestions and I think I might have
> > confused you on my problem. My panel data is like this:
> > Gvkey psic ssic year subno
> > 1223 4767 4743 1999 1
> > 1223 4767 4763 1999 2
> > 1223 4757 4767 1999 3
> > 1223 4767 4753 1999 4
> > 1223 4777 4787 1999 5
> > 1223 4767 4743 1999 6
> >
> > Using command unique, I can count the distinct values of
> psic and ssic by
> > gvkey by year. So for psic it's 3 and for ssic it's 5. what
> I want is to
> > count the distinct values of both psic and ssic by gvkey by
> year. In this
> > case, it's 7 (4767, 4757, 4777, 4743, 4763, 4753, 4787).
> How to generate a
> > new variable for my purpose? Hope I'm clear now. Pls help.
> >
> > Thanks.
> > Wanli Zhao
> >
>
>
> *
> * 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/