Hi everyone,
I have a dataset with 14 million observations and 22 variables. The variables of interest for this question are as follows:
presonid: unique identifier for a person (not unique in the dataset, but uniquely identifies a person)
konisert: (0/1) a treatment has been carried out true or false.
screeningprove: (0/1): a sample can be classified as either a screening sample or not (in which case it is a follow-up sample), true or false
proveDato: sample date.
Each observation is a sample and these can be grouped by a unique person id. Each person has one to many samples(=observations), so the number of observations per person vary. I would now like to set the variable "screeningprove" to either 0 or 1 based on the occurrence of another variable being set to true "konisert ==1" in a time window of 10 years before the sample in question. This is to be done by personid.
I have tried the following:
*generate a sampleid within personid
bysort personid (proveDato): egen provenr = seq()
*generate the maximum number of samples per person
egen maxprovenr = max(provenr), by(personid)
*create an inner loop; for testing purposes keep just one personid
keep if personid == 100000493
local maxprovenr = maxprovenr[_n]
forval f = 1/ ‘maxprovenr’ {
replace screeningprove = 0 if konisert[_n-‘f’] == 1 & proveDato -proveDato[_n-‘f’] <3650
}
This seems to behave as expected.
I thought i could now nest this loop within another loop that would carry out this inner lopp for each personid. But this is where I can't wrap my head around how to do it. Is this possibly not at all the right approach to this problem?
cheers, Linn
Related Posts with nested loop for cycling over observations by group
Programming dummyI posted this question in a response to a topic earlier, but I am not sure whether that is the right…
Calculating Standard Deviation of the ResidualsHi all, I ran an ECM and after this, I want to inspect the Standard Deviation of the Residuals plot …
Clopper-Pearson Confidence Intervals with Complex Survey DataHello, I am currently working with complex survey data in Stata14 and would like to calculate exact…
Create a table with sectoral share valuesDear all, I am currently working with a dataset that contains information on quarterly gross value …
median survival time using interval censored dataHi statalisters, I'm looking to estimate the median survival time (plus IQR) overall for my interva…
Subscribe to:
Post Comments (Atom)
0 Response to nested loop for cycling over observations by group
Post a Comment