# st: Generate data and matrix from unbalanced panel

 From Marcello Pagano To statalist@hsphsun2.harvard.edu Subject st: Generate data and matrix from unbalanced panel Date Sun, 29 Jul 2007 17:53:13 -0400

```Nguyen Cong Minh wrote:
```
Subject: Generate data and matrix from unbalanced panel

Hi,

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]?

Can someone help me on this problems?

Thank you so much,
Minh

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