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]

st: AW: Find variable with first occurrence of a value in a list of variables


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: AW: Find variable with first occurrence of a value in a list of variables
Date   Tue, 13 Jul 2010 16:22:24 +0200

<> 



I do not think there is a canned solution, but have you had a look at
-egen-, functions -anymatch-, -anyvalue- and -anycount-? They do offer
partial solutions here.



HTH
Martin

-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Mike Lacy
Gesendet: Dienstag, 13. Juli 2010 15:34
An: [email protected]
Betreff: st: Find variable with first occurrence of a value in a list of
variables


I'm looking for a relatively easy way to find, for each observation, 
which variable in a list has the first occurrence of a particular 
value.  I was looking for a built-in solution, but not finding 
something, came up with a "roll your own" solution that seems less 
than ideal, and so am seeking suggestions

// Example data: For each case, find first occurrence of a "1" in a 
list of five variables.
set obs 10
forval i = 1/5 {
    gen byte x`i' = (runiform() > 0.6)
}
local value_of_interest 1
tempvar found
gen byte `found' = 0
gen str varname = ""
foreach v of varlist x1-x5 {
    replace varname  = "`v'" if (!`found') & (`v' == `value_of_interest')
    replace `found' = 1 if (`v' == `value_of_interest')
}
//

I'm interested in a simple and efficient approach to the general 
case, i.e., not assuming that the variable list of interest is 
numbered. Among other things, the looping approach here would seem a 
bit better if there was a way to terminate the search for each 
observation the loop when `found' ==1.

Suggestions?

Regards,
=-=-=-=-=-=-=-=-=-=-=-=-=
Mike Lacy, Assoc. Prof.
Soc. Dept., Colo. State. Univ.
Fort Collins CO 80523 USA
(970)-491-6721   


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/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/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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