Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: separating components of a string variable

From   Nick Cox <>
Subject   Re: st: separating components of a string variable
Date   Mon, 1 Oct 2012 20:40:27 +0100

gen whatever = substr(id, 2, 2)

gen last3 = substr(id, -3, 3)

use -real()- as well if desired.

See for the functions that "everyone should know":

Nicholas J. Cox. 2011. Speaking Stata: Fun and fluency with functions.
The Stata Journal
11(3): 460-471

Abstract.  Functions are the unsung heroes of Stata. This column is a
tour of functions that might easily be missed or underestimated, with
a potpourri of tips, tricks, and examples for a wide range of basic

On Mon, Oct 1, 2012 at 8:32 PM, Mahbubeh Parsaeian
<> wrote:

> I have a question about separating the component of a string variable.
> Suppose you have a string variable which is an id variable and consists of 9 numbers. The second and third numbers introduce the province and you want
> to separate this part of the string variable and save it as a new
> variable.
> I also have another question. I have an id number such as 123456 and I want
> to separate 3 numbers from the right side (456).
> Would you please help me to find good solutions for these questions?
> Thanks for your attention.
> *
> *   For searches and help try:
> *
> *
> *
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index