<?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: need to seperate full name to last and firstname using substring function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/need-to-seperate-full-name-to-last-and-firstname-using-substring/m-p/330511#M74166</link>
    <description>&lt;P&gt;In theory, this should be easy:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;last_name = scan(name, 1, ',');&lt;/P&gt;
&lt;P&gt;first_name = scan(name, 2, ',');&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In practice, a little more care needs to be taken.&amp;nbsp; There may be no commas, for example.&amp;nbsp; (That may be why you ran into trouble with the INDEX function returning a zero.)&amp;nbsp; And if there is a blank after the comma, you may need to use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;first_name = left(scan(name, 2, ','));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And what should happen if there are actually two commas, such as a last name that contains ", Jr."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 07 Feb 2017 16:33:50 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-02-07T16:33:50Z</dc:date>
    <item>
      <title>need to seperate full name to last and firstname using substring function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-to-seperate-full-name-to-last-and-firstname-using-substring/m-p/330508#M74164</link>
      <description>&lt;P&gt;Need to seperate the comma delimited &amp;nbsp;full name to last name and first name.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The word in front of the comma&amp;nbsp;as the Last Name column and the word after the comma&amp;nbsp;as First Name . I have tried with attached code and getting the errors like :-&lt;/P&gt;&lt;P&gt;NOTE: Invalid second argument to function SUBSTR at line 6059 column 12.&lt;/P&gt;&lt;P&gt;NAME=UNKNOWN LAST_NAME= FIRST_NAME=UNKNOWN _ERROR_=1 _N_=35&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data namepart; 
set TEST; 
length LAST_NAME FIRST_NAME $45; 
LAST_NAME= substr(name,index(name,',') - 1); 
FIRST_NAME = trim(left(substr(name,index(name,',') + 1))); 
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2017 16:14:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-to-seperate-full-name-to-last-and-firstname-using-substring/m-p/330508#M74164</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2017-02-07T16:14:23Z</dc:date>
    </item>
    <item>
      <title>Re: need to seperate full name to last and firstname using substring function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-to-seperate-full-name-to-last-and-firstname-using-substring/m-p/330510#M74165</link>
      <description>&lt;P&gt;Add a starting position to the line where you are trying to create lastname. i.e.,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;LAST_NAME= substr(name,1,index(name,',') - 1); 
&lt;/PRE&gt;
&lt;P&gt;However, it sounds like you might have some names that are missing commas.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HTH,&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2017 16:32:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-to-seperate-full-name-to-last-and-firstname-using-substring/m-p/330510#M74165</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-02-07T16:32:16Z</dc:date>
    </item>
    <item>
      <title>Re: need to seperate full name to last and firstname using substring function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-to-seperate-full-name-to-last-and-firstname-using-substring/m-p/330511#M74166</link>
      <description>&lt;P&gt;In theory, this should be easy:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;last_name = scan(name, 1, ',');&lt;/P&gt;
&lt;P&gt;first_name = scan(name, 2, ',');&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In practice, a little more care needs to be taken.&amp;nbsp; There may be no commas, for example.&amp;nbsp; (That may be why you ran into trouble with the INDEX function returning a zero.)&amp;nbsp; And if there is a blank after the comma, you may need to use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;first_name = left(scan(name, 2, ','));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And what should happen if there are actually two commas, such as a last name that contains ", Jr."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2017 16:33:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-to-seperate-full-name-to-last-and-firstname-using-substring/m-p/330511#M74166</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-02-07T16:33:50Z</dc:date>
    </item>
    <item>
      <title>Re: need to seperate full name to last and firstname using substring function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-to-seperate-full-name-to-last-and-firstname-using-substring/m-p/330512#M74167</link>
      <description>&lt;P&gt;Check your code with light modification:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data namepart; 
   set TEST; 
         length LAST_NAME FIRST_NAME $45; &lt;BR /&gt;         pos = index(name, ',');&lt;BR /&gt;         if pos &amp;gt; 0 then do;
            LAST_NAME= substr(name,&lt;STRONG&gt;1,&lt;/STRONG&gt; pos - 1); 
            FIRST_NAME = trim(left(substr(name,pos + 1))); &lt;BR /&gt;         end; else put '&amp;gt;&amp;gt;&amp;gt; No comma in name, line=' _N_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;By the way, you can do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;STRONG&gt;strip&lt;/STRONG&gt;(substr(name,index(name,',') + 1)))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;instead using&amp;nbsp;&lt;STRONG&gt;left(trim(&lt;/STRONG&gt;...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;last_name = scan(name,1 ',');
first_name = scan(name,2,',');&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Feb 2017 16:37:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-to-seperate-full-name-to-last-and-firstname-using-substring/m-p/330512#M74167</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-02-07T16:37:38Z</dc:date>
    </item>
  </channel>
</rss>

