Bookmark and Share

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: Choosing the Minimum Value and Recording Variable Name


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Choosing the Minimum Value and Recording Variable Name
Date   Wed, 3 Apr 2013 16:00:25 +0100

In addition to Ryan's excellent advice, check out

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 some technique in this territory.

On 3 April 2013 15:07, Ryan Kessler <[email protected]> wrote:
> How about something like this?
>
> input id drug1 drug2 drug3
> 1 0 10 100
> 2 100 10 0
> 3 10 0 100
> end
>
> egen min=rowmin(drug*)
> gen min_var = ""
> foreach v of varlist drug* {
> replace min_var = "`v'" if `v' == min
> }
>
> Best,
>
> Ryan Kessler
>
>
> On Wed, Apr 3, 2013 at 9:45 AM, Erika Kociolek <[email protected]> wrote:
>> I am working with a dataset that looks like this:
>>
>> id  drug1  drug2  drug3  drug4  drug5  drug6
>> 1   10     20     30      .      .      .
>> 2   40     30      .     50      .      .
>>
>> I'd like write code that finds the minimum value of drug* and for each
>> observation, puts the name of the variable with this minimum value in
>> a new variable (let's call it minimum_var), i.e.:
>>
>> id  drug1  drug2  drug3  drug4  drug5  drug6 minimum_var
>> 1   10     20     30      .      .      .    drug1
>> 2   40     30      .     50      .      .    drug2
>>
>> I would prefer to do this without reshaping the data, and know that I
>> will need to account for ties.
>>
>> Thank you in advance for your assistance!
>>
>> Erika
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/



-- 
Nick
[email protected]
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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