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: cleaning height data
From
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: cleaning height data
Date
Sat, 15 Feb 2014 21:05:59 +0000
I am fond of -split- but I wanted to advertise a different method and
a specific trick.
gen p1 = strpos(height, char(39))
gen p2 = strpos(height, char(34))
gen feet = real(substr(height, 1, p1 - 1))
gen inches = real(substr(height, p1 + 1, p2 - p1 - 1))
This method is pedestrian but that is its virtue too. Find the
positions of key characters and use functions to extract what you
want.
The trick is to use -char()- when wanting to use awkward characters.
For a cheatsheet, see -asciiplot- (SSC).
Nick
[email protected]
On 14 February 2014 21:21, Kathleen L. Carluzzo
<[email protected]> wrote:
> Hello, I am trying to clean height data that looks like this:
>
> height
> 5' 5"
> 5' 0.5"[PT S
> 5' 5"[verb
> 5' 2"
> 6' 0.5"[stat
>
> I want to convert height to inches; using the "split" command, I can split
> the variable at feet using the single quotation but can't split out the
> inches because the sign for inches is one double quotation mark ("), which
> gives me the error message "unmatched quote". For example,
>
> This works: split height, p(' )
> This does not work: split height, p(")
> Nor does this: strparse height, gen(height_in) parse(")
>
> I also tried variables on this, such as: split height, p(""")
>
> Any suggestions? Thanks!
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/