<?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: how to extract first and last names if last word is space in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-first-and-last-names-if-last-word-is-space/m-p/901184#M356154</link>
    <description>Thank you &lt;BR /&gt;s_Lassen</description>
    <pubDate>Thu, 02 Nov 2023 05:53:31 GMT</pubDate>
    <dc:creator>BrahmanandaRao</dc:creator>
    <dc:date>2023-11-02T05:53:31Z</dc:date>
    <item>
      <title>how to extract first and last names if last word is space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-first-and-last-names-if-last-word-is-space/m-p/901010#M356090</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data firstlast;
   input string $60.;
   First_Word=scan(string,1, 1,);
   Last_Word=scan(string, -1,1);
   if _n_ = ' ' then last_word =' ' ;
datalines;
Jack and Jill
&amp;amp; Bob &amp;amp; Carol &amp;amp; Ted &amp;amp; Alice &amp;amp;
Leonardo
Gates
! $ % &amp;amp; ( ) * + , - . / 
;;;;
proc print data=firstlast;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;i want output below like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="dgrid-header dgrid-header-row ui-widget-header" role="row"&gt;
&lt;TABLE id="dgrid_68-header" class="dgrid-row-table" role="presentation"&gt;
&lt;TBODY&gt;
&lt;TR class="dojoDndSource dojoDndTarget dojoDndHorizontal dojoDndContainer"&gt;
&lt;TH class="dgrid-cell dgrid-cell-padding dgrid-column-gridFiller field-gridFiller" role="columnheader"&gt;
&lt;DIV class="dgrid-resize-header-container"&gt;
&lt;DIV class="dgrid-resize-handle resizeNode-gridFiller"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/TH&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;DIV class="dgrid-scroller"&gt;
&lt;DIV class="dgrid-content ui-widget-content"&gt;
&lt;DIV id="dgrid_68-row-0" class=" dgrid-row dgrid-row-even ui-state-default" role="row"&gt;
&lt;TABLE class="dgrid-row-table" role="presentation"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-0 field-col0 obsNumCell dgrid-focus"&gt;1&lt;/TD&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-1 field-col1"&gt;Jack&lt;/TD&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-2 field-col2"&gt;Jill&lt;/TD&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-gridFiller field-gridFiller"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;DIV id="dgrid_68-row-1" class=" dgrid-row dgrid-row-odd ui-state-default" role="row"&gt;
&lt;TABLE class="dgrid-row-table" role="presentation"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-0 field-col0 obsNumCell"&gt;2&lt;/TD&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-1 field-col1"&gt;&amp;amp;&lt;/TD&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-2 field-col2"&gt;&amp;amp;&lt;/TD&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-gridFiller field-gridFiller"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;DIV id="dgrid_68-row-2" class=" dgrid-row dgrid-row-even ui-state-default" role="row"&gt;
&lt;TABLE class="dgrid-row-table" role="presentation"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-0 field-col0 obsNumCell"&gt;3&lt;/TD&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-1 field-col1"&gt;Leonardo&lt;/TD&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-2 field-col2"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-gridFiller field-gridFiller"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;DIV id="dgrid_68-row-3" class=" dgrid-row dgrid-row-odd ui-state-default" role="row"&gt;
&lt;TABLE class="dgrid-row-table" role="presentation"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="40px" class="dgrid-cell dgrid-cell-padding dgrid-column-0 field-col0 obsNumCell"&gt;4&lt;/TD&gt;
&lt;TD width="53.1875px" class="dgrid-cell dgrid-cell-padding dgrid-column-1 field-col1"&gt;Gates&lt;/TD&gt;
&lt;TD width="53.1875px" class="dgrid-cell dgrid-cell-padding dgrid-column-2 field-col2"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="40px" class="dgrid-cell dgrid-cell-padding dgrid-column-gridFiller field-gridFiller"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;DIV id="dgrid_68-row-4" class=" dgrid-row ui-state-default dgrid-row-even" role="row"&gt;
&lt;TABLE class="dgrid-row-table" role="presentation"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-0 field-col0 obsNumCell"&gt;5&lt;/TD&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-1 field-col1"&gt;!&lt;/TD&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-2 field-col2"&gt;/&lt;/TD&gt;
&lt;TD class="dgrid-cell dgrid-cell-padding dgrid-column-gridFiller field-gridFiller"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 06:26:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-first-and-last-names-if-last-word-is-space/m-p/901010#M356090</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2023-11-01T06:26:57Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract first and last names if last word is space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-first-and-last-names-if-last-word-is-space/m-p/901014#M356091</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for supplying the data in a data step; you're nearly there with your solution. I would advise checking your log for issues in the form of "NOTE:" messages. Also examine the documentation for the scan() function, as some of the arguments you're supplying don't match the scan() function's specification. Lastly _n_ is a numeric type.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input string $60.;
  datalines4;
Jack and Jill
&amp;amp; Bob &amp;amp; Carol &amp;amp; Ted &amp;amp; Alice &amp;amp;
Leonardo
Gates
! $ % &amp;amp; ( ) * + , - . / 
;;;;


data want;
  set have;
  
  First_Word = scan(string,1,' ');
  Last_Word  = scan(substr(string,length(first_word)+1),-1,' ');
run;

proc print data=want;
run;
&lt;/CODE&gt;&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;
&lt;P&gt;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 07:27:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-first-and-last-names-if-last-word-is-space/m-p/901014#M356091</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2023-11-01T07:27:45Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract first and last names if last word is space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-first-and-last-names-if-last-word-is-space/m-p/901015#M356092</link>
      <description>&lt;P&gt;I think you can get what you want by counting the number of words in the string, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data firstlast;
   input string $60.;
   First_Word=scan(string,1, ' ');
   if countw(string,' ')&amp;gt;1 then 
     Last_Word=scan(string, -1,' ');
datalines;
Jack and Jill
&amp;amp; Bob &amp;amp; Carol &amp;amp; Ted &amp;amp; Alice &amp;amp;
Leonardo
Gates
! $ % &amp;amp; ( ) * + , - . / 
;;;;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Nov 2023 07:33:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-first-and-last-names-if-last-word-is-space/m-p/901015#M356092</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-11-01T07:33:03Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract first and last names if last word is space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-first-and-last-names-if-last-word-is-space/m-p/901027#M356098</link>
      <description>&lt;P&gt;_N_ is an automatic variable which is set to the current data step iteration number at the beginning of each iteration. It is &lt;U&gt;numeric&lt;/U&gt; and can never be missing (unless it is explicitly set to missing in code). Your use of it makes no sense at all.&lt;/P&gt;
&lt;P&gt;What did you&amp;nbsp;&lt;EM&gt;think&lt;/EM&gt; this statement would do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if _n_ = ' ' then last_word =' ' ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 08:57:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-first-and-last-names-if-last-word-is-space/m-p/901027#M356098</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-11-01T08:57:51Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract first and last names if last word is space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-first-and-last-names-if-last-word-is-space/m-p/901070#M356106</link>
      <description>&lt;P&gt;Hello &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265860"&gt;@BrahmanandaRao&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;I see the issue is with your data. Particularly the second and last lines.&lt;BR /&gt;In the second line the ampersands perhaps represent delimiters.&lt;BR /&gt;The last line appears to be unnecessary at least for the sample data.&lt;BR /&gt;I have modified the code as below. See if this is what you intended.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data firstlast;
   input string $60.;
   
   First_Word=scan(string,1,"&amp;amp;");
  Last_Word=scan(string, -1,"&amp;amp;");
 
datalines;
Jack and Jill
Bob &amp;amp; 
Carol &amp;amp; 
Ted &amp;amp; 
Alice &amp;amp;
Leonardo
Gates
;
proc print data=firstlast;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output will be as follows&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sajid01_0-1698846624098.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89311i21A6E7D0DB91D51A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Sajid01_0-1698846624098.png" alt="Sajid01_0-1698846624098.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 13:51:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-first-and-last-names-if-last-word-is-space/m-p/901070#M356106</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2023-11-01T13:51:09Z</dc:date>
    </item>
    <item>
      <title>Re: how to extract first and last names if last word is space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-extract-first-and-last-names-if-last-word-is-space/m-p/901184#M356154</link>
      <description>Thank you &lt;BR /&gt;s_Lassen</description>
      <pubDate>Thu, 02 Nov 2023 05:53:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-extract-first-and-last-names-if-last-word-is-space/m-p/901184#M356154</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2023-11-02T05:53:31Z</dc:date>
    </item>
  </channel>
</rss>

