Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st:looping over 41 months over 3 full years(12 months each) and a half year (5 months)
From
Amanda Fu <[email protected]>
To
[email protected]
Subject
st:looping over 41 months over 3 full years(12 months each) and a half year (5 months)
Date
Thu, 30 Sep 2010 10:00:13 -0400
Dear Statalisters,
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
*************************************************************
Thank you for your time! I appreciate it!
Best regards,
Amanda
*
* 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/