Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# st: RE: looping to value of a variable

 From Nick Cox To "'statalist@hsphsun2.harvard.edu'" Subject st: RE: looping to value of a variable Date Wed, 22 Feb 2012 17:23:14 +0000

```Your example is not very clear. You have FU* and by implication DFU*. Do you want to flag missings or non-missings? I can read your post either way.

However, you (almost surely) do not need to loop over observations. It is sufficient to loop over variables.

See a review in this territory

SJ-9-1  pr0046  . . . . . . . . . . . . . . . . . . .  Speaking Stata: Rowwise
(help rowsort, rowranks if installed) . . . . . . . . . . .  N. J. Cox
Q1/09   SJ 9(1):137--157
shows how to exploit functions, egen functions, and Mata
for working rowwise; rowsort and rowranks are introduced

Nick
n.j.cox@durham.ac.uk

Richard Fox
I want to loop to the value of a variable. Let's say I have generated the number of non-missing values in a row of data (maxFU in example below). I want to loop to that value which clearly can differ between records.

The following does the job but feels like cheating.

egen maxFU = rownonmissing(FU1 FU2 FU3 FU4 FU5 )

count
local N=r(N)
forvalues i = 1/`N' {
local sp = maxFU[`i']
forvalues j=1/`sp'	{
qui replace flag`j'=1 if DFU`j'==.
}
}

There must be a simpler way; any ideas?

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```