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 on April 23, 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, 14 Dec 2012 13:19:56 +0000

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/


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