Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: string function

From   "David Harrison" <>
To   <>
Subject   st: RE: string function
Date   Thu, 9 Mar 2006 09:00:08 -0000

I have produced something similar before... and with a few tweaks, here
it is. The following code strips numbers and decimal points from the
start of the lab variable (storing these in num) until it finds any
character that is not a number or a decimal point (storing the remainder
in unit).

gen num=""
gen unit=lab
tempvar c l
gen `c'=""
gen `l'=length(lab)
su `l', meanonly
local maxl=r(max)
forvalues i=1/`maxl' {
	replace `c'=substr(unit,1,1)
	replace num=num+`c' if strpos("0123456789.",`c')
	replace unit=substr(unit,2,.) if strpos("0123456789.",`c')


-----Original Message-----
[] On Behalf Of Andy Choi
Sent: 08 March 2006 21:51
Subject: st: string function

I have a large file with patient's labs.  The lab variable contains the
lab result (a number) with the units (for example mg/dl) included.
There are many variations to the way the units are reported: they may be
capitalized or in parentheses or the units themselves may contain

I would like to create a variable that includes only the lab result.
And if possible a separate variable with the units.


This email has been scanned by the MessageLabs Email Security System.
For more information please visit 

*   For searches and help try:

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