Bookmark and Share

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.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: RE: RE: looping to value of a variable


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: RE: looping to value of a variable
Date   Thu, 23 Feb 2012 10:24:51 +0000

Sorry, but I am still unclear on what flags you want. 

The fact that -maxFU- exists seems to be a red herring. You can create flags by 

forval j = 1/8 { 
	gen ismissing`j' = missing(dFU`j') 
} 

Or, if you want it the other way round, negate the function call with -!missing()-

But why do you need the flags at all? 

Even if I am misunderstanding you, which is quite likely, the small bit of Stata technique may be some help. 

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

Richard Fox

Hi Nick,

Yes you're correct, sorry for the confusion over DFU and FU. I added the egen function to illustrate where the loop count values could come from. In fact the values came from reshaping long data.

I want to flag missing dates, however, for each record I need to assess only to a certain point. These are missing follow-up forms in a medical scenario - if patients are only followed for a certain time then I can't record some forms as missing if the patient has reached that time-point.

Take the example below; for the 1st id I only want to loop to 3 to test for missing values. In the second id I only want to loop to 4, and so on. I suppose I could just only increment a counter if `i' <= maxFU. Just to note that the code within the loops (replace flag.....) was incomplete in my previous message - it was really just the form of the loop statements that I was interested in.

id 	dfu1		dfu2		dfu3		dfu4		dfu5		dfu6		dfu7		dfu8		maxFU
1	30/10/1910			08/02/1904											3
2	16/12/1908	24/01/1913			08/02/1904									4
3	04/09/1907	13/10/1911	21/11/1915	30/12/1919	07/02/1924	17/03/1928	25/04/1932			7
4	18/10/1914			08/02/1904	18/03/1908	26/04/1912	04/06/1916	13/07/1920	21/08/1924	8

I'll have a look at the reference.

Nick Cox

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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index