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

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
RE: st:easy way to deal with "paired" variables? |

Date |
Mon, 2 Feb 2009 16:48:02 -0000 |

Maarten gave very good advice. It's perhaps worth pointing out as well that it is possible to work "row-wise", which is the title of my next Speaking Stata column in Stata Journal 9(1) (2009). The topic was sparked by several postings last month on this list. You could loop over the variables, say like this: gen doingtime = 0 qui foreach r of var reason* { local g : subinstr local r "reason" "gap" replace doingtime = doingtime + cond(`r' == 4 & !missing(`g'), `g', 0) } Or suppose you know in advance that there are reason1-reason42, gap1-gap42. gen doingtime = 0 qui forval i = 1/42 { replace doingtime = doingtime + cond(reason`i' == 4 & !missing(gap`i'), gap`i', 0) } The main trickiness that can be guessed from Mandy's example is the possibility of missing values. That's tackled above. Nick n.j.cox@durham.ac.uk Maarten buis --- Mandy fu <mandy.fu1@gmail.com> wrote: > The data set I'm working on includes 100 variables measuring the time > spell of gaps between jobs. And for each gap there's a variable > explaining why the person does not work during that gap. I want to > calculate the total length of any gaps caused by being in > prison(reason=4) . Problems like these can sometimes become a lot easier when the data is transformed from a wide format to a long format. As you seem to want to avoid creating a prison dummy, I used in the example below a logical statement (reason==4) inside the -total- command. Logical statements evaluate to 1 if true and 0 if false, so this statement plays the same role as the dummies in your method. *-------------- begin example -------------------- drop _all input id gap1 reason1 gap2 reason2 1 30 1 20 2 2 25 2 180 4 end list reshape long gap reason, i(id) j(spell) list bys id : egen prisontime = total((reason==4)*gap) list *----------------- end example ------------------- (For more on how to use examples I sent to the Statalist, see http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html ) * * 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/

**References**:**st:easy way to deal with "paired" variables?***From:*Mandy fu <mandy.fu1@gmail.com>

**Re: st:easy way to deal with "paired" variables?***From:*Maarten buis <maartenbuis@yahoo.co.uk>

- Prev by Date:
**RE: -varabbrev- setting [was: st: re: question about "testparm"]** - Next by Date:
**st: stdes, SD for time at risk?** - Previous by thread:
**Re: st:easy way to deal with "paired" variables?** - Next by thread:
**st: SSC Archive activity, Jan 2009** - Index(es):

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