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: using foreach and encode together


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: using foreach and encode together
Date   Thu, 7 Jun 2012 10:07:38 +0100

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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index