I am currently trying to code regression routine that looped over several hundred data file with identical variable list. The code itself is given below:
When I run the set trace on command, the error is found at the following piece of code (boldfaced by me):
* Append the results to the temporary dataset
replace file_name = "`file'" in `row'
Apparently, Stata is unable to find the `file' string even though it displays this string at the beginning of executing this code (boldfaced by me). This is perplexing, is something happening between the first time the file name shows up and this part? I am not sure and I need to solve it for this code to work. The regression part executes properly so I am not including that here.
Code:
* Define input and output directories local input_dir "/Users/muhammadkhan/Library/CloudStorage/OneDrive-Personal/WBES Data/Stata Data Files" local output_file "/Users/muhammadkhan/Library/CloudStorage/OneDrive-Personal/WBES Data/Stata Data Files/regression_results.xlsx" * Create a temporary dataset to store all results clear input str100 file_name float coef_1 se_1 coef_2 se_2 coef_3 se_3 underid_pvalue weakid_fstat sargan_pvalue end * List all .dta files in the input directory and store them in a local macro local files : dir "`input_dir'" files "*.dta" * Initialize row index for storing results in the temporary dataset local row = 1 * Loop through each file foreach file of local files { * Display file being processed display "Processing file: `file'" * Load the dataset use "`input_dir'/`file'", clear capture{ REGRESSION COMMAND (SUPPRESED FOR BREVITY HERE } * Check if regression was successful (error code) if (_rc != 0) { display "Skipping file `file' due to regression error" continue // Skip this file and go to the next one } * Extract the regression coefficients and standard errors matrix b = e(b) // Coefficients matrix se = e(V) // Variance-covariance matrix * Store the coefficients and standard errors scalar coef_1 = b[1, 1] scalar se_1 = sqrt(se[1, 1]) scalar coef_2 = b[1, 2] scalar se_2 = sqrt(se[2, 2]) scalar coef_3 = b[1, 3] scalar se_3 = sqrt(se[3, 3]) * Extract the post-estimation results: * Underidentification test (first stage F-statistic) scalar underid_pvalue = e(underid_p) // Underidentification p-value * Weak identification test (F-statistic) scalar weakid_fstat = e(weakid_fstat) // Weak identification F-statistic * Sargan test (overidentification test) scalar sargan_pvalue = e(sargan_p) // Sargan test p-value * Append the results to the temporary dataset replace file_name = "`file'" in `row' replace coef_1 = coef_1 in `row' replace se_1 = se_1 in `row' replace coef_2 = coef_2 in `row' replace se_2 = se_2 in `row' replace coef_3 = coef_3 in `row' replace se_3 = se_3 in `row' replace underid_pvalue = underid_pvalue in `row' replace weakid_fstat = weakid_fstat in `row' replace sargan_pvalue = sargan_pvalue in `row' * Increment row for next dataset local row = `row' + 1 } * Save the results to an Excel file export excel file_name coef_1 se_1 coef_2 se_2 coef_3 se_3 underid_pvalue weakid_fstat sargan_pvalue using `output_file', firstrow(variables) replace display "Regression results saved to `output_file'"
When I run the set trace on command, the error is found at the following piece of code (boldfaced by me):
* Append the results to the temporary dataset
replace file_name = "`file'" in `row'
Apparently, Stata is unable to find the `file' string even though it displays this string at the beginning of executing this code (boldfaced by me). This is perplexing, is something happening between the first time the file name shows up and this part? I am not sure and I need to solve it for this code to work. The regression part executes properly so I am not including that here.
Comment