Hi everyone,

I have about twenty csv files on which I need to do some estimation after I have obtained a certain sub-sample. Hence I have an outer loop for importing each csv file; within this loop I have: i) an inner loop to trim the sample in function of OLS residuals; ii) an OLS regression to compute some coefficients that then I save in separate tables.
Below is my complete code. As output I get:

using required
r(100);
end of do-file
r(100);

Any idea on what I'm doing wrong?

Other details:
- STATA 14;
- 4 vars, 92638168 obs
- each file is called "xxxxxx_night-light_Africa_adjusted.csv", where xxxxxx are six digits;
- variables are for each file: v1, v2, z_base, z_xxxxxx, where xxxxxx is the same six digit code in the file name.

PHP Code:
// This code computes the coefficients for inter-calibrating night-light data across DMSP satellites
clear all
local w_f 
"C:\\Users\\matteo\\Desktop\\Conflict and social cohesion\\Data\\Night-lights\\DMSP\\DMSP intercalibration\"
local filename "
_night-light_Africa_adjusted.csv"
local ext "
.csv"

foreach sat_year in "
101992" "101993" {

    import delimited `w_f'`sat_year'`filename', clear
    local dn_t "
z_`sat_year'"

// Trim sample for obtaining the base sample for intercalibration
    local outlier = 1
    while (
`outlier' > 0) {
        quietly reg z_base `dn_t'
r
        quietly predict st_res
`outlier', rstandard
        quietly replace st_res
`outlier' = 0 if missing(st_res`outlier')  // avoid problems if slope = 1
        
quietly local outlier r(N)
    }

// Obtain intercalibration coefficients and store them
    
quietly gen `dn_t'_sq = (`dn_t')^2

    eststo, title("Inter-calibration coefficients `sat_year'"): quietly reg z_base `dn_t' `dn_t'_sq, r

    estout using `w_f'`sat_year'`ext', cells(b se(par)) stats(r2 N, labels(R-squared)) legend label collabels("
OLS coefficients (SE)") varlabels(_cons Constant)