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

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

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

Date |
Thu, 20 May 2004 16:19:48 +0100 |

In this circumstance, and assuming that there is no way to tell from the data, or otherwise, how many digits there should be, you must return to your data file. Adrian Mander suggested a solution which assumed that you were using Excel. Another is to surround strings with "". Nick n.j.cox@durham.ac.uk > -----Original Message----- > From: owner-statalist@hsphsun2.harvard.edu > [mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of > Rebecca Kalmus > Sent: 20 May 2004 16:13 > To: statalist@hsphsun2.harvard.edu > Subject: st: RE: RE: RE: inputting string variables in comma-separated > files > > > 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/ > * * 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/

- Prev by Date:
**Re: st: RE: RE: inputting string variables in comma-separatedfiles** - Next by Date:
**st: RE: Using postfile** - Previous by thread:
**st: Using postfile** - Next by thread:
**st: RE: RE: RE: RE: inputting string variables in comma-separated files** - Index(es):

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