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

st: RE: Help with arrays


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Help with arrays
Date   Tue, 23 Mar 2004 13:08:11 -0000

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 
n.j.cox@durham.ac.uk 

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
>        }
> 
>        But when I run the code as a do file I get the following error
> message:
> 
> 
>        . 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' {
>          2.   local var1: word 'i' of 'read'
>          3.   local var2: word 'i' of 'able'
>          4.   local var3: word 'i' of 'willing'
>          5.   recode 'var1' 0=1 if (('var2'>0 & 'var2'<4) & 
> ('var3'>0 &
> 'var3'<4))
>          6.   recode 'var1' 0=2 if (('var2'>0 & 'var2'<4) & 
> ('var3'>3 &
> 'var3'<6))
>          7.   recode 'var1' 0=3 if (('var2'>3 & 'var2'<6) & 
> ('var3'>0 &
> 'var3'<4))
>          8.   recode 'var1' 0=4 if (('var2'>3 & 'var2'<6) & 
> ('var3'>3 &
> 'var3'<6))
>          9.   local i = 'i' + 1
>         10. }
>        'i' invalid name
>        r(198);
> 
>        Why is it telling me that 'i' is an invalid name. I'm 
> relying on Bill
> Gould's explanation on using SAS-like arrays from
> http://www.stata.com/support/faqs/data/arrays.html

*
*   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