Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: Destring and split variable


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
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
recipe.

-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()-.
Alternatively

tab1 digit?

to check results carefully and follow with -destring- if needed.
Nick
njcoxstata@gmail.com


On 3 May 2013 13:34, André Gyllenram <a_gyllenram@hotmail.com> 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:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index