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
Matthew White <[email protected]>
To
[email protected]
Subject
Re: st: is there a way to determine variable storage type within a dofile
Date
Fri, 9 Dec 2011 19:18:14 -0500
Hi Sarah,
You can use the -:type- extended macro function. For example:
sysuse auto, clear
local type : type foreign
assigns "byte" to local `type'. For more information on extended macro
function, see -help extended_fcn- and -help macrolists-.
That said, you might want to try other approaches. -ds, has(type
string)- will save the names of all string variables in -r(varlist)-.
Then you can loop over this variable list.
In this case, I might try something like this:
***BEGIN***
label define yesno 0 "no" 1 "yes"
ds, has(type string)
foreach var in `r(varlist)' {
levelsof `var'
if `"`r(levels)'"' == `"`"X"'"' {
replace `var' = "1" if `var' == "X"
replace `var' = "0" if `var' == ""
destring `var', replace
label values `var' yesno
}
}
ds, has(type numeric)
foreach var in `r(varlist)' {
count if `var' == .
if r(N) == _N {
replace `var' = 0
label values `var' yesno
}
}
***END***
Best,
Matt
On Fri, Dec 9, 2011 at 6:52 PM, Sarah Edgington <[email protected]> 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/
--
Matthew White
Data Coordinator
Innovations for Poverty Action
101 Whitney Avenue, New Haven, CT 06510 USA
+1 434-305-9861
www.poverty-action.org
*
* 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/