Statalist


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

Re: st: transform observations in variables


From   Nick Cox <n.j.cox@stata.com>
To   Nick Cox <n.j.cox@stata.com>
Subject   Re: st: transform observations in variables
Date   Mon, 22 Sep 2008 16:31:01 -0500

An alternative is

egen period1 = mean(size) if period == 1, by(familyname)
bysort familyname (period1) : replace period1 = period1[1]


So this can be generalised to

forval i = 1/3 {
	egen period`i' = mean(size) if period == `i', by(familyname)
	bysort familyname (period1) : replace period`i' = period`i'[1]
}

That should deal more smoothly with repeats (if any) and gaps.

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

Nick Cox wrote:

Consider -period- 1. Then

(period == 1) * size

is 0 when -period- is not 1 and -size- when -period- is 1. So

egen period1 = total((period == 1) * size), by(familyname)

should do the trick so long as -period- is 1 just once for each family.

Similarly, the other -period- values should fall to the same trick:

forval i = 1/3 {
egen period`i' = total((period == `i') * size), by(familyname)
}

Now one problem is apparent: family AB for example is not observed at
periods 2 and 3. So -period2- and -period3- will be 0 for that family.
If -size- can only be positive, then no problem: 0 means missing, and that can be fixed.

If -size- can be zero, then some other solution will be needed.

If each value of -period- can occur more than once for each family, then that is soluble, but I'll wait to hear that can happen.

Nick
permanent address:n.j.cox@durham.ac.uk


Fabian Brenner

My data look like the following:

"familiyname" "period" "size" "period1" "period2"
"period3"

AA 1 8
AA 2 7
AA 3 3

AB 1 4

BG 1 4
BG 2 8

For each "familyname" I want to get the values of "size" in the column
with the equivalent "period" (1 to 3). The values of "size" should be
written in the column of the equivalent period for all observations of
the same "familyname" (that is why I wasn't able to manage it with -if-)

The result should be:

"familiyname" "period" "size" "period1"
"period2" "period3"

AA 1 8 8
7 3
AA 2 7 8
7 3
AA 3 3 8
7 3

AB 1 2 2


BG 1 4 4
6
BG 2 6 4
6




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