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: Sort rows and give variable name of the observation


From   Haluk Vahaboglu <[email protected]>
To   "[email protected]" <[email protected]>
Subject   RE: st: Sort rows and give variable name of the observation
Date   Thu, 21 Nov 2013 10:09:38 +0000

Nick many thanks for this highly informative overview.

Prof. Dr. Haluk Vahaboğlu



----------------------------------------
> Date: Thu, 21 Nov 2013 09:47:05 +0000
> Subject: Re: st: Sort rows and give variable name of the observation
> From: [email protected]
> To: [email protected]
>
> People interested in this kind of question might find
>
> 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
>
> a useful overview. Accessible at
> http://www.stata-journal.com/sjpdf.html?articlenum=pr0046
>
> Nick
> [email protected]
>
>
> On 21 November 2013 08:58, Haluk Vahaboglu <[email protected]> wrote:
>> Antonis & Maarten,
>> Many thanks for prompt responses. Both solutions are working.
>> However, solving this problem with a loop is more versatile (for further problems) and enjoyable for me.
>
> From: [email protected]
>
>>> On Thu, Nov 21, 2013 at 7:19 AM, Haluk Vahaboglu wrote:
>>>> I have a set of variables
>>>> coded binary as 1/0 and an ID variable:
>>>> ID x y z
>>>> 1 1 0 0
>>>> 2 0 1 0
>>>> 3 0 0 1
>>>> I want to search row-wise
>>>> and find out the first occurrence of -1- to generate a new string
>>>> variable displaying variable name of that obs.
>>>
>>> Here is one solution:
>>>
>>> *------------------ begin example ------------------
>>> // create the example data
>>> input ///
>>> ID x y z
>>> 1 1 0 0
>>> 2 0 1 0
>>> 3 0 0 1
>>> end
>>>
>>> // an empty string "" is considered a missing value in Stata
>>> // so the first occurance of a 1 happens when one of the
>>> // variables is 1 and W is still missing
>>> gen str1 W = ""
>>> foreach var of varlist x y z {
>>> replace W = "`var'" if `var' == 1 & missing(W)
>>> }
>>>
>>> // admire the result
>>> list
>>> *------------------- end example -------------------
>>> * (For more on examples I sent to the Statalist see:
>>> * http://www.maartenbuis.nl/example_faq )
>>>
>>> Notice, that I gave the variable W the string storage type str1, which
>>> means a string with 1 character. This works in this example since all
>>> the variable names are only 1 character long. If in your real data the
>>> longest variable name contains say 10 characters, you would replace
>>> str1 with str10
>>>
> *
> * 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/


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