Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Ryan Kessler <ryan.kessler.stata@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: Choosing the Minimum Value and Recording Variable Name |
Date | Wed, 3 Apr 2013 10:07:55 -0400 |
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/