<?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: string variables with several parts, I want to delete certain parts in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452463#M114194</link>
    <description>Instead of using scan(var, countw(var), position, length), you can use scan(var, -1, position, length). Does the same but much faster and simpler (scans from the end instead of scanning twice from the start).&lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Mon, 09 Apr 2018 10:51:50 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2018-04-09T10:51:50Z</dc:date>
    <item>
      <title>string variables with several parts, I want to delete certain parts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452372#M114162</link>
      <description>&lt;P&gt;Hi everyone, thank you for reading my question!&lt;/P&gt;&lt;P&gt;I have a "cityname" variable whose data look like this:&lt;BR /&gt;"Autaugaville town"&lt;BR /&gt;"Billingsley town"&lt;BR /&gt;"Marbury CDP "&lt;BR /&gt;"Millbrook city"&lt;BR /&gt;"Pine Level CDP"&lt;BR /&gt;"Prattville city"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone know how to code efficiently to transform this variable into the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;"Autaugaville"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"Billingsley"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"Marbury "&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"Millbrook"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"Pine Level"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"Prattville"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I just want to delete the ending word whenever it's tow/city/CDP/borough/village...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my way, just wondering any other more efficient ways...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* city, borough, town, cdp, village, */&lt;BR /&gt;data xu1.citycounty;&lt;BR /&gt;set xu1.citycounty;&lt;BR /&gt;cityname = upcase(cityname);&lt;BR /&gt;cityname1 = scan(cityname,1,'');&lt;BR /&gt;cityname2 = scan(cityname,2,'');&lt;BR /&gt;cityname3 = scan(cityname,3,'');&lt;BR /&gt;cityname4 = scan(cityname,4,'');&lt;BR /&gt;cityname5 = scan(cityname,5,'');&lt;BR /&gt;cityname6 = scan(cityname,6,'');&lt;BR /&gt;if cityname2 in( 'CITY','TOWN','BOROUGH', 'VILLAGE','CDP') then cityname2= '';&lt;BR /&gt;if cityname3 in( 'CITY','TOWN','BOROUGH', 'VILLAGE','CDP') then cityname3= '';&lt;BR /&gt;if cityname4 in( 'CITY','TOWN','BOROUGH', 'VILLAGE','CDP') then cityname4= '';&lt;BR /&gt;if cityname5 in( 'CITY','TOWN','BOROUGH', 'VILLAGE','CDP') then cityname5= '';&lt;BR /&gt;if cityname6 in( 'CITY','TOWN','BOROUGH', 'VILLAGE','CDP') then cityname6= '';&lt;BR /&gt;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data xu1.citycounty;&lt;BR /&gt;set xu1.citycounty;&lt;BR /&gt;citynamenew= trim(cityname1)||''||trimn(cityname2)||''||trimn(cityname3)||''||trimn(cityname4)||''||trimn(cityname5)||''||trimn(cityname6);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thank you all!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Apr 2018 22:49:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452372#M114162</guid>
      <dc:creator>changxuosu1</dc:creator>
      <dc:date>2018-04-08T22:49:50Z</dc:date>
    </item>
    <item>
      <title>Re: string variables with several parts, I want to delete certain parts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452373#M114163</link>
      <description>&lt;P&gt;Use the SCAN FUNCTION.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, such a solution may run into problems if the city name has two words like St. Paul&lt;/P&gt;</description>
      <pubDate>Sun, 08 Apr 2018 22:34:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452373#M114163</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-04-08T22:34:13Z</dc:date>
    </item>
    <item>
      <title>Re: string variables with several parts, I want to delete certain parts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452375#M114165</link>
      <description>&lt;P&gt;thank you Paige... I tried scan function it worked!&lt;/P&gt;</description>
      <pubDate>Sun, 08 Apr 2018 22:51:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452375#M114165</guid>
      <dc:creator>changxuosu1</dc:creator>
      <dc:date>2018-04-08T22:51:40Z</dc:date>
    </item>
    <item>
      <title>Re: string variables with several parts, I want to delete certain parts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452379#M114167</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input var $40. ;
var=dequote(var);
datalines;
"Autaugaville town"
"Billingsley town"
"Marbury CDP "
"Millbrook city"
"Pine Level CDP"
"Prattville city"
;

data want;
set have;
call scan(var, countw(var), position, length);
substr(var,position,length)=' ';
keep var;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 08 Apr 2018 23:00:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452379#M114167</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-04-08T23:00:22Z</dc:date>
    </item>
    <item>
      <title>Re: string variables with several parts, I want to delete certain parts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452391#M114175</link>
      <description>&lt;P&gt;I would try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if upcase(scan(cityname, -1)) in &lt;SPAN&gt;('CITY', 'TOWN', 'BOROUGH', 'VILLAGE', 'CDP')&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;they cityname = substr(cityname, 1, length(cityname) - length(scan(cityname, -1)));&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Apr 2018 02:40:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452391#M114175</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-04-09T02:40:19Z</dc:date>
    </item>
    <item>
      <title>Re: string variables with several parts, I want to delete certain parts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452395#M114176</link>
      <description>&lt;P&gt;Don't forget the case where there is a single name...&amp;nbsp; For the greatest flexibility, use regular expressions:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input str $60.;
datalines;
Autaugaville town
Billingsley town
Marbury CDP 
Millbrook city
Pine Level CDP
Prattville city
Somecity
;

data want;
set have;
city = prxChange("s/((\w+\s+)+?)(\w+\s+)$/$1/o",1,str);
run;

proc print; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Apr 2018 03:42:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452395#M114176</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-04-09T03:42:16Z</dc:date>
    </item>
    <item>
      <title>Re: string variables with several parts, I want to delete certain parts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452462#M114193</link>
      <description>&lt;P&gt;I would use PRX, like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  var=prxchange('s/\b(town|cdp|borough|city)\s*$//i',1,var);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;A short explanation: \b is a word boundary, (town|cdp|borough|city) is any of the nouns in question, \s* is an arbitrary amount of whitespace, and $ means end-of-string. All of which is replaced by nothing. The "i" at the end makes the search disregard letter cases.The "1" parameter means that the replacement is done only once.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Apr 2018 10:49:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452462#M114193</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-04-09T10:49:19Z</dc:date>
    </item>
    <item>
      <title>Re: string variables with several parts, I want to delete certain parts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452463#M114194</link>
      <description>Instead of using scan(var, countw(var), position, length), you can use scan(var, -1, position, length). Does the same but much faster and simpler (scans from the end instead of scanning twice from the start).&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 09 Apr 2018 10:51:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452463#M114194</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-04-09T10:51:50Z</dc:date>
    </item>
    <item>
      <title>Re: string variables with several parts, I want to delete certain parts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452483#M114201</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input var $quote40. ;
want=prxchange('s/\w+$//',1,strip(var));
datalines;
"Autaugaville town"
"Billingsley town"
"Marbury CDP "
"Millbrook city"
"Pine Level CDP"
"Prattville city"
;
proc print;run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Apr 2018 12:46:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/452483#M114201</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-04-09T12:46:44Z</dc:date>
    </item>
    <item>
      <title>Re: string variables with several parts, I want to delete certain parts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/454185#M114801</link>
      <description>&lt;P&gt;Your code works perfectly! Thank you so much!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 14 Apr 2018 18:43:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/454185#M114801</guid>
      <dc:creator>changxuosu1</dc:creator>
      <dc:date>2018-04-14T18:43:05Z</dc:date>
    </item>
    <item>
      <title>Re: string variables with several parts, I want to delete certain parts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/454288#M114811</link>
      <description>&lt;P&gt;thank you so much. This one takes into the account the ending name may not be&amp;nbsp;&lt;SPAN&gt;in&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;('CITY', 'TOWN', 'BOROUGH', 'VILLAGE', 'CDP').&amp;nbsp; Really appreciate your input.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Apr 2018 19:44:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/string-variables-with-several-parts-I-want-to-delete-certain/m-p/454288#M114811</guid>
      <dc:creator>changxuosu1</dc:creator>
      <dc:date>2018-04-15T19:44:46Z</dc:date>
    </item>
  </channel>
</rss>

