Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

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

From |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

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

Date |
Sat, 10 Dec 2011 09:57:47 +0000 |

If you are interested in -ds-, check out -findname- (SJ). Go for the article in SJ 10-2 and the bug-fixed software from the SJ 10-4 files. As always, installing the software does not depend on a SJ subscription. dm0048_1 from http://www.stata-journal.com/software/sj10-4 SJ10-4 dm0048_1. Update: Finding variable names / Update: Finding variable names / by Nicholas J. Cox, Durham University, UK / Support: n.j.cox@durham.ac.uk / After installation, type help findname dm0048 from http://www.stata-journal.com/software/sj10-2 SJ10-2 dm0048. Finding variables / Finding variables / by Nicholas J. Cox, Durham University, UK / Support: n.j.cox@durham.ac.uk / After installation, type help findname -findname- can do what -ds- can and more. -ds- is and was an official command, but includes several user-written details. -findname- is user-written, a superset of -ds- with slightly different syntax. Nick On Sat, Dec 10, 2011 at 12:18 AM, Matthew White <mwhite@poverty-action.org> wrote: > 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*** On Fri, Dec 9, 2011 at 6:52 PM, Sarah Edgington <sedging@ucla.edu> wrote: >> 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? * * 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/

**References**:**st: is there a way to determine variable storage type within a dofile***From:*"Sarah Edgington" <sedging@ucla.edu>

**Re: st: is there a way to determine variable storage type within a dofile***From:*Matthew White <mwhite@poverty-action.org>

- Prev by Date:
**Re: st: is there a way to determine variable storage type within a dofile** - Next by Date:
**st: I) plot the probability (instead of the dummy) on the residuals // II) cluster test** - Previous by thread:
**Re: st: is there a way to determine variable storage type within a dofile** - Next by thread:
**st: I) plot the probability (instead of the dummy) on the residuals // II) cluster test** - Index(es):