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: Extract identifier in the middle of a string variable


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Extract identifier in the middle of a string variable
Date   Fri, 21 Dec 2012 18:56:11 +0000

Glad you solved your problem. I am happy to endorse -moss- as a useful
tool here.

The code I supplied is self-contained and works perfectly for me.
Without seeing the code you used I can't comment on the error you got.

Nick

On Fri, Dec 21, 2012 at 5:59 PM, Ghislaine Vantomme
<ghislaine.vantomme@gmail.com> wrote:
> Dear Robert and Nick,
>
> sorry for the delay to answer.
>
> I finally implemented your two solutions. The one by Nick yields a "r"
> error message in the loop but the moss solution by Robert work
> perfectly. I was able to adapt it to subsequent query.
>
> I also take this mail as an opportunity to wish all the Statlist users
> a merry Christmas.
>
> Thanks a lot !!
> Best
> Ghislaine
>
> 2012/12/14 Robert Picard <picard@netbox.com>:
>> 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 <njcoxstata@gmail.com> 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
>>> <ghislaine.vantomme@gmail.com> 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/
> *
> *   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