Hello,
I'm trying to extract dates (in mm/dd/yyyy format) that are my variables' labels. I want to use them to rename my variables (which are unhelpfully called v39-v41 at the moment). However, I also want to get rid of the dd part of the date string, replace / with _, and add a string prefix to each.
Here is some sample data.
I want v39 to be renamed elect_2017_05, v40 to be elect_2016_11, and v41 to be elect_2016_08.
So far, I can pull out the forward slashes and replace them with underscores with this code:
Trying to take it a step further to remove everything between (and including) the forward slashes, i tried this:
but to no avail.
If anyone has any ideas, they'd be gratefully received
I'm trying to extract dates (in mm/dd/yyyy format) that are my variables' labels. I want to use them to rename my variables (which are unhelpfully called v39-v41 at the moment). However, I also want to get rid of the dd part of the date string, replace / with _, and add a string prefix to each.
Here is some sample data.
Code:
clear input str3(v39 v40 v41) "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" end label var v39 "05/16/2017" label var v40 "11/08/2016" label var v41 "08/15/2016"
I want v39 to be renamed elect_2017_05, v40 to be elect_2016_11, and v41 to be elect_2016_08.
So far, I can pull out the forward slashes and replace them with underscores with this code:
Code:
forvalues i = 39/63{ local newname subinstr("`:variable label v`i''", "/", "_",.) di `newname' rename v`i' elect_`=`newname'' }
Code:
local newname subinstr("`:variable label v39'", "/[0-9][0-9]/", "_",.) di `newname'
If anyone has any ideas, they'd be gratefully received
Comment