Stata: How to check whether a variable is string

Standard

I often find that commands don’t work, because I have forgotten the “” to denote a string or the other way around.

Thus if found this useful:

	if(substr("`: type VARIABLE'",1,3)=="str"){ 
		dis "Do something with a string variable"
	}
	else{
		dis "Do something with a different type of variable"
	}

P.S.
This uses 1) the macro extend function:

 local locname : type VARNAME

and 2) the string function:

substr(STRING,STARTPOSITION,NUMBEROFSTRINGSTOBESELECTED)
Advertisements