|
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: macro for the type of a variable (string/numeric)
At 11:01 AM 4/21/2008, you wrote:
The following program is a kluge to determine the type of a variable
and to assign a string to a local macro which tells us the type. Is
there some better way? For instance, "return list" after "describe
myvar" or "codebook myvar" does not tell us this.
program returnType
args THISVAR
* Determine whether it is a numeric or string variable.
* kluge: if THISVAR is numeric with all missing, this
program thinks it's string.
quietly: summarize `THISVAR'
if r(N)==0 {
local thisTYPE="string"
}
else {
local thisTYPE="numeric"
}
display "`thisTYPE'"
end
[...]
Perhaps what you want is...
local typ : type `THISVAR'
This will tell the specific type. If you want to be more general,
then test whether substr("`typ'", 1,3) == "str"
I wouldn't bother making this into a program.
HTH
--David
*
* 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/