Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: How to use functions within variable names
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: How to use functions within variable names
Date
Thu, 18 Aug 2011 09:20:09 +0100
It seems that you need a new variable that is the largest -c- in any group
egen d = max(c), by(group)
Then use -d- not -c-.
Alternatively, see
SJ-9-1 pr0046 . . . . . . . . . . . . . . . . . . . Speaking Stata: Rowwise
(help rowsort, rowranks if installed) . . . . . . . . . . . N. J. Cox
Q1/09 SJ 9(1):137--157
shows how to exploit functions, egen functions, and Mata
for working rowwise; rowsort and rowranks are introduced
for a review of work in this territory. -rowsort- is either what you
want or a source of possible ideas on how to get what you want.
Nick
On Thu, Aug 18, 2011 at 7:08 AM, Jakob Fischer <[email protected]> wrote:
> Hello, first of all thank you Maarten and Daniel for your input and time.
> I tried to keep the example small and simple and focus only on my issue. But here is what I want to do:
> I want to reverse sets of variables for groups with different lengths. The example should illustrate it.
>
>
> *----------- begin example -------------
> clear
> input a0 a1 a2 a3 c group result0 result1 result2 b0 b1 b2
> . 1 2 3 3 1 3 2 1 . . .
> . 4 5 . 3 1 . 5 4 . . .
> . 6 . . 3 1 . . 6 . . .
> . 7 . . 2 2 . 7 . . . .
> . 8 . . 2 2 . 8 . . . .
> . 9 10 . 2 2 10 9 . . . .
> end
> compress
>
> list a1 a2 a3 group c
> //The result should be in reversed order, always starting with variable result0. Where c is the biggest "length" of a-variables within a group.
>
> list result0 result1 result2 group c
> //This is what the final result should look like
> //Group 1: c=3 because of observation 1
> //Group 2: c=2 because of observation 6
>
> forvalue i=0/2 {
> replace b`i'=a`=c-`i''
> }
>
> list b0 b1 b2 group c
> // Observations 4-6 are not correct
> *------------ end example --------------
>
> Unfortunately, as stated by Daniel, Stata takes the c value from the first observation for all observations. Using c[_n] is not working either, but I hope to find a solution to that problem.
*
* 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/