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

Re: st: chopping suffixes off values of a string variable

From   "Nick Cox" <>
To   <>
Subject   Re: st: chopping suffixes off values of a string variable
Date   Thu, 11 Sep 2003 19:21:57 +0100

Stephen Jenkins 

I have a string variable where values
have format 2 letters, followed by 6 digits,
followed by a letter. I'd like to create a new 
variable with values for each obs equal to the
old value except that the final letter of the
value is removed. That final suffix letter can 
range from "A" to "Z", in principle.

I suspect that there is an easy way of doing this,
but I can't think of it (nor did various searches
using -findit- reveal an easy way).  [I can only
think of a roundabout way based on creating a macro with
an alpha list in it, and then doing some looping using
subinstr function.]  Suggestions please.

(Illustration) how to convert values for
variable 'ni_number' to values of NEW_VAR,
as follows?

  | ni_number    NEW_VAR 
  | ZX223571C   ZX223571 
  | LR796192A   LR796192 
  | NE384662C   NE384662 
  | LL283409A   LL283409 
  | ZS663720B   ZS663720 
  | ZW878830B   ZW878830 
  | NM175824B   NM175824 
  | YM212595C   YM212595 
  | TA602684D   TA602684 

>>> gen NEW_VAR = substr(ni_number,1,length(ni_number)-1) 

*   For searches and help try:

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