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

st: RE: RE: RE: inputting string variables in comma-separated files


From   Rebecca Kalmus <kalmus@fas.harvard.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: RE: RE: RE: inputting string variables in comma-separated files
Date   Thu, 20 May 2004 11:12:35 -0400 (EDT)

That's helpful, thanks.  However, that only works if the variable has the
same number of digits for all observations.  Suppose one observation is
001 and another is 0001, both of which are read into Stata as 1, and no
observation has more than four digits for this variable.  In that case
tostring will turn this variable into 0001 for both these observations.

Rebecca Kalmus

*********************************************************************

I think what Nick was trying to get at is that you can recreate your lost
leading zeroes by using the -format()- option in -tostring- as illustrated
below.
Joseph Coveney
--------------------------------------------------------------------------------
. clear
. set obs 5
obs was 0, now 5
. generate int x = 10^(_n - 1)  // <- original comma-separated values
. tostring x, generate(sx) format(%05.0f)  // note the format--leading
zeroes
sx generated as str5
. clist, noobs
       x         sx
       1      00001
      10      00010
     100      00100
    1000      01000
   10000      10000
--------------------------------------------------------------------------------
From: Rebecca Kalmus wrote:
Once I've read the comma-separated file into Stata, Stata has already read
the variable as numeric.  The leading zeros are already gone, so
converting the variable to string using tostring doesn't help.
In the past I've gotten around this problem by converting the
comma-separated file to Stata format using Stat/transfer, with which I can
specify that the variable should be read as string.  However, it seems
like there should be a more straightforward solution.
Rebecca Kalmus
> >From   "Nick Cox" <n.j.cox@durham.ac.uk>
> To   <statalist@hsphsun2.harvard.edu>
> Subject   st: RE: inputting string variables in comma-separated files
> Date   Tue, 18 May 2004 19:46:19 +0100
>
> A good bet in this circumstance is to make
> it a string variable again using -tostring-.
> Make sure you specify a format which produces
> leading zeros.
>
> 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