extract last 4 numbers from zip code

  • Follow


I have a string variable (ZIP) containing zip codes, some with just
five numbers and some in the 00000-0000 format.

How can I delete from ZIP the "-0000" portion (when it occurs) and
paste it into a new variable, but without the "-"?

This is what I have so far:

STRING ziplast4(A25).
COMPUTE ziplast4=SUBSTR(zip,INDEX(zip,"-")+1).

The above moves the last four numbers to ziplast4, but doesn't delete
it from zip. Also, when a zip has just 5 numbers, it moves the five
numbers to ziplast4, when it should just ignore them.

e.g.,

23510-1506 becomes 1506 (and 23510-1506 stays in zip)
20109 becomes 20109  (and 20109 stays in zip)

Thank you.

0
Reply 0 2/22/2011 11:19:54 PM

On 22/02/2011 6:19 PM, 0 1 wrote:
> I have a string variable (ZIP) containing zip codes, some with just
> five numbers and some in the 00000-0000 format.
>
> How can I delete from ZIP the "-0000" portion (when it occurs) and
> paste it into a new variable, but without the "-"?
>
> This is what I have so far:
>
> STRING ziplast4(A25).
> COMPUTE ziplast4=SUBSTR(zip,INDEX(zip,"-")+1).
>
> The above moves the last four numbers to ziplast4, but doesn't delete
> it from zip. Also, when a zip has just 5 numbers, it moves the five
> numbers to ziplast4, when it should just ignore them.
>
> e.g.,
>
> 23510-1506 becomes 1506 (and 23510-1506 stays in zip)
> 20109 becomes 20109  (and 20109 stays in zip)
>
> Thank you.
>

I don't have SPSS on this machine, so the following is untested.

STRING ziplast4(A4).
do if INDEX(zip,"-") NE 0 . /* hyphen is found .
- COMPUTE ziplast4=SUBSTR(zip,INDEX(zip,"-")+1).
- COMPUTE zip = SUBSTR(zip,1,5). /* first 5 characters only.
end if.

-- 
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/Home
"When all else fails, RTFM."
0
Reply Bruce 2/23/2011 1:59:00 AM


Bruce:

Thank you. That successfully extracted (and deleted) the -0000 from
zips that had 00000-0000, and copied the 0000 into ziplast4.

But when zip had 00000, it copied the whole thing over to ziplast4.
This wasn't a big deal and I can easily delete these. It was the
lingering -0000s in zip that I was most concerned with getting rid of.

Thanks.
0
Reply 0 2/25/2011 8:25:56 PM

On 25/02/2011 3:25 PM, 0 1 wrote:
> Bruce:
>
> Thank you. That successfully extracted (and deleted) the -0000 from
> zips that had 00000-0000, and copied the 0000 into ziplast4.
>
> But when zip had 00000, it copied the whole thing over to ziplast4.
> This wasn't a big deal and I can easily delete these. It was the
> lingering -0000s in zip that I was most concerned with getting rid of.
>
> Thanks.

How can that be?  When zip = 00000, there is no hyphen, and the 
condition on the DO IF below should be false.  Do your 00000 cases 
include a hyphen?

STRING ziplast4(A4).
do if INDEX(zip,"-") NE 0 . /* hyphen is found .
- COMPUTE ziplast4=SUBSTR(zip,INDEX(zip,"-")+1).
- COMPUTE zip = SUBSTR(zip,1,5). /* first 5 characters only.
end if.


-- 
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/Home
"When all else fails, RTFM."
0
Reply Bruce 2/25/2011 11:33:31 PM

3 Replies
1040 Views

(page loaded in 0.124 seconds)

Similiar Articles:













7/22/2012 8:26:42 PM


Reply: