Statalist The Stata Listserver


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

Re: Re: st: vreverse when not all possible values of a variable areused by respondents


From   n j cox <[email protected]>
To   [email protected]
Subject   Re: Re: st: vreverse when not all possible values of a variable areused by respondents
Date   Thu, 31 May 2007 22:15:08 +0100

This is cleaned up a bit from my previous posting:

*! 1.0.1 NJC 31 May 2007
program reversev
	version 9
	syntax varlist(numeric) [if] [in] , min(int) max(int)

	marksample touse
	qui count if `touse'
	if r(N) == 0 error 2000

	if `min' > `max' {
		di as err "`min' > `max'"
		exit 498
	}
	else if `min' == `max' {
		di as err "`min' = `max'"
		exit 498
	}

	foreach v of local varlist {
		capture assert `v' == int(`v') if `touse'
		if _rc {
			di as err "non-integer values in `v'"
			exit 498
		}	
	}

	quietly foreach v of local varlist {
		clonevar new_`v' = `v' if `touse'
 		replace new_`v' = `min' + `max' - `v'
	
		forval i = `min'/`max' {
			local j = `min' + `max' - `i'
			local lbl : label (`v') `j'
			label def new_`v' `i' `"`lbl'"', modify
		}
	
		label val new_`v' new_`v'
	}
end

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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