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, is already up and running.

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

st: RE: renaming variables

From   "Nick Cox" <>
To   <>
Subject   st: RE: renaming variables
Date   Fri, 21 May 2010 13:26:20 +0100

-renvars- was written by Jeroen Weesie and myself. Good practice as
often requested on the list is to say where user-written stuff comes
from, which here is the Stata Journal site. 

As you say, your code was incorrect.

1. -postsub()- is an option of -renvars- and therefore must follow a

2. -postsub()- does not use comma as separator but a space. 

3. Your code would not change "var" to "year". 

4. You need at most one loop simultaneously over 53(3)107 and 1990/2008
(with 19 iterations) but your two nested loops would produce 19 * 19

In principle, you seem to be trying to call -renvars- once for each
renaming. If you are going to do that you might as well use -rename-. 

-renvars- can be used to solve your problem -- in one line -- but I
think the solution is too messy to be interesting. After all, you want
to change the prefix systematically and the postfix from an
idiosyncratic number to a more systematic number, all at once, and no
one-line solution for that could look nice. 

Any way, this is a solution: 

renvars var*, map("year" + string(1989 + (real(substr("@",4,.)) - 50) /

Here all the looping is inside -renvars-. 

Even as an author of -renvars- I would much prefer going back to first
principles and very much agree with Martin Weiss's strategy. 

His code can be very slightly simplified: 

set obs 1

forv i=53(3)107{
	gen var`i'=1

loc start 1990

foreach var of varlist var*{
	ren `var' year`start'
	loc ++start


Abhimanyu Arora

I would like to rename my variables var53....var108 such that var53,
var56, var59 ......var107 etc...are renamed as year1990,
year1991......year2008 repectively. I tried to use renvars(by Jeroen
Weesie) and a forvalues loop, but am not able to succeed.
Is there any other way?

PS-my incorrect way follows

forvalues j =53(3)107{
forvalues i =1990/2008{
renvars postsub(`i',`j')    /*the arguments should be strings, I guess,

*   For searches and help try:

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