Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Confirming whether a variable is binary or continuous


From   daniel klein <klein.daniel.81@googlemail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Confirming whether a variable is binary or continuous
Date   Mon, 19 Mar 2012 10:13:29 +0100

Bert,

I do not want to comment on the statistical issues Cameron has raised
(tby the way, hanks for the literature Cameron). However I would like
to share some thoughts on the programming.

I think your syntax is not very convenient since the user is requiered
to specify (i) a complete varlist, (ii) all continuous variables in
that varlist and (iii) all binary variables in that varlist. So the
user has to specify the same information twice. This seems rather
burdensome and I do not think it is necessary. You can at least omit
one of the options, since a variable has to continuous if not
specified "binary" and the other way round. Further the options should
be optional, not required, since the program cannot be used if none of
the variables in varlist is binary (or continuous).

Here is a sketch that has, in my opinion, a more convenient syntax.
Note that the order of variables in varlist is not affected at all.

cap pr drop varcheck
pr varcheck
	vers 12.1
	
	syntax varlist(num) [, Continuous(varlist)]
	
	* check continuous in varlist
	if ("`continuous'" != "") {
		foreach c of loc continuous {
			if !(`: list c in varlist') {
				di as err "option continuous(): `c' not specified in varlist"
				e 198
			}
		}
	}
	
	* main loop
	foreach v of local varlist {
			
			// v specified as continuous
		loc cont : list v in continous
			
			// check levels
		if !(`cont') {
			cap as inlist(`v', 0, 1)
			loc cont = _rc
		}
		
			// checks
		if (`cont') {
			<do whatever you want to do with continous variables>
		}
		else {
			<do whatever you want to do with binary variables>
		}
	}
end

Best
Daniel

-- 
cap program drop varcheck
program varcheck, nclass

	syntax varlist, contvars(varlist) binaryvars(varlist)

	* Loop over all variables in varlist; this approach keeps the order
in -varlist- intact
	foreach v of local varlist {

		* (a) Is variable part of the variables specified in "contvars"?
		local contvar: list v in contvars	
	
		if `contvar'==1 {
			di "`v' is specified as continuous variable"
		}

		
		* (b) Is variable part of the variables specified in "binaryvars"?
		local propvar: list v in binaryvars
		
		if `propvar'==1 {
			di "`v' specified as binary variable"		
		}
	}
	
end
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index