Stata模拟编程求助:导入Excel时保留内存随机数的方法
Hey there! Let's work through this Stata error you're hitting. That no; data in memory would be lost r(4) message pops up because when you run import excel, Stata tries to overwrite the dataset currently in memory (which has your random knots) with the Excel data—and it's warning you about losing that existing data. Here are a few straightforward solutions to keep your random knot values and successfully load your Excel data:
Solution 1: Add clear to import excel (Simplest Fix)
Since you've already extracted your random knot values into the local macro list_knots (using levelsof), you don't actually need the vals variable in memory anymore. Adding the clear option tells Stata you intentionally want to replace the current in-memory data with your Excel file, skipping the warning.
Here's your modified code:
* Create list of specified knots local spec_knots 0.5 1 2 84.5 * Create random knots between 3 and 50 set obs 3 gen vals = floor(3 + 47 * runiform()) * Save levelsof vals and save to local to join specified and random knots levelsof vals, local(values) * Join specified and random knots to include in the mvrs command local list_knots `spec_knots' `values' * Use clear to overwrite in-memory data with Excel data import excel file, sheet("Analysis") firstrow clear mvrs glm deaths age, family(poisson) exposure(population) all scale(1) alpha(1) knots(`list_knots')
Solution 2: Save Random Knots to a Temporary File (If You Need Them Later)
If you think you'll need the vals variable later in your workflow, save it to a temporary file before importing Excel, then reload it afterward:
* Create list of specified knots local spec_knots 0.5 1 2 84.5 * Create random knots between 3 and 50 set obs 3 gen vals = floor(3 + 47 * runiform()) * Save levelsof vals and save to local to join specified and random knots levelsof vals, local(values) * Join specified and random knots to include in the mvrs command local list_knots `spec_knots' `values' * Save random knots to a temporary file tempfile knot_data save `knot_data', replace * Import Excel data import excel file, sheet("Analysis") firstrow * Reload the random knots data (adjust merge/append based on your needs) use `knot_data', clear // or `append using `knot_data'' if combining datasets mvrs glm deaths age, family(poisson) exposure(population) all scale(1) alpha(1) knots(`list_knots')
Optimized Solution for Your Simulation Workflow
Since you're wrapping this into a simulation program, you can save time by loading your Excel data once outside the simulation loop instead of importing it every time. This avoids repeated file reads and the memory error entirely:
* First, load and save your Excel data once (outside the simulation) import excel file, sheet("Analysis") firstrow tempfile master_data save `master_data', replace * Define your simulation program capture program drop knots_location program define knots_location * Create list of specified knots local spec_knots 0.5 1 2 84.5 * Generate random knots set obs 3 gen vals = floor(3 + 47 * runiform()) * Extract random knot values to local macro levelsof vals, local(values) local list_knots `spec_knots' `values' * Load the pre-saved Excel data use `master_data', clear * Run mvrs command mvrs glm deaths age, family(poisson) exposure(population) all scale(1) alpha(1) knots(`list_knots') * Extract fit statistics for simulation scalar aic = e(aic) scalar bic = e(bic) scalar dev = e(deviance) end * Run the simulation simulate aic=aic bic=bic dev=dev, reps(10) nodots: knots_location
This approach is much more efficient for repeated simulations, and you won't run into the memory loss error because we're loading a pre-saved dataset instead of importing Excel each time.
内容的提问来源于stack exchange,提问作者psoares




