Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Emily Farchy <emily.farchy@sant.ox.ac.uk> |
To | "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu> |
Subject | st: strpos: cleaning string variables |
Date | Mon, 21 Feb 2011 20:29:17 +0000 |
I would like to run a loop to transform some messy names into cleaner versions eg "Drs. H. Muslim Kasim, Ak"--> "Muslim Kasim" "Drs. Makmur Syahputra, Sh" -->"Makmur Syah Putra" "H Amru Helmy Daulay Sh" --> "Amru Helmy Daulay" I was thinking of using the strpos command which seems like it might be useful but is there a way to get it to find the nth occurrence (eg of a space) and a way to eliminate substrings of for example 3 letters or less? I was working on the following but get stuck when I pass the first occurence foreach x of local xlist { **** get rid of . , or & in name (A, B --> A B; st. A --> st A, A & B --> A and B) foreach z in , . & { qui gen `x'_bh= trim(substr(`x', 1, strpos(`x', "`z'")-1)) qui gen `x'_ah= trim(substr(`x', strpos (`x', "`z'")+1, 50)) qui gen `x'_mid= substr(`x', strpos (`x', "`z'"), 1) replace `x'=trim(`x'_bh+" "+`x'_ah) replace `x'=trim(`x'_bh+" and "+`x'_ah) if `x'_mid=="&" drop `x'_bh `x'_ah `x'_mid } } Thanks a lot * * 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/