[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: sorting a variable list within each obs.(row)

From   "Nick Cox" <>
To   <>
Subject   st: RE: RE: sorting a variable list within each obs.(row)
Date   Tue, 26 Aug 2008 12:12:57 +0100

You can do it much more directly. 

gen rowposmin = . 

qui foreach v of varlist v? { 
	replace rowposmin = min(rowposmin, `v') if `v' > 0 

-egen- is fine for the hundred or so problems for which canned recipes
are available. For the thousands of equally plausible problems of
similar kind that might arise, it is a bit of distraction. 
Rajesh Tharyan

One way would be to use egen. -help egen - however, the negative values
be picked up. To avoid that I create a new variable for each old one
missing values replacing values<0.

* create new variables & replace negative with missing values

foreach var of varlist  v1- v4 {
gen `var'i=`var'
replace `var'i=. if `var'i<0

* use egen  to get the minimum of the row
egen min=rowmin(v1i- v4i)

* drop unnecessary variables
drop v*i

Murali Kuchibhotla

      I have a dataset that looks like this:

Obs.  V1  V2  V3  V4
1     -1  -2  3   5
2     4    5  7   9

I would like to create a variable that picks out the minimum positive
from each row, i.e. one that picks out 3 from row 1 and 4 from row 2.How
accomplish this? 

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index