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

st: RE: RE: Truncating data


From   "Nichols, Austin" <ANichols@ui.urban.org>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: Truncating data
Date   Wed, 8 Sep 2004 17:03:58 -0400

The proposed solution works well if every observation is 
exactly 4 digits, but if you have a variable whose length 
varies and you really want the first 2 digits, try:

. gen byte x2 = real(substr(string(x1,"%12.0f"),1,2))
. li x1 x2, noo clean
            x1       x2
            12       12  
          1234       12  
       1234567       12  
    1234567890       12  

If it is a string variable, you can:
. gen byte x2 = real(substr(x1,1,2))

-----Original Message-----
From: Hulley, Benjamin (NIH/NCI) [mailto:hulleyb@mail.nih.gov]
Sent: Wednesday, September 08, 2004 4:45 PM
To: 'statalist@hsphsun2.harvard.edu'
Subject: st: RE: Truncating data


Assuming x1 is numeric and all observations are 4 digits you could

gen x2 = int(x1/100)

-Ben

*******************
NCI/DCEG/GEB
Room: EPS 7007
Tel: (301) 402-1622
Fax: (301) 402-4489
*
*   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/
*
*   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