Hi guys,

I am trying, with the following loop to drop some of my observations, specifically the ones that have quarters below the molecule with the highest quarter and the ones that have quarters above the molecule with the lowest quarter (so if I have mol1 appearing from 2012Q3 and mol2 ending in 2015q3 and other molecules ranging from 2009q3 to 2020q3 I would like to keep only observations from 2012q3 to 2015q3):

Code:
quietly drop if trimestre < `min' | trimestre > `max'
Now I would like to reproduce it on several datasets but it turns out that if I use the code for a single dataset it works as expected, whereas if I use it in the following lop it doesn't (i.e. it does not raise any error but it does no change to the data as if a local is not read or something like that):

Code:
use "/Users/federiconutarelli/Dropbox/PhD/Elasticities/2008_2020_db/dati_per_paese/2008_2020_prd.dta", clear
drop if Country =="ITALY"
levelsof Countr, local(levels)
foreach l of local levels {
    use "/Users/federiconutarelli/Dropbox/PhD/Elasticities/2008_2020_db/dati_per_paese/data_ctry/`l'_new.dta", clear
    quietly levelsof Mole, local(molecules)
    foreach k of local molecules {
        local rex =  strtoname("`k'")
        use "/Users/federiconutarelli/Desktop/here/`l'/`rex'.dta", clear
        gen price = sales/stdunits
        gen ln_price=ln(price)
        gen ln_stdunits = ln(stdunits)
        quietly sum panelsize
        local min_panelsize = r(min)
        sum trimestre if panelsize == `min_panelsize'
        local min = r(min)
        local max = r(max)
        quietly drop if trimestre < `min' | trimestre > `max'
        quietly drop panels*
        bysort id_prd id_mole: gen  panelsize = _N
        save "/Users/federiconutarelli/Desktop/here/`l'/`rex'.dta",replace
    }
 
}

Is there something wrong I am doing here?

Thank you,

Federico