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: Re: keep if variable is string


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Re: keep if variable is string
Date   Sat, 7 Jan 2012 08:17:02 +0000

ds, has(type string)
keep `r(varlist)'

See also -findname- (SJ).

Nick

On Sat, Jan 7, 2012 at 4:55 AM, Joseph Coveney <jcoveney@bigplanet.com> wrote:
> Shubhabrata Mukherjee wrote:
>
>  I am using --insheet-- to read in a big file where some variables are numeric
> and some are string.
>
>  How can I keep the variables that are string only?
>
> --------------------------------------------------------------------------------
>
> After loading in the spreadsheet, you can drop the numeric variables with
> something like the loop in the following:
>
> sysuse auto, clear
> foreach var of varlist _all {
>        local variable_type : type `var'
>        if strpos("`variable_type'", "str") == 1 continue
>        display in smcl as text "`var' is numeric--dropping `var'"
>        drop `var'
> }
> exit
>
> A caution:  for some reason, -insheet- does not respect quoted numerical values,
> such as "01".  It reads them in as numeric, stripping any leading zeros.
>
> Joseph Coveney
>
> . clear *
>
> . set more off
>
> . set obs 3
> obs was 0, now 3
>
> . generate str a = string(_n, "%02.0f")
>
> . generate str b = "B"
>
> . list, noobs
>
>  +--------+
>  |  a   b |
>  |--------|
>  | 01   B |
>  | 02   B |
>  | 03   B |
>  +--------+
>
> . describe
>
> Contains data
>  obs:             3
>  vars:             2
>  size:            21 (99.9% of memory free)
> ----------------------------------------------------------------------
>              storage  display     value
> variable name   type   format      label      variable label
> ----------------------------------------------------------------------
> a               str2   %9s
> b               str1   %9s
> ----------------------------------------------------------------------
> Sorted by:
>     Note:  dataset has changed since last saved
>
> . tempfile tmpfil0
>
> . quietly outsheet using `tmpfil0', names quote
>
> . type "`tmpfil0'"
> a       b
> "01"    "B"
> "02"    "B"
> "03"    "B"
>
> . insheet using `tmpfil0', clear
> (2 vars, 3 obs)
>
> . list, noobs
>
>  +-------+
>  | a   b |
>  |-------|
>  | 1   B |
>  | 2   B |
>  | 3   B |
>  +-------+
>
> . describe
>
> Contains data
>  obs:             3
>  vars:             2
>  size:            18 (99.9% of memory free)
> ----------------------------------------------------------------------
>              storage  display     value
> variable name   type   format      label      variable label
> ----------------------------------------------------------------------
> a               byte   %8.0g
> b               str1   %9s
> ----------------------------------------------------------------------
> Sorted by:
>     Note:  dataset has changed since last saved
>
> . exit
>
> end of do-file
>

*
*   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