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

Re: st: macro for the type of a variable (string/numeric)

From   David Kantor <>
Subject   Re: st: macro for the type of a variable (string/numeric)
Date   Mon, 21 Apr 2008 13:25:30 -0400

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
* 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'"
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.


* For searches and help try:

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