Bookmark and Share

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]

st: regular expressions has too many literals


From   "Dimitriy V. Masterov" <[email protected]>
To   Statalist <[email protected]>
Subject   st: regular expressions has too many literals
Date   Mon, 25 Feb 2013 19:41:17 -0800

I would like to do something like this:

keep if regexm(string,"Buffalo Bills") | regexm(string,"Dallas
Cowboys") | regexm(string,"Miami Dolphins") | regexm(string,"New York
Giants") | regexm(string,"New England Patriots") |
regexm(string,"Philadelphia Eagles") | regexm(string,"New York Jets")
| regexm(string,"Washington Redskins") | regexm(string,"Baltimore
Ravens") | regexm(string,"Chicago Bears") | regexm
(string,"Cincinnati Bengals") | regexm(string,"Detroit Lions") |
regexm(string,"Cleveland Browns") | regexm(string,"Green Bay Packers")
| regexm(string,"Pittsburgh Steelers") | regexm(s    tring,"Minnesota
Vikings") | regexm(string,"Houston Texans") | regexm(string,"Atlanta
Falcons") | regexm(string,"Indianapolis Colts") |
regexm(string,"Carolina Panthers") | regexm(string,"Jacksonville
Jaguars") | regexm(string,"New Orleans Saints") |
regexm(string,"Tennessee Titans") | regexm(string,"Tampa Bay
Buccaneers") | regexm(string,"Denver Broncos") |
regexm(string,"Arizona Cardinals") | regexm(string,"Kansas City
Chiefs") | regexm(string,"San Francisco 49ers") |
regexm(string,"Oakland Raiders") | regexm(string,"Seattle Seahawks") |
regexm(string,"San Diego Chargers") | regexm(string,"St. Louis Rams")

Just looking at this, you know the expression is too long for Stata to
evaluate. Is the only way around this to loop over the 32 team names
like this:

gen keepers = .
foreach team in "Buffalo Bills" "Dallas Cowboys" "Miami Dolphins" "New
York Giants" "New England Patriots" "Philadelphia Eagles" "New York
Jets" "Washington Redskins" "Baltimore Ravens" "Chicago Bears"
"Cincinnati Bengals" "Detroit Lions" "Cleveland Browns" "Green Bay
Packers" "Pittsburgh Steelers" "Minnesota Vikings" "Houston Texans"
"Atlanta Falcons" "Indianapolis Colts" "Carolina Panthers"
"Jacksonville Jaguars" "New Orleans Saints" "Tennessee Titans" "Tampa
Bay Buccaneers" "Denver Broncos" "Arizona Cardinals" "Kansas City
Chiefs" "San Francisco 49ers" "Oakland Raiders" "Seattle Seahawks"
"San Diego Chargers" "St. Louis Rams" {
     replace keepers = 1 if regexm(string,"`team'")
}
keep if keepers ==1

Or is there a more clever way?
*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index