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

st: RE: RE: reduce a variables numbers of digits to 4 counting from left


From   "Nick Winter" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: reduce a variables numbers of digits to 4 counting from left
Date   Thu, 16 Jan 2003 09:56:04 -0500

> -----Original Message-----
> From: [email protected] [mailto:[email protected]] 
> Sent: Thursday, January 16, 2003 9:49 AM
> To: [email protected]
> Subject: st: RE: reduce a variables numbers of digits to 4 
> counting from left
> 
> 
> If there aren't any decimal values in your numeric variable, you may
> destring the variable, i.e.
> 
>    destring numvar, gen(strvar)
> 
> followed by a invoking the substr() function
> 
>    replace strvar = substr(strvar,1,4)
> 

The first step here is going the wrong way, I think. -destring- turns a
string variable into a numeric variable; here we want the reverse.

	gen str1 strvar=""
	replace strvar = substr(real(numvar),1,4)

Should do the trick in (almost) one step.

In St8a8, it's actually one step:

	gen str strvar = substr(real(numvar),1,4)


Nick Winter
*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index