Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | "Ben Hoen" <bhoen@lbl.gov> |
To | <statalist@hsphsun2.harvard.edu> |
Subject | st: RE: RE: parsing degree minute second |
Date | Tue, 31 Jan 2012 14:28:09 -0500 |
Thanks Nick! Using word(..) makes sense and is considerably more efficient! Ben Ben Hoen LBNL Office: 845-758-1896 Cell: 718-812-7589 -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox Sent: Tuesday, January 31, 2012 1:32 PM To: 'statalist@hsphsun2.harvard.edu' Subject: st: RE: parsing degree minute second My principle and practice with string problems is to reach for regex machinery if and only if simpler tools won't suffice. (When that's not true, they can be very helpful.) Thus here the elements you want are the first, third, fifth words of -latitude- to be extracted with -word(latitude, 1)- ... I infer that all your data are in the Northern Hemisphere but if not then -word(latitude, -1)- extracts "N" or "S". Alternatively, for a regex convenience tool, see -moss- (SSC) and . moss lat, regex match(([0-9.]+)) (A rolling -moss- gathers all strings....) Nick n.j.cox@durham.ac.uk -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Ben Hoen Sent: 31 January 2012 18:05 To: statalist@hsphsun2.harvard.edu Subject: st: parsing degree minute second Hello statalisters, I am trying to extract portions of a string "latitude" (in degree minute seconds) so that I can convert it to decimal degrees. I have not been able to get the extraction to work; the ".list" returns null sets for all the latdd* variables. Can anyone see my error? clear all input str20 latitude "39 - 46 - 21.02 N" "39 - 44 - 58.66 N" "118 - 06 - 27.95 N" end gen latdd1=regexs(1) if regexm(latitude, "(([0-9]+)[\ - ]([0-9]+)[\ - ]([0-9]+[\.][0-9]+))") gen latdd2=regexs(2) if regexm(latitude, "(([0-9]+)[\ - ]([0-9]+)[\ - ]([0-9]+[\.][0-9]+))") gen latdd3=regexs(3) if regexm(latitude, "(([0-9]+)[\ - ]([0-9]+)[\ - ]([0-9]+[\.][0-9]+))") list Thank you in advance for any help you can offer. Ben Ben Hoen Principal Research Associate Lawrence Berkeley National Laboratory Office: 845-758-1896 Cell: 718-812-7589 bhoen@lbl.gov http://eetd.lbl.gov/ea/emp/staff/hoen.html * * 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/