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: Extract identifier in the middle of a string variable
From
Robert Picard <[email protected]>
To
[email protected]
Subject
Re: st: Extract identifier in the middle of a string variable
Date
Fri, 14 Dec 2012 12:58:37 -0800
Here is how I would do it with -moss- (SSC). The pattern looks for
strings that start with "NYSE:" and matches any characters that follow
that are not a ";". Since some observations do not end with a ";", the
final "*" makes the last semi-colon optional. All NYSE identifiers are
matched. I assume that when there are multiple identifiers, they all
start the same way so I then remove the period and what follows in the
first match if there was a second match ("D.PRI" become "D" for the
last observation).
clear
input str70 s
"BMV:STP N; DB:S9H; NYSE:STP; XTRA:S9H"
"BMV:STP N; DB:S9H; XTRA:S9H"
"BASE:C; BAYB:C; BMV:C *; BST:TRVC; BVL:C; DB:TRVC; NYSE:C.PRH;"
"NYSE:C; NYSE:C.PRI; SOMA:CTGP11B; TSE:8710; XTRA:TRV"
"SOMA:CTGP11B; TSE:8710; XTRA:TRV; NYSE:D.PRI; NYSE:D; "
end
moss s, match("NYSE:([^;]+);*") regex
replace _match1 = regexr(_match1,"\..*","") if !mi(_match2)
list _match1 s
On Fri, Dec 14, 2012 at 5:19 AM, Nick Cox <[email protected]> wrote:
> I tried various solutions with -moss- (SSC) but kept messing up the
> regular expression. Robert Picard is more fluent than I with regexp.
>
> Here is a more simple-minded approach:
>
> clear
> input str80 myid
> "BASE:C; BAYB:C; BMV:C *; BST:TRVC; BVL:C; DB:TRVC; NYSE:C.PRH;"
> "NYSE:C; NYSE:C.PRI; SOMA:CTGP11B; TSE:8710; XTRA:TRV"
> end
> gen wc = wordcount(myid)
> gen nyse = ""
> su wc, meanonly
> qui forval i = 1/`r(max)' {
> replace nyse = nyse + substr(word(myid, `i'), 6, .) + " " if
> substr(word(myid, `i'), 1, 4) == "NYSE"
> }
> replace nyse = trim(subinstr(nyse, ";", "", .))
> split nyse
> local nsplit : word count `r(varlist)'
> gen shortest = nyse1
> qui forval i = 2/`nsplit' {
> replace shortest = nyse`i' if length(nyse`i') < length(shortest) &
> length(nyse`i')
> }
>
>
> On Fri, Dec 14, 2012 at 12:46 PM, Ghislaine Vantomme
> <[email protected]> wrote:
>> BASE:C; BAYB:C; BMV:C *; BST:TRVC; BVL:C; DB:TRVC; NYSE:C.PRH;
>> NYSE:C; NYSE:C.PRI; SOMA:CTGP11B; TSE:8710; XTRA:TRV
>>
>>
> *
> * 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/