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 at the end of May, and its replacement, statalist.org is already up and running.


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

RE: st:looping over 41 months over 3 full years(12 months each) and a half year (5 months)


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   RE: st:looping over 41 months over 3 full years(12 months each) and a half year (5 months)
Date   Thu, 30 Sep 2010 15:16:56 +0100

An overall comment is that this kind of structure is usually more awkward, so do consider a -reshape-. 

This is (untested) code intended to do what you want. 

local m = 0 
local year = 1999 
forval i = 1/41 { 
	local m = cond(`m' == 12, 1, `m' + 1) 
	local year = `year' + (`m' == 1) 
	local M : di %02.0f `m' 
	rename var`i' var`M'_`year' 
} 

See also

SJ-10-1 pr0051  . . . . . . . . . . . . Stata tip 85: Looping over nonintegers
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q1/10   SJ 10(1):160--163                                (no commands)
        tip on using forvalues to loop over numbers

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

Amanda Fu

I encounter a question related to looping. I have 41 variables that
indicating each month from Jan 2000 to May 2003.I would like to rename
the variables  into some format as var_`m'_'y'. M means month, y means
year. It looks like the following:
-------------------------------------------------------------
date            var name       new name
Jan2000        var1        var01_2000
Feb2000       var2        var02_2000
.....
Dec2000     var12        var12_2000
Jan2001      var13        var01_2001
Feb2001      var14        var02_2001
.....
Dec2001     var24         var12_2001
................
Jan2003      var37         var01_2003
.......
May2003     var41         var05_2003
--------------------------------------------------------------

QUESTION 1: var01_2000  vs. var1_2000

I find it is not easy to add the 0 before those months earlier than
10. The reason I wanted to rename it as 01,02 ... is that without the
0 sometimes the sequence of the variables will be messy , like:
1,11,12,2,3,.... Alternatively I can just rename as var1_2000,.... and
then adjust the order of the variables.

May I know how will you choose between var01_2000 and var1_2000?

QUESTION 2:  the 5 months in 2003 separately or not

The following is the code I write to rename the variables (using
var1_2000, without 0 before month 1). I separated 2003 from the
previous years since in 2003 there are only 5 month, since I have not
figure out how to put 2003 together with the rest years.

I think there could be some easy way to deal with this. Would anyone
give me some suggestion on how to simplify the codes?

***** rename as "var1_2000","var2_2000"  no 0 before month
********************2000-2002 first
local j=0
pro define renamevar
local ++j
foreach y of 2000(1)2002 {
foreach m of 1/12 {
rename var`j'       var`m'_`y'
}
}
****************** 2003 second
local j2=36
foreach m of 1/5 {
local ++j2
rename var`j2'       var`m2'_2003
}
end
renamevar
*************************************************************

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