* Closes a log named read_ad_rev if it is already open. * capture suppresses any error message if the log does not exist. capture log close read_ad_rev * Removes any dataset currently in memory. clear * Prevents Stata from pausing output one screen at a time. set more off * Sets the output width to 255 characters. set linesize 255 * Opens the combined NSDUH Stata dataset containing the individual data from 2002 to 2012. use "F:\Heroin\short0212.dta", clear * Produces a frequency table showing the number of records for each survey year. tab1 year * Number of at-risk individuals for heroin initiation tab1 year if ELIGHER2==1 * Shows the unweighted count of respondents classified as past-year heroin initiates. table year if RECHER2==1 * Creates a log file. * Replace overwrites an existing file of the same name. * All subsequent output is written to the log. log using "F:\heroin\read_ad_rev.log", name (read_ad_rev) replace * Estimate total heroin initiates (svy total) by year from the individal yearly * data files that are extracted from combined 11 years of data files * The foreach loop goes through survey years 2002 to 2012. * For each year, reload short0212.dta. * keep if (year==`x') retains only observations from that year (e.g., 2002). * compress optimizes variable storage types to reduce file size. * save "F:\Heroin\short_nsduh_`x'.dta", replaces saves the annual dataset (e.g., short_nsduh_2002.dta). * use "F:\Heroin\short_nsduh_`x'.dta", clear loads the newly created single-year dataset. * svyset specifies the complex survey design. * verep is the variance estimation replicate or PSU variable. * [pweight=analwt] is the analysis weight. * strata (vestr) is the stratification variable. * dof(900) uses 900 degrees of freedom for variance estimation. * singleunit(missing) handles strata with only one PSU by treating variance as missing rather than using certainty or scaled adjustments. * di %~59s "NSDUH:" `x' prints a heading such as:NSDUH: 2002 before the survey estimate. * svy: total RECHER2 computes a survey-weighted population total of those who initiated heroin use during the past year (RECHER2=1). * The output includes:Estimated total, Standard error, t statistic, Confidence interval with proper adjustment for the complex survey design. * est store total`x' stores each year's results in memory under names such as:total2002... total2012. * The // significance ...part is commented out, preventing execution. * log close read_ad_rev closes the log file and writes output to disk. * exit terminates the Stata session. foreach x of numlist 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 { use "F:\Heroin\short0212.dta", clear keep if (year==`x') compress save "F:\Heroin\short_nsduh_`x'.dta", replace use "F:\Heroin\short_nsduh_`x'.dta", clear svyset verep [pweight=analwt],strata (vestr) dof(900) singleunit(missing) di %~59s "NSDUH:" `x' svy: total RECHER2 est store total`x' } //significance tests not performed and suppression rules not performed log close read_ad_rev exit