Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: using regex


From   Phil Clayton <philclayton@internode.on.net>
To   <statalist@hsphsun2.harvard.edu>
Subject   Re: st: using regex
Date   Fri, 03 Dec 2010 09:05:38 +1030

Why not just: 
gen fips=trunc(real(code)/1000) 
 
That creates fips as a numeric variable rather than string variable. You could 
always add a string() function if you wanted it in string format for some reason. 
 
Phil 
 
On Fri 03/12/10 09:22 , Rijo John rmjohn@gmail.com sent: 
> 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 
> atotal 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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index