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

Re[3]: st: Problem with foreach loops


From   Ian Watson <i.watson@econ.usyd.edu.au>
To   Ian Watson <statalist@hsphsun2.harvard.edu>
Subject   Re[3]: st: Problem with foreach loops
Date   Mon, 7 Jun 2004 19:02:54 +1000

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?


  

-- 
Kind regards,
 Ian                          

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

Monday, June 7, 2004, 6:35:18 PM, you wrote:

IW> Dear Richard,

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

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

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

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

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

IW> end snippet============

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

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

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

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


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