Statalist


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

Re: st: Extracting Numbers from Strings


From   Richard Goldstein <[email protected]>
To   [email protected]
Subject   Re: st: Extracting Numbers from Strings
Date   Wed, 24 Dec 2008 14:54:44 -0500

I don't think that this is quite what was requested (the number after the hyphen)

if the number wanted is always the last number, then you can slightly amend Bob's suggestion to get the last character (replace the first "1" in Bob's command with "-1")

If there can be more than one character after the hyphen and you only want the first, then you can do it in two steps -- use the "strpos" function to locate the hyphen and then grab the next character after than

if there can be more than one character after the hyphen and you want all of them, the first step remains to locate the hyphen -- then grab all characters after that hyphen with the substr function

Rich

Robert A Yaffee wrote:
Jason,
  gen y = substr(x,1,1)
  destring y, replace
    should do it.
         Robert

Robert A. Yaffee, Ph.D.
Research Professor
Silver School of Social Work
New York University

Biosketch: http://homepages.nyu.edu/~ray1/Biosketch2008.pdf

CV:  http://homepages.nyu.edu/~ray1/vita.pdf

----- Original Message -----
From: [email protected]
Date: Wednesday, December 24, 2008 2:27 pm
Subject: st: Extracting Numbers from Strings
To: "[email protected]" <[email protected]>

I'm trying to extract the first number after a hyphen.
For example, from x="1234-1" I want to create y=1.
Thanks.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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