Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: Re: how to extract numeric part of a string


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: Re: how to extract numeric part of a string
Date   Tue, 17 Dec 2002 12:50:07 -0000

Nick Cox
> 
> -charlist-
> ==========
> 
> Finally, this utility may be of use or interest:
> 
> =========== begin
> program def charlist, rclass
> *! NJC 1.0.0 17 Dec 2002
> 	version 7
> 	syntax varname(string) [if] [in]
> 	marksample touse, novarlist
> 	* not 0: see [P] file formats .dta
> 	forval i = 1/255 {
> 		capture assert index(`varlist', char(`i')) == 
> 0 if `touse'
> 		if _rc {
> 			local c = char(`i')
> 			local charlist "`charlist'`c' "
> 			local numlist "`numlist'`i' "
> 		}
> 	}
> 	di as text "`charlist'"
> 	return local charlist "`charlist'"
> 	return local numlist "`numlist'"
> end
> ========== end
> 

There's a bug in this version of -charlist-, 
mirroring a longstanding misfeature (bug?) 
in Stata itself. User-programmers may find 
a short discussion of interest; others 
may want, at most, to know that the bug is 
fixed. 

In accumulating characters actually met 
I added them to a list by 

local c = char(`i')
local charlist "`charlist'`c' "

Why didn't I go 

local charlist = "`charlist'" + char(`i') 

Because this fails on strings longer than 
the allowed limit in string expressions. 

However, if char(`i') is char(32), i.e. 
a space " ", 

local c = char(32) 

will _not_ put a space in the local 
macro -c-. If you don't believe this, 
try 

local c = char(32) 
di "|`c'|" 

This is a longstanding behaviour 
of Stata, which strips leading spaces
from string expressions. I have 
been told its historical rationale, 
which I can't recall at present. 
Whatever, a work-around is this: 

if char(`i') == " " { 
	local c " " 
} 
else local c = char(`i') 
local charlist "`charlist'`c' " 

Note that 

local c = cond(`i' == 32, " ", char(`i')) 

would not work for exactly the 
same reason. 

I'll post a fixed version 
of -charlist- to SSC. 

Nick 
n.j.cox@durham.ac.uk 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index