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]
st: Re: keep if variable is string
From
"Joseph Coveney" <[email protected]>
To
<[email protected]>
Subject
st: Re: keep if variable is string
Date
Sat, 7 Jan 2012 13:55:28 +0900
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/