Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <njcoxstata@gmail.com> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
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 1s. Either way, try gen Z = inlist(A + B + C + D + E, 1, 2) & !Y Nick njcoxstata@gmail.com On 25 June 2013 17:46, Hitesh Chandwani <hchandwani.stata@gmail.com> 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: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/