Dear Statlist,
I have a number of small datasets with the same structure and I would like to rename a variable (with a common name across datasets) in each dataset with a different name.
Say I have datasets (data1 data3 data6 ...) where I want to rename variable y (same name in all datasets) with (aaa bbb ccc ...) where "aaa" should be assigned to file "data1", "bbb" to "data2", and so on.
If filename numbers were sequential I would write:
But as I am dealing with numerical suffixes in filenames that do not follow any logical sequence looping over a number isn't a solution and I wonder if you know any way to call the jth element of a local list within the jth iteration of a foreach loop.
In more general terms, I would like to assign the ith element from a list of files to the corresponding ith element in a list of variables.
Thank you,
Andre
I have a number of small datasets with the same structure and I would like to rename a variable (with a common name across datasets) in each dataset with a different name.
Say I have datasets (data1 data3 data6 ...) where I want to rename variable y (same name in all datasets) with (aaa bbb ccc ...) where "aaa" should be assigned to file "data1", "bbb" to "data2", and so on.
If filename numbers were sequential I would write:
Code:
local newvarlist aaa bbb ccc local i=1 foreach nv of local newvarlist { use ".../data`i'.dta", clear local ++i rename `y' `nv' save ".../`nv'.dta", replace }
In more general terms, I would like to assign the ith element from a list of files to the corresponding ith element in a list of variables.
Thank you,
Andre
Comment