<?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: Remove &amp;quot;III&amp;quot; suffix from name variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Remove-quot-III-quot-suffix-from-name-variable/m-p/719367#M222741</link>
    <description>&lt;P&gt;The order in which you use TRANWRD to remove values is removing the first two characters from "III" leaving just a single "I" behind before the attempt to find "III", so a match will never be found for "III".&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Changing the order should resolve that issue:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	do word= 'JR', 'SR', 'III', 'II', 'IV' ;
		new_FirstName = tranwrd(' '||new_FirstName, ' '||strip(word)||' ', ' ');&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 15 Feb 2021 16:06:07 GMT</pubDate>
    <dc:creator>SASJedi</dc:creator>
    <dc:date>2021-02-15T16:06:07Z</dc:date>
    <item>
      <title>Remove "III" suffix from name variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-quot-III-quot-suffix-from-name-variable/m-p/719345#M222736</link>
      <description>&lt;P&gt;I am cleaning raw data for matching purposes with another dataset. The K dataset requires a lot of reformatting, including moving the suffices from the FirstName variable to Suffix variable and removing them from the FirstName variable. This code works to move the suffices to a new variable, but will not remove "III" from the Firstname variable. This works for other suffices, but not III. Including SAS programming specific to this section of data only.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SAMPLE IMPORTED DATA:&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;input FirstName:$13.;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;RICHARD&lt;/P&gt;&lt;P&gt;MICHAEL&lt;/P&gt;&lt;P&gt;JR&lt;/P&gt;&lt;P&gt;III&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;/* ADD SUFFIX VARIABLE AND REMOVE FROM FIRST NAME */
/* REMOVES ALL 'I' n_lastName = COMPRESS(new_lastName,' III'); */
data K5;
set K4;
new_FirstName = FirstName;
	length suffix $ 7;
	do word= 'JR', 'SR', 'II', 'III', 'IV' ;
		new_FirstName = tranwrd(' '||new_FirstName, ' '||strip(word)||' ', ' ');
	new_FirstName = compbl(new_FirstName);
	if  findw(FirstName, 'JR')&amp;gt;0 then do;
		suffix='JR';
		end;
	if  findw(FirstName, 'SR')&amp;gt;0 then do;
		suffix='SR';
		end;
	if  findw(FirstName, 'II')&amp;gt;0 then do;
		suffix='II';
		end;
	if  findw(FirstName, 'III')&amp;gt;0 then do;
		suffix='III';
		end;
	if  findw(FirstName, 'IV')&amp;gt;0 then do;
		suffix='IV';
		end;
	end;
	drop word; 
	*newlastName newlName last_name first_name middle_name lastName firstName middleName;
run;
/* NOTE: III IS STILL PRESENT IN new_FirstName */&lt;/PRE&gt;&lt;P&gt;SAMPLE K5 OUTPUT&lt;/P&gt;&lt;P&gt;FirstName new_FirstName suffix&lt;/P&gt;&lt;P&gt;RICHARD&amp;nbsp;RICHARD&lt;/P&gt;&lt;P&gt;MICHAEL&amp;nbsp;MICHAEL&lt;/P&gt;&lt;P&gt;JR&amp;nbsp; &amp;nbsp;JR&lt;/P&gt;&lt;P&gt;III III III&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;/* MOVE FIRST NAME FROM MIDDLENAME VARIABLE */
/* need to drop iii */
data K6;
set K5;
if new_FirstName = "III" then do;
new_FirstName = "";
end;
If missing(new_FirstName) then new_FirstName=MiddleName;
run;&lt;/PRE&gt;&lt;P&gt;SAMPLE K6 OUTPUT&lt;/P&gt;&lt;P&gt;FirstName new_FirstName suffix&lt;/P&gt;&lt;P&gt;RICHARD&amp;nbsp;RICHARD&lt;/P&gt;&lt;P&gt;MICHAEL&amp;nbsp;MICHAEL&lt;/P&gt;&lt;P&gt;JR WILLIAM JR&lt;/P&gt;&lt;P&gt;III III III&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I successfully drop the "III" from the name variable?&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2021 14:24:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-quot-III-quot-suffix-from-name-variable/m-p/719345#M222736</guid>
      <dc:creator>ILoveKismet</dc:creator>
      <dc:date>2021-02-15T14:24:12Z</dc:date>
    </item>
    <item>
      <title>Re: Remove "III" suffix from name variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-quot-III-quot-suffix-from-name-variable/m-p/719366#M222740</link>
      <description>&lt;P&gt;1) I ran next code. check the lo - there is no word='III' - only 'II'.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data K5;
 set K4;
    new_FirstName = FirstName;
	*length suffix $ 7;
	do word= 'JR', 'SR', 'II', 'III', 'IV' ;
		new_FirstName = tranwrd(' '||strip(new_FirstName), ' '||strip(word)||' ', ' ');
	put word=  new_FirstName=;   
	    new_FirstName = compbl(new_FirstName);
	end;
run;	
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; The reason - first word='JR' - only 2 characters, therefore length of word is 2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Add the statement: &lt;STRONG&gt;length word $3;&amp;nbsp;&lt;/STRONG&gt; or the max length of all words.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) The syntax of tranwrd() function is:&lt;/P&gt;
&lt;DIV class="sgml"&gt;
&lt;TABLE cellspacing="2" cellpadding="4"&gt;
&lt;TBODY&gt;
&lt;TR valign="top"&gt;
&lt;TD&gt;&lt;SPAN class="strong"&gt;TRANWRD&lt;/SPAN&gt;(&lt;SPAN class="emph"&gt;source,target,replacement&lt;/SPAN&gt;)&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;I ran next code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _NULL_;
   length FirstName new_FirstName $13 word $3;
   FirstName = 'III';
   word = 'III';
   new_FirstName = tranwrd(' '||strip(new_FirstName), ' '||strip(word)||' ', ' ');
   put FirstName= new_FirstName=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;the result in log is:&amp;nbsp;&lt;STRONG&gt;&lt;SPAN&gt;FirstName=III new_FirstName=&amp;nbsp;&lt;/SPAN&gt;&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is that what you want ? Pay attention to strip() added for New_FirstName,&lt;/P&gt;
&lt;P&gt;and to LENGTH applying to WORD and to New_FirstName.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3) In all your &lt;STRONG&gt;DO;...END;&lt;/STRONG&gt; parts there is only &lt;U&gt;one&lt;/U&gt; statement to do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; Why not just:&amp;nbsp;&lt;STRONG&gt;if findw(FirstName, 'JR')&amp;gt;0 then suffix='JR';&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; the same for all other suffixes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4) I don't understand why you call it "suffix" if you relate to the whole string of&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; FirstName ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/DIV&gt;</description>
      <pubDate>Mon, 15 Feb 2021 16:22:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-quot-III-quot-suffix-from-name-variable/m-p/719366#M222740</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-02-15T16:22:28Z</dc:date>
    </item>
    <item>
      <title>Re: Remove "III" suffix from name variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-quot-III-quot-suffix-from-name-variable/m-p/719367#M222741</link>
      <description>&lt;P&gt;The order in which you use TRANWRD to remove values is removing the first two characters from "III" leaving just a single "I" behind before the attempt to find "III", so a match will never be found for "III".&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Changing the order should resolve that issue:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	do word= 'JR', 'SR', 'III', 'II', 'IV' ;
		new_FirstName = tranwrd(' '||new_FirstName, ' '||strip(word)||' ', ' ');&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Feb 2021 16:06:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-quot-III-quot-suffix-from-name-variable/m-p/719367#M222741</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2021-02-15T16:06:07Z</dc:date>
    </item>
    <item>
      <title>Re: Remove "III" suffix from name variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-quot-III-quot-suffix-from-name-variable/m-p/719411#M222748</link>
      <description>&lt;P&gt;Thank you! I removed the extra/unnecessary statements as you suggested.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, I do want the following:&amp;nbsp;&lt;BR /&gt;&lt;SPAN&gt;the result in log is:&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;&lt;SPAN&gt;FirstName=III new_FirstName=&amp;nbsp;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The original FULLNAME variable was divided by space.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;If FULLNAME=BROWN JR JOHN&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Then LastName=BROWN FirstName=JR MiddleName=JOHN&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I wanted to add the suffix variable to remove suffices from the FirstName variable.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;In my next step I move the actual first name from MiddleName when a suffix is present:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* MOVE FIRST NAME FROM MIDDLENAME VARIABLE */
data K5;
set K4;
if missing(new_FirstName) then new_FirstName=MiddleName;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Feb 2021 17:53:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-quot-III-quot-suffix-from-name-variable/m-p/719411#M222748</guid>
      <dc:creator>ILoveKismet</dc:creator>
      <dc:date>2021-02-15T17:53:55Z</dc:date>
    </item>
    <item>
      <title>Re: Remove "III" suffix from name variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-quot-III-quot-suffix-from-name-variable/m-p/719453#M222757</link>
      <description>&lt;P&gt;This would probably be easier if you do it when you read in the original data, something like this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Make test data *.
data have;
  infile datalines truncover dlm=',';
  input FULLNAME:$40.;
datalines;
BROWN JR JOHN K
SPRAT III JACK CLIVE
LAMB MARY H
;;;;

data want;
   set have;
   array name[*] $ 20 Last First Middle  Suffix;
   drop i element;
   element=0;
   do i=1 to dim(name);
      name[i-element]=scan(fullname,i,' ');
      if upcase(compress(name[i-element],,'ka')) in ('II','III','IV','JR','SR') then do;
         Suffix=name[i-element];
         element+1;
      end;
   end;
run;
&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Feb 2021 19:16:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-quot-III-quot-suffix-from-name-variable/m-p/719453#M222757</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2021-02-15T19:16:19Z</dc:date>
    </item>
  </channel>
</rss>

