Hi

I am trying to analyze some data, and have entered some commands,..as reproduced below,..however when i get to the loop in red,..i'm getting "==1 invalid name" and have run down quite a number of answers on similar problems, but i am not managing to find the problem. Help would be much appreciated,..i have also attached the excel file so that the commands can be reproduced.

// Importing data
import excel "Version3_20_06.xlsx", sheet("Form responses 1") firstrow

// Cleaning data

// Store-level variables
// Filling in store level variables
replace Retail_size="Small" if Retail_size=="small"
replace Retail_size=Retail_size[_n-1] if Retail_size==""
egen store_number=group(StoresTimestamp)
replace store_number=store_number[_n-1] if store_number==.
replace Shop_location=Shop_location[_n-1] if Shop_location==""

// Assigning numeric values to categorical variables
encode Retail_size, gen(shop_size)
encode Shop_location, gen(shop_location)
// Reordering variables
order store_number shop_size shop_location

// Dropping un-needed variables
drop StoresTimestamp Retail_size Shop_location


// Brand-level variables
drop Category_beverage
rename Cocacola_available cocacola_available
rename Cocacola_price cocacola_price
rename Coke_light_available coke_light_available
rename Coke_light_price coke_light_price
rename sprite_zeroavailable sprite_zero_available

//rename sprite_Size sprite_size
//rename BeverageSize stoney_size
rename cocacola_Size cocacola_size
rename Coo_ee_Price Coo_ee_price
rename CloverTropikajuicePrice clover_tropika_juice_price
rename cococola_promotion cocacola_promotion

//rename Stoney_zero_available stoney_zero_available
rename Stoney_zero_price stoney_zero_price
rename Stoney_zero_promotion stoney_zero_promotion

// Encode availability

foreach var in cocacola coke_light coke_zero sprite_zero sprite sparletta stoney stoney_zero tab_caffeine_free Coo_ee appletiser Oros halls energade red_bull_caffeine red_bull_sugarfree vitaminwater still_water clover60_fruit_juice liqui_fruitJ_juice clover_tropika_juice pure_milk flavoured_water {
encode `var'_available, gen(`var'_available_)
drop `var'_available
rename `var'_available_ `var'_available
}
// Generating size measure in litres
foreach var in cocacola coke_light coke_zero sprite_zero sprite sparletta stoney stoney_zero tab_caffeine_free Coo_ee appletiser Oros halls energade red_bull_caffeine red_bull_sugarfree vitaminwater still_water clover60_fruit_juice liqui_fruitJ_juice clover_tropika_juice pure_milk flavoured_water {
gen `var'_size_litres=.
replace `var'_size_litres=0.250 if `var'_size=="> 250ml"
replace `var'_size_litres=0.300 if `var'_size=="> 300ml"
replace `var'_size_litres=0.330 if `var'_size=="> 330ml"
replace `var'_size_litres=0.355 if `var'_size=="> 355ml"
replace `var'_size_litres=0.440 if `var'_size=="> 440ml"
replace `var'_size_litres=0.473 if `var'_size=="> 473ml"
replace `var'_size_litres=0.500 if `var'_size=="> 500ml"
replace `var'_size_litres=0.750 if `var'_size=="> 750ml"
replace `var'_size_litres=1 if `var'_size=="1L"
replace `var'_size_litres=1.25 if `var'_size=="1> .25L"
replace `var'_size_litres=1.5 if `var'_size=="1.> 5L"
replace `var'_size_litres=2 if `var'_size=="2L"
replace `var'_size_litres=2.25 if `var'_size=="2 > .25L"
drop `var'_size
}
// Generating price per litre
destring coke_zero_price, replace force float
destring sprite_zero_price, replace force float
destring red_bull_sugarfree_price, replace force float

foreach var in cocacola coke_light coke_zero sprite_zero sprite sparletta stoney stoney_zero tab_caffeine_free Coo_ee appletiser Oros halls energade red_bull_caffeine red_bull_sugarfree vitaminwater still_water clover60_fruit_juice liqui_fruitJ_juice clover_tropika_juice pure_milk flavoured_water {
gen `var'_price_litre=`var'_price/`var'_size_litres
}
// Re-shaping data for analysis
order store_number-flavoured_water_price_litre, alpha
order store_number shop_size shop_location
local i=1

foreach var in cocacola coke_light coke_zero sprite_zero sprite sparletta stoney stoney_zero tab_caffeine_free Coo_ee appletiser Oros halls energade red_bull_caffeine red_bull_sugarfree vitaminwater still_water clover60_fruit_juice liqui_fruit_juice clover_tropika_juice pure_milk flavoured_water {
preserve
if `i'==1 {
keep store_number shop_size shop_location `var'*
drop if `var'_size_litres==.
rename `var'_available available
rename `var'_price price
rename `var'_price_litre price_litre
rename `var'_size_litres size_litres
rename `var'_promotion promotion
gen brand="`var'"
keep store_number shop_size shop_location brand size_litres available price price_litre promotion
order store_number shop_size shop_location brand size_litres available price price_litre promotion
save "Data.dta", replace
}

if `i'>1 {
keep store_number shop_size shop_location `var'*
drop if `var'_size_litres==.
rename `var'_available available
rename `var'_price price
rename `var'_price_litre price_litre
rename `var'_size_litres size_litres
rename `var'_promotion promotion
gen brand="`var'"
keep store_number shop_size shop_location brand size_litres available price price_litre promotion
order store_number shop_size shop_location brand size_litres available price price_litre promotion
save "Temp/`var'.dta", replace
use "Data.dta", clear
append using "Temp/`var'.dta", force
save "Data.dta", replace
}
local i=`i'+1
restore
}


I can't seem to find a solution to the "==1 invalid name" error,..i will greatly appreciate any possible help