Hi,
I have data from 136 separate months that have the same variable names in each file. I'm trying to merge the monthly data with a a different data set. I would like to rename the variable of interest, mean, to ndviYYMM each time a file is merged (YYMM will be digits identifying the year and month). The code I have, which successfully merges but overwrite ndviYYMM for each month is:
cd "C:\Users\csutera\Desktop\N. Burger MENA Climate Change\weather data\srndvi"
local i = 1
fs *.csv
foreach x in `r(files)' {
insheet using `x', clear
save `x', replace
use "C:\Users\csutera\Desktop\N. Burger MENA Climate Change\stata data\lab algeria.dta", clear
merge m:1 orig_fid using `x'
drop count_ area min_ max_ range std sum_ variety minority majority median
rename mean ndvi`i'
local ++i
}
Because I could not figure out how to attach digits YYMM i just tried to attach numbers from 1-136. A YYMM suffix is preferred. I also tried putting the 'local = 1' line inside the loop and separately tried using the mmerge function posted here:
local i = 1
fs *.csv
foreach x in `r(files)' {
insheet using `x', clear
save `x', replace
use "C:\Users\csutera\Desktop\N. Burger MENA Climate Change\stata data\lab algeria.dta", clear
mmerge orig_fid using `x', type(n:1) ukeep(mean) urename(mean ndvi`i')
local ++i
}
I also tried using the line ' rename mean ndvi(###), addnumber' but it also overwrites in each iteration of the loop.
Can you help me attach the desired suffix?
Thanks for reading.
I have data from 136 separate months that have the same variable names in each file. I'm trying to merge the monthly data with a a different data set. I would like to rename the variable of interest, mean, to ndviYYMM each time a file is merged (YYMM will be digits identifying the year and month). The code I have, which successfully merges but overwrite ndviYYMM for each month is:
cd "C:\Users\csutera\Desktop\N. Burger MENA Climate Change\weather data\srndvi"
local i = 1
fs *.csv
foreach x in `r(files)' {
insheet using `x', clear
save `x', replace
use "C:\Users\csutera\Desktop\N. Burger MENA Climate Change\stata data\lab algeria.dta", clear
merge m:1 orig_fid using `x'
drop count_ area min_ max_ range std sum_ variety minority majority median
rename mean ndvi`i'
local ++i
}
Because I could not figure out how to attach digits YYMM i just tried to attach numbers from 1-136. A YYMM suffix is preferred. I also tried putting the 'local = 1' line inside the loop and separately tried using the mmerge function posted here:
local i = 1
fs *.csv
foreach x in `r(files)' {
insheet using `x', clear
save `x', replace
use "C:\Users\csutera\Desktop\N. Burger MENA Climate Change\stata data\lab algeria.dta", clear
mmerge orig_fid using `x', type(n:1) ukeep(mean) urename(mean ndvi`i')
local ++i
}
I also tried using the line ' rename mean ndvi(###), addnumber' but it also overwrites in each iteration of the loop.
Can you help me attach the desired suffix?
Thanks for reading.
Comment