Hi alll,
Would really appreciate some help with this, its so far completed stumped me!
I am importing csv files with variable names over 32 characters. I want to create new variables that add to the variable names and therefore get an error message because the new names will be >32.
I therefore want to open each csv file, delete say the last 5 characters of variable names with 32 characters and replace with a number. The end result being that a variable named:
"reallyoverlylongvariablenameits" = "reallyoverlylongvariablen"
"notsolong" = "notsolong"
"anotherridiculouslylongvariable1" "anotherridiculouslylongvariab"
This is what I have so far, but I really don't know how to get it working!
One of the big problems is that I get an error that I cannot seem to resolve. Without the nested loop and taking just a subset of the data:
I get error: syntax error
Syntax is
rename oldname newname [, renumber[(#)] addnumber[(#)] sort ...]
rename (oldnames) (newnames) [, renumber[(#)] addnumber[(#)] sort ...]
rename oldnames , {upper|lower|proper}
Would really appreciate some help with this, its so far completed stumped me!
I am importing csv files with variable names over 32 characters. I want to create new variables that add to the variable names and therefore get an error message because the new names will be >32.
I therefore want to open each csv file, delete say the last 5 characters of variable names with 32 characters and replace with a number. The end result being that a variable named:
"reallyoverlylongvariablenameits" = "reallyoverlylongvariablen"
"notsolong" = "notsolong"
"anotherridiculouslylongvariable1" "anotherridiculouslylongvariab"
This is what I have so far, but I really don't know how to get it working!
Code:
clear local myfilelist : dir . files"*.csv" foreach file of local myfilelist { insheet using `file' foreach var of varlist waistbeltattachknsbyfolder waistbelttopstitchkns waistbeltattachsn { local newname = substr(`var', ?,?) if *? rename `var' `x' } save "`outfile'", replace }
Code:
clear input str5 firm long id str10(waistbeltattachknsbyfolder waistbelttopstitchkns waistbeltattachsn waistbelttopstitchsn) "SMV" . "100" "100" "80" "80" "7001" 78 "" "" "" "50" "7001" 208 "" "" "" "" "7001" 227 "" "" "" "" "7001" 391 "" "" "" "55" "7001" 406 "" "" "" "" "7001" 672 "" "" "50" "" "7001" 817 "" "" "" "" "7001" 2067 "" "" "60" "70" end foreach var of varlist waistbeltattachknsbyfolder waistbelttopstitchkns waistbeltattachsn { local x = substr(`var', -10,10) rename `var' `x' }
Syntax is
rename oldname newname [, renumber[(#)] addnumber[(#)] sort ...]
rename (oldnames) (newnames) [, renumber[(#)] addnumber[(#)] sort ...]
rename oldnames , {upper|lower|proper}
Comment