RE: st: using foreach and encode together

Thanks Nick for your help,
I've changed the length of my variable name and all is fine!
Best wishes
Tim
-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: 07 June 2012 10:08
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: using foreach and encode together
The variable name
IntegrTNM_Stage_Algo_No_assumpti
is already 32 characters long, so you can't prepend "v2" to get
another variable name. You have to shorten it.
I would -drop- only if the -encode- works. If the -encode- fails then
it's true that the loop would stop, as here, but in general watch out
that you might lose the variable.
if _rc == 0 drop `var'
Nick
On Thu, Jun 7, 2012 at 9:57 AM, Tim Evans <Tim.Evans@wmciu.nhs.uk> wrote:
> I'm trying to encode a number of string variables and generate new variables which contain the string values as labels. I have a number of these variables and am trying to avoid doing each manually, however, I am encountering some problems with the syntax. A quick google search has not yielded any answers for me. I'm using Stata 11.2.
>
> Here is my code:
>
> foreach var of varlist IntegrT_Stage_Algo Nodes_Positive IntegrTNM_Stage_Algo_No_assumpti InvStatus {
> encode `var', gen(v2`var')
> }
>
> this is my error message:
>
> option gen() incorrectly specified
> r(198);
>
> I guess what I would also like once I have generated the new variable, to drop the string version - perhaps I can add -drop `var'- after the -encode- line?
*
* 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/
_DISCLAIMER:
This email and any attachments hereto contains proprietary information, some or all of which may be confidential or legally privileged. It is for the exclusive use of the intended recipient(s) only. If an addressing or transmission error has misdirected this e-mail and you are not the intended recipient(s), please notify the author by replying to this e-mail. If you are not the intended recipient you must not use, disclose, distribute, copy, print, or rely on this e-mail or any attachments, as this may be unlawful.
*
* 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/