[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:35:25 +0100 |

Try some variation on . infile a b str80 c d e using my.csv where the "str80" must precede the variable name and where "80" can be replaced by the largest number of digits. 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/

**Follow-Ups**:**Re: st: RE: RE: RE: RE: inputting string variables in comma-separated files***From:*"Surajit Das" <surajitdas@fastmail.fm>

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

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