Hey, so I'm writing a command for synthetic controls, and I want to create a variable representing time relative to treatment. Let's look at my treatment and counterfactual dataset to see what I mean, constructed from Abadie's proposition 99 dataset.
Okay, so now we have the observed outcomes for California (packspercapita3) versus the potential outcomes (cf) imputed by SCUL, the command I've written.
What I want, is to create a variable relative to the time the intervention begins. So, it would have 1988 = -1, 1989 = 0, 1990 = 1, and so on and so forth.
I know there's likely a simple way to do this, I've just never had to do it before.
if it helps at all, here's the main text of my program, as well as the subroutine where I would do this step in.
And the sub-routine est_lasso. Note, I want to generate the time-to-event variable at the bottom where I've kept the year, intervention, and predicted counterfactual data, where I write
Here's the routine:
Code:
* Example generated by -dataex-. For more info, type help dataex clear input int year float packspercapita3 double cf 1970 123 122.19726134054034 1971 121 121.60684452804827 1972 123.5 123.61810115195998 1973 124.4 124.10491916225804 1974 126.7 126.78590055138683 1975 127.1 126.67481467170963 1976 128 128.0089463866536 1977 126.4 126.5537778451874 1978 126.1 125.4280714477338 1979 121.9 122.37068825118985 1980 120.2 119.82692177230953 1981 118.6 119.10198128501125 1982 115.4 115.60891910107874 1983 110.8 110.9889278433448 1984 104.8 104.64746596103345 1985 102.8 102.73724278561677 1986 99.7 99.36503988533293 1987 97.5 97.36699298640269 1988 90.1 91.00718304320216 1989 82.4 88.57593642265928 1990 77.8 85.22012650464623 1991 68.7 80.99865923091724 1992 67.5 79.31463033963786 1993 63.4 79.73996773485305 1994 58.6 77.188796304251 1995 56.4 74.5961461880601 1996 54.5 73.92071179502868 1997 53.8 73.9526331950957 1998 52.3 71.70392309654518 1999 47.2 71.12642951460154 2000 41.6 66.0358503449446 end format %ty year
What I want, is to create a variable relative to the time the intervention begins. So, it would have 1988 = -1, 1989 = 0, 1990 = 1, and so on and so forth.
I know there's likely a simple way to do this, I've just never had to do it before.
if it helps at all, here's the main text of my program, as well as the subroutine where I would do this step in.
Code:
cap prog drop scul // Drops previous iterations of the program *! SCUL v1.0.0, Jared Greathouse, 1/2/22 prog scul * Installs needed commands loc package st0594 foreach x of loc package { // begin foreach qui: cap which cvlasso if _rc { // if command is missing qui: net inst `x'.pkg, replace } // ends if } // ends foreach loc comm distinct gtools labvars foreach x of loc comm { // begin foreach qui: cap which `x' if _rc { // if command is missing qui: ssc inst `x', replace } // ends if } // ends foreach qui: xtset gl time: disp "`r(timevar)'" gl panel: disp "`r(panelvar)'" syntax anything, /// TReated(varname) /// ahead(numlist min=1 max=1 >=1 int) // We need a treatment variable as 0 1 gettoken depvar anything: anything local y_lab: variable label `depvar' gl outlab: disp "`y_lab'" /********************************************************** * Pre-Processing* **********************************************************/ preserve numcheck, unit($panel) time($time) depvar(`depvar') // Routine 1 treatprocess, treat(`treated') time($time) unit($panel) // Routine 2 data_org, unit($panel) time($time) depvar(`depvar') treat(`treated') // Routine 3 /********************************************************** * Estimation* **********************************************************/ est_lasso, time($time) h(`ahead') restore end
Code:
qui: keep `time' `treated_unit' cf
Code:
/********************************************************** *Section 2: Estimating Causal Effects **********************************************************/ cap prog drop est_lasso // Subroutine 2.1 prog est_lasso syntax, time(varname) h(numlist min=1 max=1 >=1 int) di as txt "{hline}" di as txt "Second Step: Estimation" di as txt "{hline}" qui: ds loc temp: word 1 of `r(varlist)' loc time: disp "`temp'" loc t: word 2 of `r(varlist)' loc treated_unit: disp "`t'" loc a: word 3 of `r(varlist)' loc donor_one: disp "`a'" local nwords : word count `r(varlist)' loc b: word `nwords' of `r(varlist)' loc last_donor: disp "`b'" di as txt "Estimating... This could take a while..." cvlasso `treated_unit' `donor_one'-`last_donor' if `time' < $int_date, lse lglmnet roll h(`h') qui{ cap drop cf predict double cf, lopt } qui: keep `time' `treated_unit' cf sa "$treat_lab", replace qui: esize unpaired `treated_unit' == cf if `time' < $int_date, cohensd tw /// (line `treated_unit' `time', lcol("49 140 231")) /// Real Outcomes (line cf `time', lcol("237 41 57")), /// Potential Outcomes xli($int_date, lcol("152 152 152")) /// scheme(black_jet) /// legend(order(1 "$treat_lab" 2 "Syn. $treat_lab") /// color(black) fcolor(white) region(fcolor(white)) position(6) rows(1)) /// yti("$outlab") note("Cohen's D: `r(d)'.", position(6)) /// ylab(, noticks nogrid) xlab(, noticks nogrid) macro drop _all end
Comment