Statalist


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

st: RE: Function to identify data type


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Function to identify data type
Date   Tue, 26 Feb 2008 14:54:26 -0000

Maarten Buis has already given a good answer to your question. 
I agree with Maarten that learning about -foreach- is a very good idea. 
-for- has been obsolete since Stata 7. 

As far as your original approach was concerned, it could be made to 
work. The first thing to note is that the type of a variable and its
format are quite different properties. You want the type. 

With the auto data, 

sysuse auto 
di "`: type make'" 

will show "str18". Thus extracting the first three characters will 
reveal whether a variable is of string type. However, testing for string

type on the -replace- command is testing too late. You need to test
before 
you try the -replace-

You would need something like 

foreach X of var X1-X50 { 
	local type = substr("`: type `X''", 1, 3) 
	if "`type'" == "str" { 
		replace `X'_new = 1 if `X' == "New" 
	} 
} 

In practice, I would rather use -capture confirm str var- or (indeed)
-ds- (like Maarten), but 
this is just to show that your main idea can be made to work. The stuff
you wanted 
is documented at -help extended_fcn-. 

Nick
n.j.cox@durham.ac.uk 

alvine.bissery 

I would like to generate new variables using this command:

for var  X1-X50 :replace X_new=1 if X=="New" 

But some Xi variables are numeric because empty

This command stops when one of the Xi is numeric 

How can I ask to execute this command only for string Xi variables ?  Is
there a "format" function which returns the type of the variable ? If
this
exists, I could use:

for var  X1-X50 :replace X_new=1 if X=="New" & format(X)==string


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index