Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Data Management question

From   Nick Cox <>
To   "" <>
Subject   Re: st: Data Management question
Date   Tue, 25 Jun 2013 18:02:58 +0100

You said

"I could do this the long way with repeated gen and replace commands".

Actually that sounds a nightmare without some small tricks. Here are mine:

egen ABCDE = concat(A B C D E)

For consecutive 1s, the trick is only that somewhere in the string
concatenation we will find "111" or "11".

gen X = strpos(ABCDE, "111") > 0
gen Y = strpos(ABCDE, "11") > 0

Your third definition splits into two

For one non-consecutive 1, the condition "non-consecutive" is superfluous.

For two non-consecutive 1s, there are in total two 1s and they must
not  be consecutive. The variable -Y- already codes two consecutive

Either way, try

gen Z = inlist(A + B + C + D + E, 1, 2) & !Y


On 25 June 2013 17:46, Hitesh Chandwani <> wrote:

> I have a data management issue. I need to generate a couple of
> indicator variables from variables that already exist in the dataset.
> Let's call the required indicator vars X, Y, and Z with values of 1 or
> 0. Whether the value of these vars is 1 or 0 depends on the value of 5
> other indicator variables (A, B, C, D, E). A-E are chronologically
> ordered (i.e., A indicates an event that happened before B, which
> happened before C, and so on).
> For each observation:
> X = 1 if at least 3 "consecutive" vars from A-E are equal to 1.
> Y = 1 if at least 2 "consecutive" vars from A-E are equal to 1.
> Z = 1 if only 1 or 2 "non-consecutive" vars from A-E are equal to 1.
> I could do this the long way with repeated gen and replace commands
> but I'm sure there's a better way to do it (only I don't know how).
*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index