Statalist The Stata Listserver

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

Re: st: Collapsing with strings

From   Gary Longton <>
Subject   Re: st: Collapsing with strings
Date   Tue, 17 Jan 2006 11:54:40 -0800

Yes, as usual, a cleaner and much more simple solution to Daphna's duplicates problem than mine, staircase or not!

- Gary

Nick Cox wrote:

L'esprit de l'escalier:

When observations come in blocks of 2 another
approach is possible that removes the need for sorting on each variable, although one prior

sort id
is needed.
foreach var of varlist var1-var3 {
by id: replace `var' = `var'[3 - _n] if missing(`var') }

When the missing values are in the first observation, _n is 1 and 3 - _n is 2. When the second, _n is 2 and 3 - _n
is 1. Hence 3 - _n indexes the other observation in blocks of 2. This works regardless of variable type.

*   For searches and help try:

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