Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: using regex

From   Rijo John <>
To   stata <>
Subject   st: using regex
Date   Thu, 2 Dec 2010 17:22:26 -0500

Hi statlist,

I have a string variable (code) with state and county codes merged.
For example, the observation 12001 indicate that the first 2 digits
are state codes and the remaining 3 digits are county codes. I want to
create a new variable (fips) only with the state code.  I use the

gen fips = regexs(0) if(regexm(code, "[0-9][0-9]"))

and it works fine when the code contains 5 digits. But if a particular
state code is only 1 digit and thereby the string "code" only has a
total of 4 digits the trick above does not work. In such cases I want
to only extract the first digit.
Can someone help?

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index