<?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 Scan function for separating out name parts in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Scan-function-for-separating-out-name-parts/m-p/759796#M80882</link>
    <description>&lt;P&gt;I have a variable that lists a person's entire name. I would like to extract the first, middle, and last names as well names suffixes (e.g., Jr.) and save them into separate variables. I have figured out how to do everything except pull the full middle name if the middle name is more than one word. An example that is giving me trouble:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Johnson, Ethan Marc Anthony, Jr.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Where Ethan is the first name, Marc Anthony is the middle name, Johnson is the last name, and Jr. is the name suffix.&lt;/P&gt;
&lt;P&gt;When I use the following, everything is pulled correctly&amp;nbsp;&lt;EM&gt;except&amp;nbsp;&lt;/EM&gt;middle name:&lt;/P&gt;
&lt;PRE&gt;lastnm = scan(Name,1,",");
firstmid = scan(Name,2,",");
firstnm = scan(firstmid ,1," ");
midnm= scan(firstmid ,2," ");
nmsuffix = scan(Name,3,",");&lt;/PRE&gt;
&lt;P&gt;When I run this, the result is:&lt;/P&gt;
&lt;P&gt;firstnm = Ethan&lt;/P&gt;
&lt;P&gt;midnm = Marc&amp;nbsp;&lt;STRONG&gt;(should be "Marc Anthony")&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;lastnm = Johnson&lt;/P&gt;
&lt;P&gt;nmsuffix = Jr.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thoughts?&lt;/P&gt;</description>
    <pubDate>Thu, 05 Aug 2021 20:23:04 GMT</pubDate>
    <dc:creator>raivester</dc:creator>
    <dc:date>2021-08-05T20:23:04Z</dc:date>
    <item>
      <title>Scan function for separating out name parts</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Scan-function-for-separating-out-name-parts/m-p/759796#M80882</link>
      <description>&lt;P&gt;I have a variable that lists a person's entire name. I would like to extract the first, middle, and last names as well names suffixes (e.g., Jr.) and save them into separate variables. I have figured out how to do everything except pull the full middle name if the middle name is more than one word. An example that is giving me trouble:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Johnson, Ethan Marc Anthony, Jr.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Where Ethan is the first name, Marc Anthony is the middle name, Johnson is the last name, and Jr. is the name suffix.&lt;/P&gt;
&lt;P&gt;When I use the following, everything is pulled correctly&amp;nbsp;&lt;EM&gt;except&amp;nbsp;&lt;/EM&gt;middle name:&lt;/P&gt;
&lt;PRE&gt;lastnm = scan(Name,1,",");
firstmid = scan(Name,2,",");
firstnm = scan(firstmid ,1," ");
midnm= scan(firstmid ,2," ");
nmsuffix = scan(Name,3,",");&lt;/PRE&gt;
&lt;P&gt;When I run this, the result is:&lt;/P&gt;
&lt;P&gt;firstnm = Ethan&lt;/P&gt;
&lt;P&gt;midnm = Marc&amp;nbsp;&lt;STRONG&gt;(should be "Marc Anthony")&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;lastnm = Johnson&lt;/P&gt;
&lt;P&gt;nmsuffix = Jr.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thoughts?&lt;/P&gt;</description>
      <pubDate>Thu, 05 Aug 2021 20:23:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Scan-function-for-separating-out-name-parts/m-p/759796#M80882</guid>
      <dc:creator>raivester</dc:creator>
      <dc:date>2021-08-05T20:23:04Z</dc:date>
    </item>
    <item>
      <title>Re: Scan function for separating out name parts</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Scan-function-for-separating-out-name-parts/m-p/759811#M80883</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/326526"&gt;@raivester&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a variable that lists a person's entire name. I would like to extract the first, middle, and last names as well names suffixes (e.g., Jr.) and save them into separate variables. I have figured out how to do everything except pull the full middle name if the middle name is more than one word. An example that is giving me trouble:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Johnson, Ethan Marc Anthony, Jr.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Where Ethan is the first name, Marc Anthony is the middle name, Johnson is the last name, and Jr. is the name suffix.&lt;/P&gt;
&lt;P&gt;When I use the following, everything is pulled correctly&amp;nbsp;&lt;EM&gt;except&amp;nbsp;&lt;/EM&gt;middle name:&lt;/P&gt;
&lt;PRE&gt;lastnm = scan(Name,1,",");
firstmid = scan(Name,2,",");
firstnm = scan(firstmid ,1," ");
midnm= scan(firstmid ,2," ");
nmsuffix = scan(Name,3,",");&lt;/PRE&gt;
&lt;P&gt;When I run this, the result is:&lt;/P&gt;
&lt;P&gt;firstnm = Ethan&lt;/P&gt;
&lt;P&gt;midnm = Marc&amp;nbsp;&lt;STRONG&gt;(should be "Marc Anthony")&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;lastnm = Johnson&lt;/P&gt;
&lt;P&gt;nmsuffix = Jr.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thoughts?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Try something like this:&lt;/P&gt;
&lt;PRE&gt;data example;
   name="Johnson, Ethan Marc Anthony, Jr.";
   length lastnm $ 10 firstmid $ 30 firstnm midnm $15 ;
   lastnm = scan(Name,1,",");
   firstmid = strip(scan(Name,2,","));
   firstnm = scan(firstmid ,1," ");
   midnm= strip(substr(firstmid ,length(firstnm)+2));
   nmsuffix = strip(scan(Name,3,","));
run;&lt;/PRE&gt;
&lt;P&gt;You didn't show an entire data step so I don't know if you set lengths for your variables. It is a good idea to do so.&lt;/P&gt;
&lt;P&gt;The Strip is because the way you build Firstmid it will often have a leading blank, as in your example.&lt;/P&gt;
&lt;P&gt;The midnm uses the length function to find the length of the first name then start pulling characters at two after that length to start on the remainder. I include another strip there because, having dealt some name fields like this, there will be garbage characters in there.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Aug 2021 21:14:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Scan-function-for-separating-out-name-parts/m-p/759811#M80883</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-08-05T21:14:22Z</dc:date>
    </item>
    <item>
      <title>Re: Scan function for separating out name parts</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Scan-function-for-separating-out-name-parts/m-p/759834#M80885</link>
      <description>&lt;P&gt;You can use substr function. Just replace midnm with following statement:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;midnm= substr(firstmid,length(firstnm)+2,length(firstmid)-length(firstnm));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Aug 2021 22:53:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Scan-function-for-separating-out-name-parts/m-p/759834#M80885</guid>
      <dc:creator>Rydhm</dc:creator>
      <dc:date>2021-08-05T22:53:40Z</dc:date>
    </item>
    <item>
      <title>Re: Scan function for separating out name parts</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Scan-function-for-separating-out-name-parts/m-p/759924#M80886</link>
      <description>&lt;P&gt;Using SUBSTR() instead of SCAN().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data example;
   name="Johnson, Ethan Marc Anthony, Jr.";

lastnm = scan(Name,1,",");
firstmid = scan(Name,2,",");
firstnm = scan(firstmid ,1," ");
midnm= substr(firstmid ,findc(left(firstmid),' ')+1);
nmsuffix = scan(Name,3,",");
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Aug 2021 12:46:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Scan-function-for-separating-out-name-parts/m-p/759924#M80886</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-08-06T12:46:48Z</dc:date>
    </item>
    <item>
      <title>Re: Scan function for separating out name parts</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Scan-function-for-separating-out-name-parts/m-p/759935#M80887</link>
      <description>Ooh! Good thinkin'!</description>
      <pubDate>Fri, 06 Aug 2021 13:15:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Scan-function-for-separating-out-name-parts/m-p/759935#M80887</guid>
      <dc:creator>raivester</dc:creator>
      <dc:date>2021-08-06T13:15:30Z</dc:date>
    </item>
  </channel>
</rss>

