Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: is there a way to determine variable storage type within a dofile


From   Phil Clayton <[email protected]>
To   [email protected]
Subject   Re: st: is there a way to determine variable storage type within a dofile
Date   Sat, 10 Dec 2011 11:19:25 +1100

Here are a couple of options:

---------------------------------------------
sysuse auto, clear

* use extended functions to determine storage type
* (see -help extended_fcn- for variations)
local maketype: type make
display "`maketype'"

* string vs numeric variable (see -help confirm- for variations)
describe, varlist
foreach var of varlist `r(varlist)' {
	capture confirm numeric variable `var'
	if !_rc display "`var' is a numeric variable"
	else display "`var' is a string variable"
}
---------------------------------------------

Phil

On 10/12/2011, at 10:52 AM, Sarah Edgington wrote:

> Hello all,
> 
> Is there a way to get Stata to return the storage type for a variable?  I
> know I can use -describe- to visually see the storage type, but I would like
> to use the information in an if statement as part of a loop.  Basically, I
> guess I'm looking for something conceptually like the isreal function in
> Mata.
> 
> I am working on importing and cleaning some data that began its life as an
> Excel spreadsheet.  I have a series of columns in the original sheet that
> either have an X in them or are blank and I'm trying to turn these into
> zero-one indicators in my final dataset.  So I'm using -foreach- to loop
> through the varlist of each of these indicator variables, changing X to 1
> and missing to zero, then using -destring- to create the final desired byte
> variable.  The problem is that sometimes a column has no Xs and gets
> imported as a byte variable (with all observations missing).  So when I get
> to those variables my loop exits due to type mismatch.  I can work around
> this with capture but this is not the first time for this project that I've
> found myself thinking that I want to do a particular thing just with string
> variables or just numeric variables.  If I were only importing the data once
> I'd just do it the hard way and make a list of the string variables by hand,
> but since I'm setting up dofiles to read in new files as we get them, I
> anticipate that exactly which variables get imported with which storage
> types will change over time.  In general it seems like I might be able
> construct cleaner and more manageable code if there were some command that
> would return the storage type for a variable that I could use to create
> lists of variables of a given type.  Or is there some other obvious trick
> I'm missing that would make constructing this type of code easier?
> 
> Thanks.
> 
> -Sarah Edgington
> 
> *
> *   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/


*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index