<?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: Extract Name in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extract-Name/m-p/445734#M283140</link>
    <description>&lt;P&gt;PRX to the rescue...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data test;
input id fullname $64.;
datalines;
1 Michael R. Boyce
2 James G. Brocksmith, Jr.
3 Gerald F. Fitzgerald, Jr.
4 Norman R. Bobins, BS, MBA
5 Peter Pace, USMC, Ret.
6 Ryan, Norman A.
7 Anne Newman Foreman
8 Michael R.S. Boyce
9 Ryan, Norman A., BS
;

data want;
length firstName lastName $20 initials $6;
if not prxId1 then prxId1 + prxParse("/^(\w+)\s+(([A-Z]\.)*\s*)([^,]+)/");
if not prxId2 then prxId2 + prxParse("/^(\w+),\s*(\w+)\s+(([A-Z]\.)*)/");
set test;
if prxMatch(prxId1, fullname) then do;
    firstName = prxPosn(prxId1, 1, fullname);
    initials = prxPosn(prxId1, 2, fullname);
    lastName = prxPosn(prxId1, 4, fullname);
    end;
else if prxMatch(prxId2, fullname) then do;
    firstName = prxPosn(prxId2, 2, fullname);
    initials = prxPosn(prxId2, 3, fullname);
    lastName = prxPosn(prxId2, 1, fullname);
    end;
drop prxId: ;
run;

proc print data=want noobs; run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;      first
      Name       lastName          initials    id    fullname

      Michael    Boyce               R.         1    Michael R. Boyce
      James      Brocksmith          G.         2    James G. Brocksmith, Jr.
      Gerald     Fitzgerald          F.         3    Gerald F. Fitzgerald, Jr.
      Norman     Bobins              R.         4    Norman R. Bobins, BS, MBA
      Peter      Pace                           5    Peter Pace, USMC, Ret.
      Norman     Ryan                A.         6    Ryan, Norman A.
      Anne       Newman Foreman                 7    Anne Newman Foreman
      Michael    Boyce               R.S.       8    Michael R.S. Boyce
      Norman     Ryan                A.         9    Ryan, Norman A., BS
&lt;/PRE&gt;</description>
    <pubDate>Thu, 15 Mar 2018 05:47:37 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2018-03-15T05:47:37Z</dc:date>
    <item>
      <title>Extract Name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Name/m-p/445716#M283137</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please help me extract first name of full name?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data test;&lt;/P&gt;&lt;P&gt;input id fullname $;&lt;/P&gt;&lt;P&gt;1 Michael R. Boyce&lt;/P&gt;&lt;P&gt;2 James G. Brocksmith, Jr.&lt;/P&gt;&lt;P&gt;3 Gerald F. Fitzgerald, Jr.&lt;/P&gt;&lt;P&gt;4&amp;nbsp;Norman R. Bobins, BS, MBA&lt;/P&gt;&lt;P&gt;5&amp;nbsp;Peter Pace, USMC, Ret.&lt;/P&gt;&lt;P&gt;6 Ryan, Norman A.&lt;/P&gt;&lt;P&gt;7 Anne Newman Foreman&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to create "first_name" column with middle name and without middle name and "last_name" using the fullname information.&lt;/P&gt;&lt;P&gt;As you can see above, In ID=6, the full name start with the last name unlike other observations. If it is the case, I need to re-order it to be consistent with others.&lt;/P&gt;&lt;P&gt;Like...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data test; set test;&lt;/P&gt;&lt;P&gt;input ID First_name_with_middle $ Last_name $ First_name_without_middle $&amp;nbsp;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 Michael R. Boyce Michael&lt;/P&gt;&lt;P&gt;2 James G. Brocksmith James&lt;/P&gt;&lt;P&gt;3 Gerald F. Fitzgerald Gerald&lt;/P&gt;&lt;P&gt;4 Norman R. Bobins Norman&lt;/P&gt;&lt;P&gt;5 Peter Pace Peter&amp;nbsp;&lt;/P&gt;&lt;P&gt;6 Norman A. Ryan Norman&lt;/P&gt;&lt;P&gt;7 Anne Newman Foreman Anne&amp;nbsp;&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That is, the first name with middle name column should contain the middle name abbreviation as well.&lt;/P&gt;&lt;P&gt;And, the last name column only contains last name of the full name.&lt;/P&gt;&lt;P&gt;Please help me!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Mar 2018 04:28:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Name/m-p/445716#M283137</guid>
      <dc:creator>hkim3677</dc:creator>
      <dc:date>2018-03-15T04:28:08Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Name/m-p/445730#M283138</link>
      <description>&lt;P&gt;I just got confused with variables you want in your output dataset&lt;/P&gt;</description>
      <pubDate>Thu, 15 Mar 2018 05:23:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Name/m-p/445730#M283138</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2018-03-15T05:23:08Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Name/m-p/445733#M283139</link>
      <description>&lt;P&gt;Sorry. The wanted dataset should look like..&lt;/P&gt;&lt;P&gt;Data test;&lt;BR /&gt;infile datalines DSD;&lt;BR /&gt;input ID First_name_with_middle $ Last_name $ First_name_without_middle $ ;&lt;BR /&gt;datalines;&lt;BR /&gt;1, Michael R., Boyce, Michael&lt;BR /&gt;2, James G., Brocksmith, James&lt;BR /&gt;3, Gerald F., Fitzgerald, Gerald&lt;BR /&gt;4, Norman R., Bobins, Norman&lt;BR /&gt;5, Peter, Pace, Peter&lt;BR /&gt;6, Norman A., Ryan, Norman&lt;BR /&gt;7, Anne Newman, Foreman, Anne&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But, when I ran the code above,&amp;nbsp;&lt;SPAN&gt;First_name_with_middle column shows that some observations do not have a dot after middle name abbreviation. I do not know why... but I need the dot after the middle name abbreviation if there is a middle name abbreviation.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Mar 2018 05:47:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Name/m-p/445733#M283139</guid>
      <dc:creator>hkim3677</dc:creator>
      <dc:date>2018-03-15T05:47:34Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Name/m-p/445734#M283140</link>
      <description>&lt;P&gt;PRX to the rescue...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data test;
input id fullname $64.;
datalines;
1 Michael R. Boyce
2 James G. Brocksmith, Jr.
3 Gerald F. Fitzgerald, Jr.
4 Norman R. Bobins, BS, MBA
5 Peter Pace, USMC, Ret.
6 Ryan, Norman A.
7 Anne Newman Foreman
8 Michael R.S. Boyce
9 Ryan, Norman A., BS
;

data want;
length firstName lastName $20 initials $6;
if not prxId1 then prxId1 + prxParse("/^(\w+)\s+(([A-Z]\.)*\s*)([^,]+)/");
if not prxId2 then prxId2 + prxParse("/^(\w+),\s*(\w+)\s+(([A-Z]\.)*)/");
set test;
if prxMatch(prxId1, fullname) then do;
    firstName = prxPosn(prxId1, 1, fullname);
    initials = prxPosn(prxId1, 2, fullname);
    lastName = prxPosn(prxId1, 4, fullname);
    end;
else if prxMatch(prxId2, fullname) then do;
    firstName = prxPosn(prxId2, 2, fullname);
    initials = prxPosn(prxId2, 3, fullname);
    lastName = prxPosn(prxId2, 1, fullname);
    end;
drop prxId: ;
run;

proc print data=want noobs; run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;      first
      Name       lastName          initials    id    fullname

      Michael    Boyce               R.         1    Michael R. Boyce
      James      Brocksmith          G.         2    James G. Brocksmith, Jr.
      Gerald     Fitzgerald          F.         3    Gerald F. Fitzgerald, Jr.
      Norman     Bobins              R.         4    Norman R. Bobins, BS, MBA
      Peter      Pace                           5    Peter Pace, USMC, Ret.
      Norman     Ryan                A.         6    Ryan, Norman A.
      Anne       Newman Foreman                 7    Anne Newman Foreman
      Michael    Boyce               R.S.       8    Michael R.S. Boyce
      Norman     Ryan                A.         9    Ryan, Norman A., BS
&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Mar 2018 05:47:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Name/m-p/445734#M283140</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-03-15T05:47:37Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Name/m-p/445735#M283141</link>
      <description>Thank you very much!</description>
      <pubDate>Thu, 15 Mar 2018 05:49:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Name/m-p/445735#M283141</guid>
      <dc:creator>hkim3677</dc:creator>
      <dc:date>2018-03-15T05:49:11Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Name/m-p/445740#M283142</link>
      <description>&lt;P&gt;Just one more quick question.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I have the fullname.. like...&lt;/P&gt;&lt;P&gt;Lavizzo-Mourey, Evans&lt;/P&gt;&lt;P&gt;B. Evan Bayh, III&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Those names are not correctly divided into firstname and lastname using the above code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Mar 2018 06:20:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Name/m-p/445740#M283142</guid>
      <dc:creator>hkim3677</dc:creator>
      <dc:date>2018-03-15T06:20:43Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Name/m-p/445957#M283143</link>
      <description>&lt;P&gt;The possibilities are endless... You can account for hyphenated names and firstnames easily, but frankly I don't even know what's what in "B. Evan Bayh, III". If such "names" are rare, you might be better sending them to a separate table and treating them by hand...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data test;
input id fullname $64.;
datalines;
1 Michael R. Boyce
2 James G. Brocksmith, Jr.
3 Gerald F. Fitzgerald, Jr.
4 Norman R. Bobins, BS, MBA
5 Peter Pace, USMC, Ret.
6 Ryan, Norman A.
7 Anne Newman Foreman
8 Michael R.S. Boyce
9 Ryan, Norman A., BS
10 Lavizzo-Mourey, Evans
11 B. Evan Bayh, III
;

data want except(keep=id fullname);
length firstName lastName $20 initials $6;
if not prxId1 then prxId1 + prxParse("/^([\w-]+)\s+(([A-Z]\.)*\s*)([^,]+)/");
if not prxId2 then prxId2 + prxParse("/^([\w-]+),\s*([\w-]+)\s+(([A-Z]\.)*)/");
set test;
if prxMatch(prxId1, fullname) then do;
    firstName = prxPosn(prxId1, 1, fullname);
    initials = prxPosn(prxId1, 2, fullname);
    lastName = prxPosn(prxId1, 4, fullname);
    output want;
    end;
else if prxMatch(prxId2, fullname) then do;
    firstName = prxPosn(prxId2, 2, fullname);
    initials = prxPosn(prxId2, 3, fullname);
    lastName = prxPosn(prxId2, 1, fullname);
    output want;
    end;
else output except;
drop prxId: ;
run;

title "Matched names";
proc print data=want noobs; run;

title "Unmatched names";
proc print data=except noobs; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Mar 2018 18:59:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Name/m-p/445957#M283143</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-03-15T18:59:16Z</dc:date>
    </item>
  </channel>
</rss>

