![]() ![]() | 4905 Lakeway Drive, College Station, Texas 7785 |Ģ. gen zip = regexs(0) if(regexm(address, "")) Note that the 0-9 indicates that the expression should match any character 0 This means that stringing five of theseĮxpressions together will enable us to find a string of exactly five digits. The characters contained in brackets will be matched. Unless otherwise indicated using a *, +, or ? mark, one and only one of That we want a five-digit number by specifying “”įive times. Indicates that Stata should set the value of zip to be equal to that Searches the variable address for a five digit number, and, if it canįind a five digit number in the variable address, the = regexs(0) The rest of the command is a little tricky, the "if" is evaluated first, if(regexm(address, “”)) (short for "generate") below tells Stata to generate a new variable called zip. To find the zip code we will look for a five-digit number within an address. "673 Jasmine Street, Los Angeles, CA 90024" "4905 Lakeway Drive, College Station, Texas 77845 USA" Let’s start with some fake entries of addresses. a specific word, a number followed by a wordĮtc.) and extract that set of values from the whole string for use elsewhere.Įxample 1: Extracting zip codes from addresses In these situations, regular expressions can be used to identify cases in whichĪ string contains a set of values (e.g. Here for demonstration purposes, not because regular expressions are necessarily Recognize?” for information on doing this. “ My date variable is a string, how can I turn it into a date variable Stata can This task can actually easily be handled with regular Stata commands, see our FAQ page We want to create a date variable in numeric format based on this string Value (which is what Stata generally expects to see), but in other cases it was entered as a two-digit Order of last name and then first name separated by comma.Įxample 2: Dates were entered as a string variable, in some cases the year was entered as a four-digit We want to create a new variable with full name in the Using these three functions. At the bottom of the page is an explanation ofĪll the regular expression operators as well as the functions that work withĮxample 1: A researcher has addresses as a string variable and wants to create a new variableĮxample 2: We have a variable that contains full names in the order of first Regular expression to extract and/or replace a portion of a string variable Regular expressions, regexm for matching, regexr for replacing and Among these string functions are three functions that are related to String processing is fairly easy in Stata because of the many built-in stringįunctions. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |