Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[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 <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
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 <ryan.kessler.stata@gmail.com> 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 <ekociole@gmail.com> 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
njcoxstata@gmail.com
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index