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: forval query


From   William Buchanan <william@williambuchanan.net>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: forval query
Date   Fri, 5 Apr 2013 07:20:04 -0700

You could include the rename command for a group of variables within the loop or if the naming conventions are a bit different you could use a -foreach- loop nested within the -forvalues- loop.  

Also, the -ma drop j- line is unnecessary in your code since the value is replaced during each iteration of the loop.

HTH,
Billy

Sent from my iPhone

On Apr 5, 2013, at 7:13, "Mirnezami, Oliver" <O.Y.Mirnezami@warwick.ac.uk> wrote:

> Thank you to Maarten and Billy for your quick and helpful replies. I wondered is it possible to alter the syntax so that this can be done in an automated way for many variables?
> 
> I can repeat your set of commands for each variable e.g. 
> 
> forvalues i = 2/10 {
>    local j = 1990 + (2*`i')
>    rename r`i'mobila mobila_`j'
>    ma drop j
> }
> 
> 
> forvalues i = 2/10 {
>    local j = 1990 + (2*`i')
>    rename r`i'lgmusa lgmusa_`j'
>    ma drop j
> }
> 
> Etc. 
> 
> However, I have do this for many variables so can you plug in a line with a list of these variables to avoid repeating the command over and over again?
> 
> Thank you
> 
> Kind regards
> 
> Oliver 
> 
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Maarten Buis
> Sent: 05 April 2013 14:40
> To: statalist@hsphsun2.harvard.edu
> Subject: Re: st: forval query
> 
> On Fri, Apr 5, 2013 at 3:32 PM, Mirnezami, Oliver wrote:
>> Previously i've been renaming variables manually en masse using the following code:
>> 
>> rename  r2mobila mobila_1994
>> rename  r3mobila mobila_1996
>> rename  r4mobila mobila_1998
>> .etc..
>> rename  r10mobila mobila_2010
>> 
>> i.e. '2' corresponds with '1994', '3' with '1996' etc.
> 
> forvalues i = 2/10 {
>    local y = 2*`i' +1990
>    rename r`i'mobila mobila_`y'
> }
> 
> ---------------------------------
> 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/
> 
> 
> 
> *
> *   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/

*
*   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