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

st: RE: RE: question about converting string vars to numerical vars


From   "Joy Chen" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: question about converting string vars to numerical vars
Date   Wed, 26 Jan 2005 18:12:42 +0100

Thanks, David. It works!!! That is great. I really appreciate your help.

Joy Chen
Munich Graduate School of Economics
University of Munich
Kaulbachstra�e 45
D-80539 Munich / Germany
Email: [email protected]

-----Original Message-----
From: [email protected]
[mailto:[email protected]]On Behalf Of David Harrison
Sent: Mittwoch, 26. Januar 2005 18:01
To: [email protected]
Subject: st: RE: question about converting string vars to numerical vars


You have -encode-ed the variables - that means that each distinct value is
mapped to an integer and given a label. To make a true numeric variable you
need to use the function -real()- (as stated in -help encode-).
Unfortunately, this doesn't recognise commas for decimals, so you will need
to substitute in periods, e.g.

gen e1=real(subinstr(v1,",",".",1))

You can then set the display format for e1 to use commas if you so desire.

David
[email protected]

-----Original Message-----
From: Joy Chen [mailto:[email protected]]
Sent: 26 January 2005 16:43
To: Stata List
Subject: st: question about converting string vars to numerical vars


Hi,

I wonder if anyone knows to how to solve this problem. I have a .txt data
and I used -insheet using data1.txt, t- to import it into stata. It shows
that the display type of the variables are string variable. I tried to
convert them into numerical vars. I replaced #.# in -format-  command with
#,#  as it is European decimal symbol (the comma) used in data. After many
trials, it still gives me strange result. The mean, max and min of the
variables are far away from the truth.  For example, the mean for e1 should
be -0,27145726 but stata gives 1,64654, for f1 the true mean is 0,49880390,
in stata is  2,299758 .

Did I make some mistake in the code? Can anyone give me a hand? Many thanks.

 des v1 v2

              storage  display     value
variable name   type   format      label      variable label
----------------------------------------------------------------------------
---
v1              str11  %11s
v2              str11  %11s

 list v1 v2 in 1/10

     +--------------------------+
     |          v1           v2 |
     |--------------------------|
  1. | -0,22561362   1,02236428 |
  2. | -0,39622573   1,53583639 |
  3. | -0,24692422   0,25572723 |
  4. | -0,31088329   0,51172709 |
  5. | -0,31088329   0,25589082 |
     |--------------------------|
  6. | -0,28955905   0,25583627 |
  7. | -0,31088329   0,25589082 |
  8. | -0,24692422   0,25572723 |
  9. | -0,15106225   0,12774791 |
 10. | -0,22561362   0,51129102 |
     +--------------------------+

 encode v1 in 1/10 , gen (e1)

. encode v2 in 1/10 , gen (f1)

. format %09,2f  e1  f 1

. set dp comma

   list e1 f1 in 1/10

     +-----------------------+
     |        e1          f1 |
     |-----------------------|
  1. | -0,225613   1,0223642 |
  2. | -0,396225   1,5358363 |
  3. | -0,246924   0,2557272 |
  4. | -0,310883   0,5117270 |
  5. | -0,310883   0,2558908 |
     |-----------------------|
  6. | -0,289559   0,2558362 |
  7. | -0,310883   0,2558908 |
  8. | -0,246924   0,2557272 |
  9. | -0,151062   0,1277479 |
 10. | -0,225613   0,5112910 |
     +----------------------

des e1  f1

Contains data
  obs:           10
 vars:             4
 size:         4.692 (99,6% of memory free)
----------------------------------------------------------------------------
---
              storage  display     value
variable name   type   format      label      variable label
----------------------------------------------------------------------------
---
e1               long   %09,2f      e1
f1               long   %09,2f      f1
----------------------------------------------------------------------------
---
   . su e1 f1 in 1/10

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
          e1 |        10         3,6    1,646545          1          6
          f1 |        10         4,2    2,299758          1          8


Joy Chen
Munich Graduate School of Economics
University of Munich
Kaulbachstra�e 45
D-80539 Munich / Germany
Email: [email protected]


*
*   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/


*
*   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