<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: First &amp; Last names in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27693#M5107</link>
    <description>Hi.&lt;BR /&gt;
&lt;BR /&gt;
String parsing/substitution is easily achievable with regular expressions.&lt;BR /&gt;
&lt;BR /&gt;
The following code will do what you need, using the regular expression functions provided by SAS:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data RESULT;&lt;BR /&gt;
length FIRST $64 LAST $64;&lt;BR /&gt;
input;&lt;BR /&gt;
drop _:;&lt;BR /&gt;
&lt;BR /&gt;
* prepare reg. expression for text parsing;&lt;BR /&gt;
_EXPR='s/(Dr.)?(\s*\S*)\s*?\S*?(\s+)(\S*)\s*(MD|M.D.)\s*/$2 $4/i';&lt;BR /&gt;
_REGX=prxparse(_EXPR);&lt;BR /&gt;
_PRX=prxchange(_REGX,1,_infile_);&lt;BR /&gt;
&lt;BR /&gt;
* split parsed text into desired variables;&lt;BR /&gt;
FIRST=scan(_PRX,1);&lt;BR /&gt;
LAST=scan(_PRX,2);&lt;BR /&gt;
&lt;BR /&gt;
cards;&lt;BR /&gt;
Dr. Smith T. Bauer MD&lt;BR /&gt;
Samuel I Rodriguez M.D.&lt;BR /&gt;
Will Glader MD&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Now, the difficult part is actually understand how regular expression work, and construct a valid one that will do precisely what you need.&lt;BR /&gt;
&lt;BR /&gt;
Give a look at this pages from info about regular expressions:&lt;BR /&gt;
&lt;A href="http://www.regular-expressions.info/tutorial.html" target="_blank"&gt;http://www.regular-expressions.info/tutorial.html&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://www.troubleshooters.com/codecorn/littperl/perlreg.htm" target="_blank"&gt;http://www.troubleshooters.com/codecorn/littperl/perlreg.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
And check the online doc about the subject at SAS:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a002291852.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a002291852.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
    <pubDate>Wed, 09 Dec 2009 11:22:03 GMT</pubDate>
    <dc:creator>DanielSantos</dc:creator>
    <dc:date>2009-12-09T11:22:03Z</dc:date>
    <item>
      <title>First &amp; Last names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27691#M5105</link>
      <description>Dr. Smith T. Bauer MD&lt;BR /&gt;
Samuel I Rodriguez M.D.&lt;BR /&gt;
Will Glader MD&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
How to split the above Physicians names into first and last names:&lt;BR /&gt;
Smith    Bauer&lt;BR /&gt;
Samuel Rodriguez &lt;BR /&gt;
Will       Glader&lt;BR /&gt;
&lt;BR /&gt;
I tried to compress Dr.,MD and then tried to compress middle initial.But it is not applicable to all cases.</description>
      <pubDate>Tue, 08 Dec 2009 21:03:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27691#M5105</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2009-12-08T21:03:14Z</dc:date>
    </item>
    <item>
      <title>Re: First &amp; Last names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27692#M5106</link>
      <description>Since you are not going to have a consistent number of "tokens", suggest you parse using the SCAN function, and then based on known possible "last" values, you can decide how to identify a FIRST, LAST and maybe MIDDLE.  Again, for this work-objective, your friend will be the DATA step and the SCAN function with some number of token-substring (declared as maybe $20 each) variables.&lt;BR /&gt;
&lt;BR /&gt;
Check the DOC for using SCAN.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Tue, 08 Dec 2009 22:26:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27692#M5106</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-12-08T22:26:55Z</dc:date>
    </item>
    <item>
      <title>Re: First &amp; Last names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27693#M5107</link>
      <description>Hi.&lt;BR /&gt;
&lt;BR /&gt;
String parsing/substitution is easily achievable with regular expressions.&lt;BR /&gt;
&lt;BR /&gt;
The following code will do what you need, using the regular expression functions provided by SAS:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data RESULT;&lt;BR /&gt;
length FIRST $64 LAST $64;&lt;BR /&gt;
input;&lt;BR /&gt;
drop _:;&lt;BR /&gt;
&lt;BR /&gt;
* prepare reg. expression for text parsing;&lt;BR /&gt;
_EXPR='s/(Dr.)?(\s*\S*)\s*?\S*?(\s+)(\S*)\s*(MD|M.D.)\s*/$2 $4/i';&lt;BR /&gt;
_REGX=prxparse(_EXPR);&lt;BR /&gt;
_PRX=prxchange(_REGX,1,_infile_);&lt;BR /&gt;
&lt;BR /&gt;
* split parsed text into desired variables;&lt;BR /&gt;
FIRST=scan(_PRX,1);&lt;BR /&gt;
LAST=scan(_PRX,2);&lt;BR /&gt;
&lt;BR /&gt;
cards;&lt;BR /&gt;
Dr. Smith T. Bauer MD&lt;BR /&gt;
Samuel I Rodriguez M.D.&lt;BR /&gt;
Will Glader MD&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Now, the difficult part is actually understand how regular expression work, and construct a valid one that will do precisely what you need.&lt;BR /&gt;
&lt;BR /&gt;
Give a look at this pages from info about regular expressions:&lt;BR /&gt;
&lt;A href="http://www.regular-expressions.info/tutorial.html" target="_blank"&gt;http://www.regular-expressions.info/tutorial.html&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://www.troubleshooters.com/codecorn/littperl/perlreg.htm" target="_blank"&gt;http://www.troubleshooters.com/codecorn/littperl/perlreg.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
And check the online doc about the subject at SAS:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a002291852.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a002291852.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Wed, 09 Dec 2009 11:22:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27693#M5107</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2009-12-09T11:22:03Z</dc:date>
    </item>
    <item>
      <title>Re: First &amp; Last names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27694#M5108</link>
      <description>Thanks Santos,&lt;BR /&gt;
 I will make necessary changes on the regex based on the conditions.</description>
      <pubDate>Wed, 09 Dec 2009 18:39:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27694#M5108</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2009-12-09T18:39:10Z</dc:date>
    </item>
    <item>
      <title>Re: First &amp; Last names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27695#M5109</link>
      <description>Whatever happened to simplicity - the SCAN function works just fine so why complicate the situation, possibly considering that someone may need to support your SAS program in the future.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Wed, 09 Dec 2009 19:33:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27695#M5109</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-12-09T19:33:18Z</dc:date>
    </item>
    <item>
      <title>Re: First &amp; Last names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27696#M5110</link>
      <description>That's called "job security" Scott.&lt;BR /&gt;
&lt;BR /&gt;
;}</description>
      <pubDate>Wed, 09 Dec 2009 19:40:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27696#M5110</guid>
      <dc:creator>Flip</dc:creator>
      <dc:date>2009-12-09T19:40:17Z</dc:date>
    </item>
    <item>
      <title>Re: First &amp; Last names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27697#M5111</link>
      <description>Hi:&lt;BR /&gt;
  As elegant as the PRX-only solution is, I agree with Scott that sometimes a more verbose or step-wise solution might be easier to maintain. For example, compare the 7 statements of the PRXCHANGE/REVERSE/SCAN solution with the 5 statements in the PRX-only solution. &lt;BR /&gt;
 &lt;BR /&gt;
  If I am a PRX newbie and my ever inventive data entry folks throw a few curve balls like Dr. Casey (no first name) or Dr. First Last, Sr. MD or Dr. First Last, III MD I have a better chance of -successfully- adjusting my code using the hybrid approach. If I'm not a PRX newbie, then adjusting the regex will probably be easy.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
data drname;&lt;BR /&gt;
  length name $30;&lt;BR /&gt;
  infile datalines dsd dlm=',';&lt;BR /&gt;
  input idnum name $;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1,"Dr. Smith T. Bauer MD"&lt;BR /&gt;
2,"Samuel I Rodriguez M.D."&lt;BR /&gt;
3,"Will Glader MD"&lt;BR /&gt;
4,"Dr. Greg House"&lt;BR /&gt;
5,"Dr Drake Morgan"&lt;BR /&gt;
6,"DR Donnie Darko, Sr. MD"&lt;BR /&gt;
7,"Dr. Casey"&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
 &lt;BR /&gt;
data parsename;&lt;BR /&gt;
length first last z revname zz $30;&lt;BR /&gt;
  set drname;&lt;BR /&gt;
  ** get rid of Dr, if present;&lt;BR /&gt;
  z = left(prxchange('s/(Dr |Dr. |DR |DR. )/  /',1, name)); &lt;BR /&gt;
                             &lt;BR /&gt;
  ** get rid of periods and commas;&lt;BR /&gt;
  z = compress(z,',.');&lt;BR /&gt;
                             &lt;BR /&gt;
  ** reverse the string so that MD, Jr, Sr is first, if present;&lt;BR /&gt;
  revname = reverse(z);&lt;BR /&gt;
                     &lt;BR /&gt;
  ** change MD, Jr, Sr (in reverse) to spaces;&lt;BR /&gt;
  zz = left(prxchange('s/(DM |rJ |rS )/  /',2, revname ));&lt;BR /&gt;
                            &lt;BR /&gt;
  ** now first name is always the first chunk of the string;&lt;BR /&gt;
  first = scan(z,1,' ');&lt;BR /&gt;
                                                     &lt;BR /&gt;
  ** and last name is always the first chunk of the reversed string;&lt;BR /&gt;
  ** but the scanned string has to be reversed again to be correct;&lt;BR /&gt;
  last = reverse(scan(zz,1,' '));&lt;BR /&gt;
                                                     &lt;BR /&gt;
  ** If first name and last name are the same (Dr. Casey), then set ;&lt;BR /&gt;
  ** spaces/missing for first name;&lt;BR /&gt;
  if first = last then first = ' ';&lt;BR /&gt;
                                                       &lt;BR /&gt;
  ** entire PRX solution;&lt;BR /&gt;
  * prepare reg. expression for text parsing;&lt;BR /&gt;
  _EXPR='s/(Dr.)?(\s*\S*)\s*?\S*?(\s+)(\S*)\s*(MD|M.D.)\s*/$2 $4/i';&lt;BR /&gt;
  _REGX=prxparse(_EXPR);&lt;BR /&gt;
  _PRX=prxchange(_REGX,1,name);&lt;BR /&gt;
                              &lt;BR /&gt;
  * split parsed text into desired variables;&lt;BR /&gt;
  alt_FIRST=scan(_PRX,1);&lt;BR /&gt;
  alt_LAST=scan(_PRX,2);&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
                                               &lt;BR /&gt;
ods listing;&lt;BR /&gt;
proc print data=parsename;&lt;BR /&gt;
 var first last name  z revname zz alt_first alt_last;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[/pre]</description>
      <pubDate>Wed, 09 Dec 2009 20:14:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27697#M5111</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2009-12-09T20:14:56Z</dc:date>
    </item>
    <item>
      <title>Re: First &amp; Last names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27698#M5112</link>
      <description>Hi all.&lt;BR /&gt;
&lt;BR /&gt;
OK, regular expressions may not be something you learn on the fly, but it's actually something very logic and worth spending sometime learning (and I must say, my knowledge in the field is pretty average, and I manage myself to successfully produce one for the above question).&lt;BR /&gt;
As I see it, string manipulation functions (scan, index, reverse, substr, etc) are quite good and will do the job for most of the needs. But in some cases, where transformation gets more complicated, those will produce a very "messy", difficult to understand and maintain code. Regular expressions will give you the same number of lines, and actually the same code, either you want to do some simple or more complex transformation. You just need to code the right expression. And it's been there for years (at leas in the UNIX world) so there's a lot of documentation, just one google away!&lt;BR /&gt;
&lt;BR /&gt;
Regular expression are to my eyes one of the greatest new features of the SAS 9 platform, one that really adds value to the SAS language.&lt;BR /&gt;
&lt;BR /&gt;
So, do not be afraid of the "infamous" PRX functions, they won't bite you.&lt;BR /&gt;
&lt;BR /&gt;
But, then again, I believe any solution is a valid, if the result is the one expected.&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Thu, 10 Dec 2009 09:01:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27698#M5112</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2009-12-10T09:01:12Z</dc:date>
    </item>
    <item>
      <title>Re: First &amp; Last names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27699#M5113</link>
      <description>Thank you Cynthia.</description>
      <pubDate>Thu, 10 Dec 2009 18:27:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27699#M5113</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2009-12-10T18:27:47Z</dc:date>
    </item>
    <item>
      <title>Re: First &amp; Last names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27700#M5114</link>
      <description>Thats true.Regular expressions are not as easy to understand as the other SAS functions.They are too confusing.</description>
      <pubDate>Thu, 10 Dec 2009 18:29:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27700#M5114</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2009-12-10T18:29:00Z</dc:date>
    </item>
    <item>
      <title>Re: First &amp; Last names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27701#M5115</link>
      <description>Daniel...&lt;BR /&gt;
  In principle, I agree with you. However, figuring out PRX expressions gives me a headache .. even with Google ... and reminds me of my old symbolic logic class, which also gave me headaches. I passed the course, but it was the hardest class I ever took. &lt;BR /&gt;
&lt;BR /&gt;
  To give Perl regular expressions their due: I -love- PRXCHANGE and if anything is going to lead me to understand the rest of Perl Regular Expressions, PRXCHANGE will get me there....but not today!&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Thu, 10 Dec 2009 18:42:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27701#M5115</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2009-12-10T18:42:16Z</dc:date>
    </item>
    <item>
      <title>Re: First &amp; Last names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27702#M5116</link>
      <description>OK folks, just wanna help here.&lt;BR /&gt;
&lt;BR /&gt;
But bear in mind, that although the easiest path may be the safest, it's probably not the one that will take you furthermore ahead (as a teacher of mine use to say).&lt;BR /&gt;
&lt;BR /&gt;
And let me say, that you're missing quite an important and powerful feature of the SAS language.&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Fri, 11 Dec 2009 08:38:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27702#M5116</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2009-12-11T08:38:37Z</dc:date>
    </item>
    <item>
      <title>Re: First &amp; Last names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27703#M5117</link>
      <description>I still take the view that a public "text definition standard" that I presume Perl regular expressions provide, gives our SAS languages something on which to build the "inPicture" equivalent of "inValue" (at &lt;A href="http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/a002473466.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/a002473466.htm&lt;/A&gt; see proc format documantation for inValue).&lt;BR /&gt;
Although I know a clever little birdie, in his "developers sandpit", has toyed with, and demonstrated techniques using perl regular expressions in the "start" string for an enhanced inValue statement, there does not seem to be enough "customer demand" behind this concept of better integration of regular expression within the SAS languages to create the prioity or budget for development and implementation of this logical extension of SAS language inFormats.&lt;BR /&gt;
&lt;BR /&gt;
 &lt;BR /&gt;
 PeterC</description>
      <pubDate>Fri, 11 Dec 2009 09:21:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-Last-names/m-p/27703#M5117</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2009-12-11T09:21:38Z</dc:date>
    </item>
  </channel>
</rss>

