Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <njcoxstata@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: Another question regarding string variables |
Date | Thu, 28 Feb 2013 09:28:15 +0000 |
Some more explanation might be wished. First off, there is a tacit assumption here that the name has been trimmed of leading and trailing spaces. Use -trim()- first, therefore. Second, the logic is that if the last word is a single character, then the previous character is a space. So we look for a space as the penultimate (one before last) character of a name. Note that replace fullname = subinstr(substr(fullname, -2, 2), "", 1) if substr(fullname, -2, 1) == " " is _not_ the way to do it as can be seen from . local foo "John Anthony A" . di subinstr("`foo'", substr("`foo'", -2, 2), "", 1) Johnnthony A Nick On Thu, Feb 28, 2013 at 8:20 AM, Nick Cox <njcoxstata@gmail.com> wrote: > Yes. For example > > replace fullname = substr(fullname, 1, length(fullname) - 2) if > substr(fullname, -2, 1) == " " > > Nick > > On Thu, Feb 28, 2013 at 6:41 AM, Dudekula, Anwar <dudekulaan@upmc.edu> wrote: > >> Is it possible to trim last word if it is one alphabet wide like removing. A from ( Graham , Tony A ) * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/