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

st: AW: RE: combination foreach forvalues

From   "Martin Weiss" <>
To   <>
Subject   st: AW: RE: combination foreach forvalues
Date   Tue, 20 Oct 2009 13:27:25 +0200


"-encode- does
precisely this."

I remember there being an issue with the order of the codes that -encode-
assigns, that is why I was reluctant to recommend it. Is that not an issue


-----Ursprüngliche Nachricht-----
[] Im Auftrag von Nick Cox
Gesendet: Dienstag, 20. Oktober 2009 12:17
Betreff: st: RE: combination foreach forvalues

This doesn't require either -foreach- or -forvalues-. -encode- does
precisely this. 


bysort stringvar : gen newvar = _n == 1 
replace stringvar = sum(stringvar) 


egen newvar = group(stringvar) 

There is not much to explain about combining -foreach- and -forvalues-,
as you just do it if and when you need it, typically by nesting one
inside the other. But that's not the case here. 


John Bunge

I have a string variable x1 with a list of values. I want to create a
numerical variable x2 in which the numbers correspond to the string
values in x1 in an ordered fashion (as a counter).

To illustrate, lets assume x1 contains all letters of the alphabet, and
I want x2 to contain a counter that corresponds to the position of the
letter in the alphabet, i.e. x1=a > x2=1, x1=b > x2=2, x1=c > x2=3,

This seems to me like a combination of foreach and forvalues, but I
cannot find information on whether and how such thing is implementable
in Stata.

*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index