Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: programming question


From   "David E Moore" <davem@hartman-group.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: RE: RE: programming question
Date   Wed, 23 Jul 2003 14:38:01 -0700

Double indirection.  Very clever, Nick.

I wonder whether looping over observations is faster than looping over
variables, however.  I seem to recall from some distant discussion that "in" is
very efficient, so Nick's solution may be the quickest way around.  Still, for
what it's worth (and with apologies to those who've moved beyond version 6.0),
here's a way to loop over variables to solve John's problem.

gen newvar = strvar=="aaa" & aaa==1
unab vlist: bbb - ccc
tokenize "`vlist'"
while "`1'" != "" {
   replace newvar = 1 if strvar=="`1'" & `1'==1
   mac shift
}


This is crude, but transparent.  Of course, the names would have to be changed
to get the desired effect.

> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Nick Cox
> Sent: Wednesday, July 23, 2003 12:42 PM
> To: statalist@hsphsun2.harvard.edu
> Subject: st: RE: programming question
>
>
> John Baughman
> >
> > I have a fair amount of experience using Stata
> > interactively, but little
> > programming experience, and I now have run into a problem
> > that can only be
> > solved with programming (I think).  Any help with how I
> > ought to think
> > about it would be greatly appreciated.  Thus, my first post to the
> > list.  By the way, I'm using Intercooled Stata 8.1.
> >
> > The dataset has a string var whose values correspond to the
> > names of dummy
> > variables in the dataset.  What I want to do is generate a
> > new dummy var
> > which equals 1 for a given observation if the dummy for the
> > var whose name
> > corresponds to that observation's string value also equals
> > 1; the new dummy
> > will be 0 otherwise.  I think an illustration of what the
> > data might look
> > like will help more than that convoluted description:
> >
> > obs     strvar  aaa     bbb     ccc     newvar
> > 1       aaa     1       1       1       1
> > 2       ccc     1       0       1       1
> > 3       bbb     1       0       1       0
> > 4       ccc     0       0       1       1
> > 5       aaa     0       0       0       0
> >
> > etc etc in various permutations.
> >
> > The actual dataset has fifty string values and fifty corresponding
> > dichotomous vars and around 5500 observations.  Any ideas
> > about how I ought
> > to think about this and/or what commands might be useful in
> > getting this
> > coded?  I sense there is something simple I'm missing . . .
> >
>
> gen newvar = .
> forval i = 1/`= _N' {
> 	replace newvar = `= strvar[`i']' == 1 in `i'
> }
>
> Nick
> n.j.cox@durham.ac.uk
>
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
>

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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