Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

Re: st: using foreach and encode together

From   Nick Cox <>
Subject   Re: st: using foreach and encode together
Date   Thu, 7 Jun 2012 10:07:38 +0100

The variable name


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'


On Thu, Jun 7, 2012 at 9:57 AM, Tim Evans <> 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:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index