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]

From |
Steven Samuels <sjsamuels@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: using regex |

Date |
Fri, 3 Dec 2010 07:34:55 -0500 |

Actually, a better one-line regex solution is:

Steve On Dec 2, 2010, at 5:58 PM, Steven Samuels wrote: The regular expression solution is: gen fips = real(regexs(1)) if regexm(x,"([0-9][0-9]?)[0-9][0-9][0-9]") Steve Steven J. Samuels sjsamuels@gmail.com 18 Cantine's Island Saugerties NY 12477 USA Voice: 845-246-0774 Fax: 206-202-4783 On Dec 2, 2010, at 5:22 PM, Rijo John wrote: 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 following 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? Thanks. Rijo. * * 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/ * * 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/ * * 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/

**Follow-Ups**:**Re: st: using regex***From:*Rijo John <rmjohn@gmail.com>

**References**:**st: using regex***From:*Rijo John <rmjohn@gmail.com>

**Re: st: using regex***From:*Steven Samuels <sjsamuels@gmail.com>

- Prev by Date:
**st: RE: nested calculations** - Next by Date:
**RE: st: Referencing individual elements of (local) macros** - Previous by thread:
**Re: st: using regex** - Next by thread:
**Re: st: using regex** - Index(es):