Hi, I have a large dataset on course enrollment. Individual students take courses in different semesters. Observations are unique at the individual-semester-coursenum level. Individuals also have different graduation years ("cohort"). I would like to choose, for each individual, a random sample of individuals in their cohort of a different size ("total") that is different for each individual. The best possible way I can think of is to loop through the individual observations and use the randomtag command, and create a unique identifer for each value of random tag (possibly the unique identifer of the student) - so for example, I could use the following commands:
preserve
keep id cohort total
duplicates drop /* We now have one observation per individual */
sort id
local N=_N
set seed 1357
forvalues i = 1/`N' {
local id = id[`i']
local year = cohort[`i']
local groupsize = total[`i']
randomtag if cohort == `year', count(`groupsize') g(selected)
g randomgroup = .
replace randomgroup = `id'*selected
}
sort id
save randomgroups.dta
restore
sort id
merge id using randomgroups.dta
I'm wondering if there is a faster way to do this, rather than looping over individual observations to generate random samples one at a time. Thank you for your suggestions.
Related Posts with Drawing random sample from a large data set for each observation
Panel data with averaged observationsHello everyone. I am new at STATA and struggling to come up with a proper methodology. I have a pane…
Looping over two localsHello, I'm endeavoring to loop over two locals. The first local is made up variables consisting of …
Different Means in Policy StancesFor a paper project I am looking at whether Evangelicals have different means in their policy stance…
Using loop to import, edit, then export .csv filesHello, I am importing a large number of .csv files into Stata. Before I can import the files, I nee…
Predicting residuals from instrumental regressionModel : ivreghdfe itotf (qt = lagqt lagcfo) cfo, absorb(gvkey fyear) endog(qt) cluster(gvkey) resid …
Subscribe to:
Post Comments (Atom)
0 Response to Drawing random sample from a large data set for each observation
Post a Comment