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 13:49:35 +0000

A more straightforward solution. Nick, thanks again for enhancing our understanding Stata.
Prof. Dr. Haluk Vahaboğlu


----------------------------------------
> Date: Thu, 21 Nov 2013 12:35:37 +0000
> Subject: Re: st: Sort rows and give variable name of the observation
> From: [email protected]
> To: [email protected]
>
> There is a simpler solution using similar ideas. This two-line
> solution can also be reduced to one line. but only rather awkwardly as
> far as I can see.
>
> . clear
>
> . input ID x y z
>
> ID x y z
> 1. 1 1 0 0
> 2. 2 0 1 0
> 3. 3 0 0 1
> 4. 4 0 1 1
> 5. 5 1 0 1
> 6. end
>
> .
> . egen all = concat(x y z)
>
> . gen which = word("x y z", strpos(all, "1"))
>
> . list
>
> +------------------------------+
> | ID x y z all which |
> |------------------------------|
> 1. | 1 1 0 0 100 x |
> 2. | 2 0 1 0 010 y |
> 3. | 3 0 0 1 001 z |
> 4. | 4 0 1 1 011 y |
> 5. | 5 1 0 1 101 x |
> +------------------------------+
>
> Nick
> [email protected]
>
>
> On 21 November 2013 08:32, A Loumiotis <[email protected]> wrote:
>> Dear Haluk,
>>
>> I assume that the last value of W is "z" and not "y" as you reported.
>> The code below uses the base function of -egenmore- that is available
>> from ssc.
>>
>> **** Code starts ****
>> clear
>> input ID x y z
>> 1 1 0 0
>> 2 0 1 0
>> 3 0 0 1
>> 4 0 1 1
>> 5 1 0 1
>> end
>> replace y=2 if y==1
>> replace z=4 if z==1
>> egen isum=rowtotal(x y z)
>> egen bsum=base(isum)
>> gen rbsum=reverse(bsum)
>> gen first=strpos(rbsum,"1")
>> gen W="x" if first==1
>> replace W="y" if first==2
>> replace W="z" if first==3
>> replace y=1 if y==2
>> replace z=1 if z==4
>> list ID x y z W
>> **** Code ends ****
>>
>> Hope this helps,
>> Antonis
>>
>> On Thu, Nov 21, 2013 at 8:19 AM, Haluk Vahaboglu <[email protected]> wrote:
>
>>> I use Stata 12.1 64 for
>>> linux.
>>> 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.
>>>
>>> In the end I hope to get a
>>> variable like this:
>>> ID x y z W
>>> 1 1 0 0 x
>>> 2 0 1 0 y
>>> 3 0 0 1 y
>>> This problem could be
>>> solved by giving the variable name instead of the value of the obs.
>>> to the output of -rowsort- (by Nicholas J. Cox available in ssc)
>>> command, may be, but I could not figure out.
> *
> * 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