Typo corrected below.
Nick
[email protected]
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]]On Behalf Of Nick Cox
> Sent: 22 July 2004 19:17
> To: [email protected]
> Subject: st: RE: Generating a variable containing names of other
> variables
>
>
> Setting aside the possibility of ties,
>
> egen mthmax = rmax(jan feb mar apr may)
>
> gen maxmth = "jan"
> foreach v of var feb mar apr may {
> replace maxmth = "`v'" if `v' == mthmax
> }
>
> You must keep straight the difference between
> a numeric variable holding the maximum, and
> in your case a string variable holding the
> string indicating when that month is.
>
> Ties you can check for by
>
> gen nties = 0
> qui foreach v of var jan feb mar apr may {
> replace nties = nties + (`v' == mthmax)
> }
> tab nties
>
> if -nties- is ever 2 or more, you have ties.
>
> At some point you will want the numeric
> equivalent of -maxmth-, for graphs, or for
> other calculations.
>
> You are (almost) using a sequence wired
> into Stata as `c(Mons)'. Try
>
> di "`c(Mons)'"
>
> You will get
>
> Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
>
> To map jan feb ... dec to 1 2 ... 12, there
> are slow obvious ways and other more subtle ways.
>
> Here is one more subtle way:
>
> gen nmthmax = .
> tokenize `c(Mons)'
> qui forval i = 1/12 {
> replace nmthmax = `i' if mthmax == lower("``i''")
> }
>
> Nick
> [email protected]
>
> Thomas Speidel
>
> > I am trying to generate a new variable that contains the
> name of the
> > variable where a certain expression is true.
> > For example consider the following:
> >
> > jan feb mar apr may
> > 0 200 195 203 119
> > 175 193 220 107 0
> > 98 226 0 0 0
> > ...
> >
> > I want to generate a new variable, say maxmth, that contains
> > the name of
> > the variable where the row maximum occurs.
> > In this case maxmth would look like this:
> >
> > maxmth
> > apr
> > mar
> > feb
> > ...
> >
> > I tried something along this:
> > .egen maxmth=rmax(jan feb mar apr may)
> > .replace maxmth=_varname if maxmth==feb | maxmth==mar |
> maxmth==apr |
> > maxmth==may
> >
> > But I don't know how to refer to the variable name...
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/