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: Destring and split variable

From   Nick Cox <>
To   "" <>
Subject   Re: st: Destring and split variable
Date   Fri, 3 May 2013 13:47:44 +0100

You don't show any code for us to comment on, but neither -destring-
nor -split- is suitable for this problem.

-destring- is for numbers accidentally trapped in a string variable,
not quite your case in so far as (e.g.) "1 3 9" doesn't fit that

-split- is for splitting on delimiters, and you don't have delimiters
in all your values. When I first wrote -split- (and afterwards) I
wondered about extending -split- to split problems based purely on
location in string and decided that it would just complicate the
syntax when there are easier alternatives. -split- is StataCorp's
responsibility now, but they haven't moved in that direction either.

I guess you want first, third, _fifth_ digits in which case try

foreach j in 1 3 5 {
     gen digit`j'  = substr(Variable1, `j', 1)

If you want those results to be numeric then wrap the RHS in -real()-.

tab1 digit?

to check results carefully and follow with -destring- if needed.

On 3 May 2013 13:34, André Gyllenram <> wrote:
> Hello,
> I have a variable that looks like this:
> Variable1
> 10305
> 20607
> 1 3 9
> 1 4 5
> From this variable I want to create three new variables. The first one should be equal to the firs digit, the second should be equal to the third digit and the third should be equal to the third digit in variable1.
> I have tried to do this using the command destring and subinstr but i must do something wrong because it does not seem to work.
> Does anyone know how to do this?
> *
> *   For searches and help try:
> *
> *
> *

*   For searches and help try:

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