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   A Loumiotis <[email protected]>
To   [email protected]
Subject   Re: st: Sort rows and give variable name of the observation
Date   Thu, 21 Nov 2013 10:32:52 +0200

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:
> P { margin-bottom: 0.21cm; }
>
>
> Dear Stata listers,
> Apologize taking your time
> with this simple question.
> 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.
> Any help would be
> appreciated.
>
>
>
> Prof. Dr. Haluk Vahaboğlu
>
> *
> *   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