STATA: Regular expressions

January 23, 2011

(This article was first published on The Data Monkey, and kindly contributed to R-bloggers)

A regular expression allows you to do a moderately fancy search (and replace if you want). So say you wanted to replace all the “Dennis”s in a variable with “Awesome”s, but only if they’re at the end of the line. You could try:
-replace PBFnamevar = regexr(PBFnamevar,”Dennis$”,”Awesome”)-
You could also replace any character, or just capitals, or just digits…there are lots of possibilities:

You can also use it for locals:
-local strata = regexr(“agecat”,”age”)-

Or -if- commands:
if regexm(“`strata'”,”age”) {

On a related note (although not actually regular expressions), say that you’ve got a string variable that consists of a bunch of what should be separate variables, only lumped all into one, separated by a semicolon (e.g. a row might look like “1;15.2;89;hi;21″). Try -split-:
-split textvar, gen(newtextvars) parse(“;”)-

I should note that Stata’s regular expressions are wimpy compared to what other languages support. R supports PERL regular expressions, which can do so many things it’s scary.

To leave a comment for the author, please follow the link and comment on their blog: The Data Monkey. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)