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: Looping over a levelsof macro to match/replace


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Looping over a levelsof macro to match/replace
Date   Mon, 16 Apr 2012 20:38:06 +0100

You say this doesn't work but don't explain what that means in your
case, i.e. what doesn't happen that should, or does happen that
shouldn't. But it doesn't seem that you need any kind of matching here
other than strict equality.

levelsof tickerlist, c local(tickerlist)
foreach 1 of local tickerlist {
     replace issuer_code=code if ticker == "`1'"
}

Nick

On Mon, Apr 16, 2012 at 8:16 PM, Andrew Back <andrewback@gmail.com> wrote:
> I have two string variables: ticker and tickerlist.
>
> "tickerlist" is a master list of all symbols; "code" is the list of
> corresponding issuer codes. These pairs of ticker symbols and codes do
> not currently parse with the rest of the dataset:
>
> ticker      issuer_code     tickerlist       code
> AAII        .                       A               4215
> AAII        .                       AA             42
> AAII        .                       AACC        4855
> AAII        .                       AACE        2344
> AAII        .                       AAI            2755
> AAII        .                       AAII           1605
>
> and so on. Ticker is a subset of tickerlist, so what I'm trying to do
> is tell Stata to populate the issuer code for each ticker in the rest
> of the dataset based on these "tickerlist-code" pairs. In otherwords,
> every time Stata sees a ticker symbol I want it to find the
> appropriate match in tickerlist and then supply the corresponding code
> to fill in the missing data:
>
> levelsof tickerlist, c local(tickerlist)
> foreach 1 of local tickerlist {
>     replace issuer_code=code if (strmatch(ticker,"`1'")==1)
> }
>
> Any thoughts on why this doesn't work would be greatly appreciated. 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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index