Dear Nick,
Thanks for this. It works a treat. Tokenize is a nice approach. I'd
only ever thought of it in the context of ado programs.
And thanks Richard re. -recode-. You're right about the -gen- option
working for multiple vars (which I should have realised from the
manual's syntax, even if there were no examples).
--
Kind regards,
Ian
=====================
Ian Watson
Senior Researcher
acirrt
University of Sydney
NSW 2006
Australia
======================
Monday, June 7, 2004, 10:48:38 PM, you wrote:
NC> In two cases here a``i'' should be a_``i''
NC> Nick
NC> [email protected]
>> -----Original Message-----
>> From: Nick Cox
>> Sent: 07 June 2004 11:30
>> To: '[email protected]'
>> Subject: RE: Re[3]: st: Problem with foreach loops
>>
>>
>> I think you just have your source and target confused.
>>
>> local i=1
>> foreach v in ajbmspay ajbmssec ajbmswrk ajbmshrs ajbmsflx ajbmsall {
>> local n : word `i' of pay security work hours flexibility overall
>> recode `v' (-4 -3 -1 =.), gen(a_`n')
>> la var a_`n' "Satisfaction with `n'"
>> local i=`i'+1
>> }
>>
>> Note that this is just one answer.
>>
>> If you know -tokenize-, you can
>>
>> tokenize pay security work hours flexibility overall
>> local i=1
>> foreach v in ajbmspay ajbmssec ajbmswrk ajbmshrs ajbmsflx ajbmsall {
>> recode `v' (-4 -3 -1 =.), gen(a_``i'')
>> la var a``i'' "Satisfaction with ``i''"
>> local i=`i'+1
>> }
>>
>> and the incrementation can also be done this way
>>
>> tokenize pay security work hours flexibility overall
>> local i=1
>> foreach v in ajbmspay ajbmssec ajbmswrk ajbmshrs ajbmsflx ajbmsall {
>> recode `v' (-4 -3 -1 =.), gen(a_``i'')
>> la var a``i'' "Satisfaction with ``i++''"
>> }
>>
>> In this instance, I think this last version
>> is not as clear as the previous one.
>>
>> Nick
>> [email protected]
>>
>> Ian Watson
>>
>> > As a follow up to my last email, I've found the answer to
>> this problem
>> > using -foreach-. Courtesy of Nick Cox's article in State
>> Journal 3(2)
>> > 2003, the trick is to step through a non-numerical list using "word
>> > [num] of".
>> >
>> > Here is the code that works:
>> >
>> >
>> > local i=1
>> > foreach v in ajbmspay ajbmssec ajbmswrk ajbmshrs ajbmsflx ajbmsall {
>> > local n : word `i' of pay security work hours flexibility overall
>> > recode `v' (-4 -3 -1 =.), gen(a_`n')
>> > la var `v' "Satisfaction with `n'"
>> > local i=`i'+1
>> > }
>> >
>> >
>> > There remains, however, still one bug. The label variables
>> command is
>> > not being executed, and the recode command's own variable
>> > label remains
>> > attached to the new variables. Does anyone have any thoughts?
>>
NC> *
NC> * For searches and help try:
NC> * http://www.stata.com/support/faqs/res/findit.html
NC> * http://www.stata.com/support/statalist/faq
NC> * http://www.ats.ucla.edu/stat/stata/
Richard wrote:
Perhaps I misunderstand you, but isn't this example from the online docs
(which might be more current than the manual) what you mean?
recode x1 x2 (1 2 3/5=1) (6/10=2), gen(nx1 nx2)
In the case of your original example (following is meant to be all one line)
recode ajbmspay ajbmssec ajbmswrk ajbmshrs ajbmsflx ajbmsall(-4 -3 -1=.),
gen(a_pay a_security a_work a_hours a_flexibility a_overall)
*
* 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/