Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: RE : RE: Foreach and rename


From   "Rajesh Tharyan" <R.Tharyan@exeter.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: RE : RE: Foreach and rename
Date   Mon, 25 Aug 2008 14:25:09 +0100

Hi,

Alvine, You showed us your code . you didn’t show us that stata gave you.
You said But it doesn't work : "invalid syntax". Did stata say invalid
syntax or variable not found?

The following code works perfectly well (the loop is exactly your code!
Except 9 has been changed to 5)

clear 
set obs 5
gen moi01008=0
gen moi02008=0
gen moi03008=0
gen moi04008=0
gen moi05008=0
 
forvalues i=1/5 {
rename moi0`i'008 date_modif_`i'
}

While the following code (where the moi3008 is missing)

clear 
set obs 5
gen moi01008=0
gen moi02008=0
gen moi04008=0
gen moi05008=0
 
forvalues i=1/5 {
rename moi0`i'008 date_modif_`i'
}

Gives me

. forvalues i=1/5 {
  2. rename moi0`i'008 date_modif_`i'
  3. } 
variable moi03008 not found
r(111);

not "invalid syntax" as mentioned in the original post.

When you post you have to show exactly what stata gave you, That makes it
easier for people trying to answer the query.

rajesh

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Martin Weiss
Sent: 25 August 2008 14:06
To: statalist@hsphsun2.harvard.edu
Subject: st: RE: RE : RE: Foreach and rename

Try this to see what I am trying to convey...

foreach number in 1 2 3 4 5{
di `number'
}


So yours would be 

  foreach i in 1 2 3 5 6 9{
 	rename moi0`i'008 date_modif_`i'
 	}



HTH
Martin

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of
alvine.bissery@chu-lyon.fr
Sent: Monday, August 25, 2008 2:50 PM
To: statalist@hsphsun2.harvard.edu
Subject: st: RE : RE: Foreach and rename

Thak you for your answer , but it doesn't help me for the invalide syntax of

  forvalues i=1/9 {
 rename moi0`i'008 date_modif_`i'
 } 

-----Message d'origine-----
De : owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] De la part de Martin Weiss
Envoyé : 25 August 2008 14:40
À : statalist@hsphsun2.harvard.edu
Objet : st: RE: Foreach and rename


-h foreach- shows you that you can specify a -numlist- where you spell out
the members explicitly.

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of
alvine.bissery@chu-lyon.fr
Sent: Monday, August 25, 2008 2:28 PM
To: statalist@hsphsun2.harvard.edu
Subject: st: Foreach and rename

Dear all, 

I'd like to change the name of many variables in this way:

 rename moi01008 date_modif_1
 rename moi02008 date_modif_2
 rename moi03008 date_modif_3
 rename moi05008 date_modif_5
 rename moi06008 date_modif_6
 rename moi09008 date_modif_9

I'd like to use a loop, like 

  forvalues i=1/9 {
 rename moi0`i'008 date_modif_`i'
 }

But it doesn't work : "invalid syntax"

Even if this loop works, I have a problem with the value i=4,7 and 8 for
which moi0`i' does not exist.

How can I do ?

Thanks 

Alvine

*****************************************************
Alvine BISSERY
Ingénieur statisticien 
Service de Biostatistique des Hospices Civils de Lyon 
162 Avenue Lacassagne 
69424 Lyon Cedex 03 - France 
tél: +33 4 72 11 57 55 
fax: +33 4 72 11 51 41 


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



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


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



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


*
*   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   |   What's new   |   Site index