Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Batch renaming variables


From   Maarten Buis <maartenlbuis@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Batch renaming variables
Date   Thu, 18 Oct 2012 16:28:27 +0200

On Thu, Oct 18, 2012 at 4:03 PM, Wameq Raza wrote:
> The variables I have start from v0915 and continue to v1005 and I need
> to rename every 5th variable in this list starting from v0917. The
> variables to be renamed to are: cost_a, cost_b....cost_n.

*--------------------------- begin example --------------------------
// create some example data
clear
set obs 10
forvalues i = 915/1005 {
	gen v`:display %04.0f `i'' = runiform()
}
	
// start renaming
local j = 0
forvalues i = 917(5)1005 {
	local j = `j' + 1
	mata: st_local("letter", strlower(numtobase26(strtoreal(st_local("j")))))
	rename v`:display %04.0f `i'' cost_`letter'
}
*---------------------------- end example ---------------------------

Hope this helps,
Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

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


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