One more simplification may be in order, you can get rid of the
four -recode- statements and the original -gen- for domlt`i' and instead use
something like this in the loop:
gen byte dom1t`i' =inrange(`a',1,5)*inrange(`w',1,5)*(1 + 2*(`a'>3) +
(`w'>3) )
If this does not provide "readable" enough code, you could also redo your
conditions using a few cond() statements in a single generate.
In general, you should just use -replace- ( or in this case -gen-) if all
you want to do is a single replacement -- recode is used for more
complicated situations like multiple replacements.
Michael Blasnik
[email protected]
----- Original Message -----
From: "Nick Cox" <[email protected]>
To: <[email protected]>
Sent: Tuesday, March 23, 2004 8:08 AM
Subject: st: RE: Help with arrays
> I replied earlier on the specific question.
>
> Here's a comment on a different issue, exploiting -forval-
> to avoid -which-. Joseph's code looks fine,
> apart from the question of macro delimiters, but here's
> another version:
>
> forval i = 1/7 {
> gen dom1t`i' = 0
> local s : word `i' of 5 10 15 20 25 30 34
> local a "able`s'"
> local w "willing`s'"
> recode dom1t`i' 0=1 if ((`a'>0 & `a'<4) & (`w'>0 & `w'<4))
> recode dom1t`i' 0=2 if ((`a'>0 & `a'<4) & (`w'>3 & `w'<6))
> recode dom1t`i' 0=3 if ((`a'>3 & `a'<6) & (`w'>0 & `w'<4))
> recode dom1t`i' 0=4 if ((`a'>3 & `a'<6) & (`w'>3 & `w'<6))
> }
>
> Nick
> [email protected]
>
> Joseph McCrary
>
> > array that will recode a variable into one of four values based on the
> > combined values of two other variables. I've got to do this over 40
> > variables across four domains. Here is the code I am using
> > for the first
> > domain:
> >
> > gen dom1t1=0
> > gen dom1t2=0
> > gen dom1t3=0
> > gen dom1t4=0
> > gen dom1t5=0
> > gen dom1t6=0
> > gen dom1t7=0
> > local read "dom1t1 dom1t2 dom1t3 dom1t4 dom1t5 dom1t6 dom1t7"
> > local able "able5 able10 able15 able20 able25 able30 able34"
> > local willing "willing5 willing10 willing15 willing20 willing25
> > willing30 willing34"
> > local i = 1
> > local n: word count 'read'
> > while 'i' <= 'n' {
> > local var1: word 'i' of 'read'
> > local var2: word 'i' of 'able'
> > local var3: word 'i' of 'willing'
> > recode 'var1' 0=1 if (('var2'>0 & 'var2'<4) & ('var3'>0 &
> > 'var3'<4))
> > recode 'var1' 0=2 if (('var2'>0 & 'var2'<4) & ('var3'>3 &
> > 'var3'<6))
> > recode 'var1' 0=3 if (('var2'>3 & 'var2'<6) & ('var3'>0 &
> > 'var3'<4))
> > recode 'var1' 0=4 if (('var2'>3 & 'var2'<6) & ('var3'>3 &
> > 'var3'<6))
> > local i = 'i' + 1
> > }
> >
<snip>
*
* 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/