Hi all, I have dataframe with 2 variables, in which did_control calculated by daily_income_2021 minus daily_income_2023 in control group; and did_treatment calculated by daily_income_2021 minus daily_income_2023 in treatment group, as follow:
Now i would like to test DID method by using ttest in stata, but I have some errors in these codes as follow:
I would appreciate any suggestion help me to find out solutions. Thanks!!
Code:
* Example generated by -dataex-. For more info, type help dataex clear input float(did_control did_treatment) . . . . . . . . -6027.396 . . . . . . . 55068.5 . . . 0 . . . 0 . . . -10958.904 . . . 16438.355 . . . . . 107123.28 . . . . . . 0 . . 41534.25 . . . . 0 . . . . . . -46575.34 . . . 39726.02 . . . 23835.615 . . . . . . 0 . . 0 . . . -8767.123 . . . 391835.6 . . . 8383.561 . . . -4383.5625 . . . . . . . -32876.71 . . . 0 . . . . . . -52054.79 . . 47671.24 . . . . 0 . . . . . . . . . -38356.156 . . . . . . . 66904.11 . . . 0 . . 142465.75 . . . -32876.71 . . . . 109589.04 . . -32876.71 . . . . . -141835.61 . . . 0 . . . . -60273.97 . . -6849.315 . . . -7671.234 . . . -10958.904 . . . -80273.98 . . . 66301.375 . . . . . 14794.52 . end
Code:
// Initialize row counter local row = 2 // Define the list of variables to analyze // Run T-Test for the DID effect di `row' di "DID Effect" ttest did_control = did_treatment if did_treatment < . & did_control < . // Perform DID t-test ret list local dif_m = `r(mu_2)' - `r(mu_1)' // difference of mean local nhan = "DID Effect" // Determine the significance star local star if r(p) < 0.01 { local star = "***" } else if r(p) < 0.05 { local star = "**" } else if r(p) < 0.1 { local star = "*" } else { local star = " " } // Format and display the p-value with the significance star local p : display %5.2f `r(p)' "`star'" // add star symbol for significance di "`p'" // Export results to Excel for the DID effect putexcel A`row' = ("DID Effect") putexcel B`row' = ("`nhan'") putexcel C`row' = `r(mu_2)', nformat(number_d2) putexcel D`row' = `r(mu_1)', nformat(number_d2) putexcel E`row' = `dif_m', nformat(number_d2) // Add bold formatting for significant p-values for DID effect if strpos("`p'", "***") > 0 { putexcel F`row' = "`p'", bold } else { putexcel F`row' = "`p'" } putexcel G`row' = `r(N_2)' putexcel H`row' = `r(N_1)'
Comment