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: Reorder variables in dataset, based on its values


From   daniel klein <[email protected]>
To   [email protected]
Subject   Re: st: Reorder variables in dataset, based on its values
Date   Mon, 17 Dec 2012 14:38:12 +0100

Pedro,

I am not sre why you want to do this, but here is my try. I
implemented a short program -vorter- (a combination of values, sort
and order). The syntax diagram is

vorter [+|-] varlist [in #] [, order_options]

where varlist is a list of 2 or more numeric variables, -in- specifies
the observation to be used (note that no range is allowed here), and
defaults to 1 (the first observation) if not specified,  and
order_options are any options used with -order-. For the + and - sign
see -help gsort-. Note that missing values in a variable will place
this variable at the very end.

Applied to your example

clear
input str5 othervariable v1 v2 v3 v4 v5
"obs1" 1 3 2 5 4
"obs2" 2 2 3 3 3
end

vorter -v1-v5 ,after(othervariable)

// Reorder by obseravtion 2 in ascending order

vorter v? in 2 ,after(othervariable)


Here is the program code (probably wil work with Stata 10 or higher)
that you can copy, paste into a dofile editor and save it under
vorter.ado in  into c:/ado/plus/v.

*! version 1.0.0 17dec2012 Daniel Klein

pr vorter
	vers 12.1
	
	// parse syntax
	gettoken ad : 0 ,p("+-")
	if inlist("`ad'", "+", "-") {
		loc 0 : subinstr loc 0 "`ad'" ""
		loc ad = 0 `ad' 1
	}
	else loc ad 1
	syntax varlist(num min = 2) [in/] [, *]

	// in
	if ("`in'" != "") {
		numlist "`in'"
		loc in `r(numlist)'
		if (`: word count `in'' > 1) {
			di as err "Obs. nos. out of range"
			e 198
		}
	}
	else loc in 1
	
	// order options
	if (`"`macval(options)'"' != "") loc options ,`options'
	
	// vorter
	m : mvorter(`in', "`varlist'", `ad')
	order `order' `options'
end

vers 12.1
m :
void mvorter(real scalar row, string scalar col, real scalar idx)
{
	real matrix sx
	sx = st_data(row, tokens(col))\ st_varindex(tokens(col))
	sx = sort(sx', idx)'[2, .]
	st_local("order", invtokens(st_varname(sx)))
}
end
e


Best
Daniel
-- 
Dear statalisters

I'm looking for a way to reorder variables in dataset, based on the
values of a specific observation. For example:

**IF MY DATASET IS:
clear*
input str5 othervariable v1 v2 v3 v4 v5
"obs1" 1 3 2 5 4
"obs2" 2 2 3 3 3
end

**I WANT THE DATASET ORDERED IN THIS FASHION
**(descending reordering, by observation 1)
order v4 v5 v2 v3 v1, after(othervariable)

Anyone out there know of any way to do this?

And if I wanted, instead, reorder by observation 2, or in ascending order?

Many thanks
*
*   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