Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Rename


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Rename
Date   Thu, 7 Aug 2008 17:00:02 +0100

Eva's solution is fine. I too am a great fan of using -strpos()-,
-substr()- and -subinstr()- for basic string manipulation. 

I thought nevertheless it would be of interest to look at a regex
solution -- partly because Ronnie may one day soon have a problem that
does not quite fit his original specification. 

foreach x of var r* { 
	if regexm("`x'", "(r[0-9]+)(k[0-9]+)") { 
		rename `x' `=regexs(2)'`=regexs(1)' 
	}
} 

That flips all varnames of the form r<digits>k<digits> to
k<digits>r<digits>. 

Nick
n.j.cox@durham.ac.uk 


Eva Poen

here is one way to do it.

foreach x of varlist r1* {
   local end = substr("`x'",-2,.)
   local start = subinstr("`x'","`end'","",1)
   rename `x' `end'`start'
}

Ronnie Babigumira 

> How do I rename some variables by switching letters around.
>
> As an example, I would like to rename r1k4 r12k4 to k4r1 and k4r12, so
take
> the last two characters and put them at the beginning and put whatever
is
> left at the end.


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index