Bookmark and Share

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


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

Re: st: Substring problem


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Substring problem
Date   Mon, 4 Mar 2013 13:57:20 +0000

One way to do this is with -moss- (SSC).


. moss C , match("([0-9.]+)") regex prefix(C)

will strip out numbers (possibly with decimal points).

But in your example something like

gen Bnum = real(word(B, 1))

should work as should

gen C2 = subinstr(C, "(", "", .)
gen Cnum = real(word(C2, 1))

Nick

On Mon, Mar 4, 2013 at 1:38 PM, Alberto R Osella
<[email protected]> wrote:

> My data look like this.
>                                       A B               C
>
>   1. | Latte di vacca parzial. scremato    200 g     (92 kcal)
>   2. |                  Caffe' in tazza                   30 g (0 kcal)
>   3. |                         Sogliola                    150 g (124 kcal)
>   4. |      Olio di oliva extra vergine          15 g    (134 kcal)
>   5. |                 Cicoria di campo            200 g     (20 kcal)
>
> These are three strinf variables coming from a software.  I need to
> transform the last two string variables into numeric ones but
> I need only a substring of them ( not 200 g but 200, not (92 kcal) but 92.
> I've tried  but I was not able to find a solution.
> I'm using Stata 12.1 on W7.
*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index