I'm trying to automate a reshape
using Stata.
I have a series of variables measured yearly. They are all named varname_yy
, where yy
is a number referring to the year of measurement. I managed to extract all the stubs varname_
from the variables and to put them into a macro using the following code:
local stubs
foreach var of varlist `myvars' {
local stub = substr("`var'",1,length("`var'") - 2)
local stubs `stubs' `stub'
}
The problem is that I end up with many repeated stubs in the stubs
macro and this causes reshape
to return an error message.
In R I would just ask for unique(stubs)
, but I couldn't find any such function in Stata.
My tentative solution is to do the following:
local uniquestubs
foreach stub in `stubs' {
if !regexm("`uniquestubs'","`stub'") {
local uniquestubs "`uniquestubs'" " `stub'"
}
}
However, I can't get rid of the duplicates.
What is the correct way of doing it?