Colleagues,
I'm working with a messy data set that broadly corresponds to the extract below:
Using the code below, I would like to keep numbers only in my local macro. Unfortuntaly, the years are coded as 2013.14 or 2014 or 20132014. Consequently, I would like to get first four figures from my macro below so I can use it in a variable name.
I'm working with a messy data set that broadly corresponds to the extract below:
Code:
// Data set clear set obs 100 local i = 1 while `i' <= 5 { gen messy_var_`i' = runiform() label variable messy_var_`i' /// `"Some nonsense and figure that I need `=floor((2012-2001+1)*runiform() + 2001)'"' local i = `i' + 1 }
Code:
* Rename the variables in the varlit with last set of figures foreach ivar of varlist `r(varlist)' { * Put variable label to macro and keep year local varlbl : variable label `ivar' * It has to be like that as years have different lengths and formats. local vyear = substr("`varlbl'",-7,.) * ---- How to take first four figures only di "`vyear'" }
Comment