Hi, this is my code. I need help in making it succint and reproducible. I want only three values should be inputted in the program veg_lower_rate_qty_1 sale_unit_1 crop_1. The program take 3 var from the file units_cf.xls: unit, crop and conversion. Unit, crop and conversion stays consistent for running the program. Is there a way to feed these 3 var in the program?
Second, I want to run the loop _N times as in units_cf.xlsx dataset.
Thanks in advance for your help
use "`input'\hfms.dta", clear
//creating a temp index number to merge 2 files
gen order1=_n
cap drop _m
tempfile m2
save `m2',replace
import excel "`input_1'\units_cf.xlsx", sheet("fruits_vegetables") firstrow clear
replace order1=_n
rename sale_unit unit
merge 1:1 order1 using `m2'
program define qtyconvert
*version 8
args qty data_unit data_crop data unit crop conversion
confirm var `qty' `data_unit' `data_crop' `unit' `crop' `conversion'
forval k=1/17{
replace `qty'= `qty' if `unit'=="kilo"
replace `qty' = `qty'*`conversion' if `data_unit'==`unit'[`k'] & `data_crop'==`crop'[`k'] & !mi(`qty')
}
end
set trace on
qtyconvert veg_lower_rate_qty_1 sale_unit_1 crop_1 unit crop conversion
Second, I want to run the loop _N times as in units_cf.xlsx dataset.
Thanks in advance for your help
use "`input'\hfms.dta", clear
//creating a temp index number to merge 2 files
gen order1=_n
cap drop _m
tempfile m2
save `m2',replace
import excel "`input_1'\units_cf.xlsx", sheet("fruits_vegetables") firstrow clear
replace order1=_n
rename sale_unit unit
merge 1:1 order1 using `m2'
program define qtyconvert
*version 8
args qty data_unit data_crop data unit crop conversion
confirm var `qty' `data_unit' `data_crop' `unit' `crop' `conversion'
forval k=1/17{
replace `qty'= `qty' if `unit'=="kilo"
replace `qty' = `qty'*`conversion' if `data_unit'==`unit'[`k'] & `data_crop'==`crop'[`k'] & !mi(`qty')
}
end
set trace on
qtyconvert veg_lower_rate_qty_1 sale_unit_1 crop_1 unit crop conversion
Comment