Statalist


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

Re: st: Generate data and matrix from unbalanced panel


From   n j cox <n.j.cox@durham.ac.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Generate data and matrix from unbalanced panel
Date   Mon, 30 Jul 2007 15:55:44 +0100

Others have addressed part of this. I just want to
comment on the example code:

sort ID
by ID: egen pd= count(var1)
sort pd
by pd: egen Np= count(pd)
replace Np= Np/pd
gsort pd  ID year

If I understand the sort order wanted, it
is achieved by

bysort ID : gen pd = _N
sort pd ID yr

Occasionally, -egen, count()- and this
more direct method give different answers
when there are missing values.

Nick
n.j.cox@durham.ac.uk

Nguyen Cong Minh

I have the unbalanced panel data like this:

Id	yr	var1	var2	var3
1	1991	2	3	4
1	1992	32	32	22
1	1993	32	2	23
2	1992	21	32	32
2	1993	32	49	88
3	1991	22	32	34
3	1992	32	87	76
3	1993	33	32	43
4	1991	3	44	33
5	1993	32	33	32
.	..	..	..	..

I would like to transform the data into new one (sorted by the number of
years that we observed the ID i)

Id	yr	var1	var2	var3
4	1991	3	44	33
5	1993	32	33	32
2	1992	21	32	32
2	1993	32	49	88
1	1991	2	3	4
1	1992	32	32	22
1	1993	32	2	23
3	1991	22	32	34
3	1992	32	87	76
3	1993	33	32	43

I know how to transform to this data in Stata:

sort ID
by ID: egen pd= count(var1)
sort pd
by pd: egen Np= count(pd)
replace Np= Np/pd
gsort pd  ID year

But I don't know how to write a program can do this transformation and give
me pd and Np into vector pd[.,1], Np[.,1],
(in this example, pd[]=(1 2 3)' and Np[]=(2 1 2)').And how I can use
pd[.,1], Np[.,1] to partition
the whole transformed matrix [Id yr var1 var2 var3] into blocks(i) [Id yr
var1 var2 var3] with rows = pd[i,1]*Np[i,1]?


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



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