Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: AW: strpos: cleaning string variables
From
Emily Farchy <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: AW: strpos: cleaning string variables
Date
Mon, 21 Feb 2011 23:07:09 +0000
Thanks a lot Hanno,
This looks along the right lines my only worry would be that I can't say +3 or +5 because there is no fixed rule
eg,
> "Drs. Makmur Syahputra, Sh" -->"Makmur Syah Putra"
> "H Amru Helmy Daulay Sh" --> "Amru Helmy Daulay"
The first would require deleting 4 characters (5 if include the space) and the second would require trimming by only 1 charater
Have I misunderstood?
Emily
On Feb 21, 2011, at 8:35 PM, Hanno Scholtz wrote:
> Hi Emily
>
> in a similar task I first saved the blank positions and afterwards split the
> string in its components:
>
> gen p1=strpos(content,", ")-1;
> gen p2=strpos(substr(content,p1+3,.),", ")-1;
> gen p3=strpos(substr(content,p1+p2+5,.),", ")-1;
> gen p4=strpos(substr(content,p1+p2+p3+7,.),", ")-1;
> gen p5=strpos(substr(content,p1+p2+p3+p4+9,.),", ")-1;
> gen p6=strpos(substr(content,p1+p2+p3+p4+p5+11,.),", ")-1;
> replace p1=length(content) if cparts==1;
> replace p2=length(substr(content,p1+3,.)) if cparts==2;
> replace p3=length(substr(content,p1+p2+5,.)) if cparts==3;
> replace p4=length(substr(content,p1+p2+p3+7,.)) if cparts==4;
> replace p5=length(substr(content,p1+p2+p3+p4+9,.)) if cparts==5;
> replace p6=length(substr(content,p1+p2+p3+p4+p5+11,.)) if cparts==6;
> gen c1=substr(content,1,p1);
> gen c2=substr(content,p1+3,p2);
> gen c3=substr(content,p1+p2+5,p3);
> gen c4=substr(content,p1+p2+p3+7,p4);
> gen c5=substr(content,p1+p2+p3+p4+9,p5);
> gen c6=substr(content,p1+p2+p3+p4+p5+11,p6);
> drop p1-p6;
>
> Okay, it won't get a price for elegance, but it worked.
>
> Best,
> Hanno
>
>
>
>
> -----Ursprüngliche Nachricht-----
> Von: [email protected]
> [mailto:[email protected]] Im Auftrag von Emily Farchy
> Gesendet: Montag, 21. Februar 2011 21:29
> An: '[email protected]'
> Betreff: st: strpos: cleaning string variables
>
> 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/
>
>
> *
> * 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/