Statalist The Stata Listserver

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

Re: st: trim spaces in postcode

From   Ronnie Babigumira <[email protected]>
To   [email protected]
Subject   Re: st: trim spaces in postcode
Date   Sat, 01 Apr 2006 14:38:42 +0200

Nick, thanks for the tip (I too have been specifying the string length when generating string variables).

A follow up question, I occasionally use -infile- and -input- both of which require that I explicitly give the length of string variables that may be in my data. Usually I start with a wild guess say str30 and then compress after reading the data in.

Is there something in newer versions of Stata that would save me from guessing the length of strings when using -infile- and -input-

Nick Cox wrote:

A quibble on the side:
As it happens I guess no British postcode
is longer than 8 characters, but in general this is dangerous, with greater
and lesser dangers.
Forget postcodes, and consider the possible
consequences of stipulating -str8-.
If I say
gen str8 whatever = "abcdefghi"
Stata assumes I mean what I say and that I trust it to do the best it can.
The consequence will be "abcdefgh" everywhere, and no warning message. The greater danger is thus loss of data.
If I say
gen str8 whatever = "foo"
the consequence is "foo" everywhere
and some wasted storage. That may be
trivial to you, but it is still avoidable.

I guess Emma has been using Stata for a while and started when you _had_ to specify a string type in generating a string variable. For a few versions
past, that has no longer been compulsory.
It is best, in fact, not to do it, as Stata is smarter at computing than you are.
If I say
gen something = "foo"
-something- will be born -str3-.
Nick [email protected]
Maurizio Gibin

Thank you Emma...
Easy and it's working!

gen str8 newvar=subinstr(oldvar," ","",.)

if oldvar is your current postcode variable and newvar is a new one.

I am trying to trim all the spaces in a code, in particular an english
unit postcode
but I still haven't found the correct way. I downloaded the ptseed
strip function but seems is not working.
Can anyone please help me? The database I am working on is quite
*   For searches and help try:

*   For searches and help try:

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