EDIT: just realised copied pasted code doesnt show indentation- have also uploaded a image of my code..
Background: I am in the process of creating a by infection by visit database (HPV infections- has different stain types). As part of it, i have to create certain clearance variables to calculate time to clearance (like clearance date and clearance visit). in my code, i have a loop in a loop. When i run the code, everything runs perfectly until the second loop, which stata refuses to recognise/ run. Im not sure if its a syntax or indentation or some other issue...
I have also tried alternative ways like writing date and visit as "date" "visit". It always stops right before the second loop- foreach y in date visit {
I have bolded the 2 loops.
note: the 1,2,3 are referring to 3 separate clearance definitions that we are looking at.
CODE:
foreach x in 1 2 3 {
gen clear`x'=.
if `x'==1 {
bys ID strain_type: replace clear`x'=1 if Result==0 & Result[_n-1]==1
continue
}
else if `x'==2 {
bys ID strain: replace clear`x'=1 if Result==0 & Result[_n-1]==1 & Result[_n+1]==0
continue
}
else if `x'==3 {
bys ID strain_type: replace clear`x' =1 if endrow[_n+1]==1 & Result==0 & Result[_n+1]==0 & inlist(infection, "Prevalent", "Incident")
continue
}
gen firstclear`x'=.
bys ID strain_type: gen sumclear`x'=sum(clear`x')
bys ID strain_type: replace firstclear`x'=1 if clear`x'==1 & sumclear`x'==1
drop clear`x' sumclear`x'
rename firstclear`x' clear`x' //IT RUNS UNTIL HERE
foreach y in date visit {
if y==visit {
bys ID strain_type: gen clear`x'`y'= Visit if firstclear`x'==1
continue
}
else if y==date {
bys ID strain_type: gen clear`x'`y'= Oral_Date if firstclear`x'==1
continue
}
bys ID strain_type: replace clear`x'`y' = clear`x'`y'[_n+1] if clear`x'`y'==.
bys ID strain_type: replace clear`x'`y' = clear`x'`y'[_n+1] if clear`x'`y'==.
bys ID strain_type: replace clear`x'`y' = clear`x'`y'[_n+1] if clear`x'`y'==.
bys ID strain_type: replace clear`x'`y' = clear`x'`y'[_n-1] if clear`x'`y'==.
}
bys ID strain_type: gen timetoclear`x'=clear`x'date-firstinfdate
}
Background: I am in the process of creating a by infection by visit database (HPV infections- has different stain types). As part of it, i have to create certain clearance variables to calculate time to clearance (like clearance date and clearance visit). in my code, i have a loop in a loop. When i run the code, everything runs perfectly until the second loop, which stata refuses to recognise/ run. Im not sure if its a syntax or indentation or some other issue...
I have also tried alternative ways like writing date and visit as "date" "visit". It always stops right before the second loop- foreach y in date visit {
I have bolded the 2 loops.
note: the 1,2,3 are referring to 3 separate clearance definitions that we are looking at.
CODE:
foreach x in 1 2 3 {
gen clear`x'=.
if `x'==1 {
bys ID strain_type: replace clear`x'=1 if Result==0 & Result[_n-1]==1
continue
}
else if `x'==2 {
bys ID strain: replace clear`x'=1 if Result==0 & Result[_n-1]==1 & Result[_n+1]==0
continue
}
else if `x'==3 {
bys ID strain_type: replace clear`x' =1 if endrow[_n+1]==1 & Result==0 & Result[_n+1]==0 & inlist(infection, "Prevalent", "Incident")
continue
}
gen firstclear`x'=.
bys ID strain_type: gen sumclear`x'=sum(clear`x')
bys ID strain_type: replace firstclear`x'=1 if clear`x'==1 & sumclear`x'==1
drop clear`x' sumclear`x'
rename firstclear`x' clear`x' //IT RUNS UNTIL HERE
foreach y in date visit {
if y==visit {
bys ID strain_type: gen clear`x'`y'= Visit if firstclear`x'==1
continue
}
else if y==date {
bys ID strain_type: gen clear`x'`y'= Oral_Date if firstclear`x'==1
continue
}
bys ID strain_type: replace clear`x'`y' = clear`x'`y'[_n+1] if clear`x'`y'==.
bys ID strain_type: replace clear`x'`y' = clear`x'`y'[_n+1] if clear`x'`y'==.
bys ID strain_type: replace clear`x'`y' = clear`x'`y'[_n+1] if clear`x'`y'==.
bys ID strain_type: replace clear`x'`y' = clear`x'`y'[_n-1] if clear`x'`y'==.
}
bys ID strain_type: gen timetoclear`x'=clear`x'date-firstinfdate
}
Comment