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

Re[2]: st: Problem with foreach loops


From   Ian Watson <[email protected]>
To   Richard Williams <[email protected]>
Subject   Re[2]: st: Problem with foreach loops
Date   Mon, 7 Jun 2004 18:35:18 +1000

Dear Richard,

  Thanks for that. You are right, all six vars have identical values.
  The mistake is that I've implemented a nested loop, whereas I should
  have been using a parallel loop.

  The examples of parallel loops I've seen (mostly courtesy of Nick
  Cox) use a local macro which increments inside the loop, something
  which won't work here because my names are not numerically based.

  At the risk of revisiting a brief discussion held on the list during
  early April, I've dug out my old favourite (-for-) and found a way of
  implementing my task as follows:

begin snippet==========
  
for var ajbmspay ajbmssec ajbmswrk ajbmshrs ajbmsflx ajbmsall ///
     \ any pay security work hours flexibility overall: ///
     recode X (-4 -3 -1=.), gen(a_Y)

for var a_pay-a_overall \ any pay security work hours flexibility overall: ///
     la var X "Satisfaction with Y"

end snippet============

This code works fine, but no doubt, there is a way of condensing these
two statements into one, and no doubt, there is also a way of using
the `modern' foreach to achieve the task. I will keep plugging away at
this.

Richard, you're right also, it could be done easily with brute
force, but I'm trying to wean myself of -for- in favour of -foreach-
and so was experimenting with an approach which can always be
generalised to large numbers of variables (where brute force is not an
option).

I've also checked -recode-, and while the manual shows how multiple
variables can be recoded in one statement, there is no example of how
one might implement the -gen- option for multiple variables. Perhaps
someone on the list knows the trick?
     

Thanks again for a prompt response at a late hour in your neck of the
woods.

-- 
Kind regards,
 Ian                          

=====================
Ian Watson
Senior Researcher
acirrt
University of Sydney
NSW 2006
Australia
======================

Monday, June 7, 2004, 4:31:39 PM, you wrote:

RW> At 03:36 PM 6/7/2004 +1000, you wrote:
>>Dear statalist,
>>
>>   Can anyone spot the mistake with the following snippet of code.
>>
>>   It actually works fine, and recodes and labels the variable
>>   in the way I wish, but in exiting (?) the loops it runs into problems.
>>
>>   Any advice would be most appreciated.

RW> I believe what is happening is that ajbmspay gets recoded 6 times, creating
RW> new variables a_pay, a_security, etc.  These 6 vars all have identical
RW> values.  The outer loop then moves on to ajbmswrk, and tries to recode it
RW> into a_pay.  Because a_pay already exists, you get an error message.  In
RW> short, the new vars may have the labels you want, but they don't have the
RW> values; all 6 are just recodes of ajbmspay.

RW> In SPSS, I would do this with DO REPEAT commands.  My Stata programming
RW> isn't as strong, but since I may be one of the few members of the list who
RW> happens to be awake right now I'll offer some possible workarounds:

RW> foreach x in pay sec wrk hrs flx all {
RW>     recode ajbms`x'(-4 -3 -1=.), gen(a_`x')
RW>     la var a_`x' "Satisfaction with `x'"
RW> }

RW> I think this does the recodes right, but doesn't give you the var names or
RW> exact labels that you wanted.  Better programmers than I can probably tell
RW> you a better solution.  On the other hand, it is only 6 variables, so
RW> unless you need to do this sort of thing routinely it may be easier just to
RW> use brute force to write the handful of lines required to do this without
RW> looping.

RW> Or,

RW> recode  ajbmspay ajbmssec ajbmswrk ajbmshrs ajbmsflx ajbmsall(-4 -3 -1=.),
RW> gen(a_pay a_security a_work a_hours a_flexibility a_overall)
RW> foreach y in pay security work hours flexibility overall{
RW> la var a_`y' "Satisfaction with `y'"
RW> }


RW> -------------------------------------------
RW> Richard Williams, Notre Dame Dept of Sociology
RW> OFFICE: (574)631-6668, (574)631-6463
RW> FAX:    (574)288-4373
RW> HOME:   (574)289-5227
RW> EMAIL:  [email protected]
RW> WWW (personal):    http://www.nd.edu/~rwilliam
RW> WWW (department):    http://www.nd.edu/~soc

RW> *
RW> *   For searches and help try:
RW> *   http://www.stata.com/support/faqs/res/findit.html
RW> *   http://www.stata.com/support/statalist/faq
RW> *   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index