That's very clear.
As earlier suggested, you are in general better off with a -reshape- for
this kind of data, but in this particular case, the problem is highly
tractable row-wise.
egen dgc = concat(dgc_?_1b_cde)
gen byte chronic = strpos(dgc,"011") > 0
So, you just push all the zeros and ones into a string variable and look
for the substring "011".
This could made a one-liner, with some loss of clarity.
Either way, no loops, no nesting.
Nick
[email protected]
David Sabapathy
Thank you very much for your reply. You are correct in that I am
figuring out how Stata works relative to some other software I used eons
ago. I also have a gut instinct this is not the way to go. A sample of
my dataset is as follows:
Obs dgc1_1b_cde dgc2_1b_cde dgc3_1b_cde dgc4_1b_cde dgc5_1b_cde
dgc6_1b_cde dhc1_age dhc2_age dhc3_age ..... dhc6_age chronic
1 0 1 1
1 0 1 20
22 24 30 .
2 0 1 0
1 0 1 66
68 70 76 .
3 0 0 0
0 1 1 34
36 38 44 .
4 1 1 0
1 0 1 42
44 46 52 .
... N>15000
Each observation represents one individual's responses to a medication
survey administered 6 consecutive times or cycles. The "dgc" variables
represent a binary response to whether this individual is taking a
certain medication (1=yes). The fourth character in the "dgc" variables
e.g. dgc"1" represents the cycle number. e.g. the first individual did
not take the medication in cycle 5 (dgc5_1b_cde==0) but did in cycle 6
(dgc6_1b_cde==0). I am defining incident chronic usage of this
medication as the patient not taking the medication in any cycle i
followed by taking the medication for two consecutive cycles i+1 and
i+2. That is you need a sequence of three cycles starting with a "0"
and followed by two "1s". Above, observations 1 and 3 qualify as being
chronic incident usage of the medication, 2 and 4 do not. The "dhc"
variables in my code are just the individuals age. I thought of setting
the "chronic" variable if the incidient chronic usage condition was met
but it ce!
rtainly looks like a new approach is needed.
David Sabapathy
________________________________________
From: [email protected]
[[email protected]] On Behalf Of Nick Cox
[[email protected]]
Sent: July 20, 2009 10:59 AM
To: [email protected]
Subject: st: RE: Nested loops by observation
My visceral instinct, fortified by some Stata experience, is that this
is not the way to go at all. That is, I guess that your second sentence
is quite wrong: you don't need to do it this way at all.
This looks like the code of someone experienced in some alternative
software, but less so in Stata. In Stata, loops over observations are
rarely needed given the scope for using -by:- and _n.
Give an example of your data and a precise definition of chronic
condition and I will try to advise further. (I'm not going to try to
infer your definition from your code.)
Nick
[email protected]
David Sabapathy
I have a question related to the FAQ "if command versus if qualifier".
For each observation in a dataset I need to perform a series of nested
loops ("forvalues" / "if") on a longitudinal variable set to evaluate
whether a "chronic" medication condition exists. That is, each
observation contains multiple years of survey information (the dgc*
variables below) which is being analyzed and if a chronic condition
exists a new variable "chronic" is set to 1. I can't use the nested
commands as shown below in a simplified version of the code as they only
work on the first observation. However I also can't use this nested
code as a qualifier (i.e. replace chronic = 1 if ...) as syntax does not
appear to allow this type of nesting. Is there anyway I can step
through observations one by one with this nested code? Any other
possible solutions?
Thank you for any help you can provide,
David
gen chronic = .
forvalues cycle = 1(1)4 {
if dgc`cycle'_1b_cde == 0 { // if no med use
cycle i
local chron_cyc1 = `cycle' + 1
if dgc`chron_cyc1'_1b_cde == 1 { // if med use cycle_i+1
local chron_cyc2 = `cycle' + 2
if dgc`chron_cyc2'_1b_cde == 1 { // if med use cycle_i+2
if dhc`chron_cyc1'_age >=65 & dhc`chron_cyc2'_age
>=65 {
// "replace chronic = 2"
}
else {
// "replace chronic = 1"
}
}
}
}
}
*
* 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/