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 08:58:13 +0000

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.
Thanks to both of you again

Prof. Dr. Haluk Vahaboğlu






----------------------------------------
> Date: Thu, 21 Nov 2013 09:46:07 +0100
> Subject: Re: st: Sort rows and give variable name of the observation
> From: [email protected]
> To: [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
>
> Hope this helps,
> Maarten
>
> ---------------------------------
> Maarten L. Buis
> WZB
> Reichpietschufer 50
> 10785 Berlin
> Germany
>
> http://www.maartenbuis.nl
> ---------------------------------
> *
> * 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