<?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 Can an array of columns be used in FINDW and TRANWRD? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Can-an-array-of-columns-be-used-in-FINDW-and-TRANWRD/m-p/435075#M108037</link>
    <description>&lt;P&gt;Hi all! I am in the process in completing physical address cleanup. One of the issues I want to clean is removing the city name in the address line. I have created mock data, via datalines, to illustrate example data in a table called "Address_Mock". I have a table called "ZipCodes_Clean" which contains all U.S. states and territories zip codes with all the possible cities attributed to those zip codes in columns named City1 to City130. The following is an example&amp;nbsp;from the table:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Zipcode State Country WorldRegion City1 City2 City3 City4 City5 City6 City7 City8 City9 City10&lt;BR /&gt;33172 FL US NA MIAMI DORAL DORAL BRANCH SWEETWATER WEST MIAMI&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am using two SAS functions (FINDW and TRANWRD)&amp;nbsp;in a conditional IF-THEN statement to locate if the city is in the address line, create new&amp;nbsp;column containing the address line with the city replaced by blank space. However, the final result has been blank result in the new column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is the mock code of how I am currently trying to resolve the issue:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Address_Mock;
infile datalines dsd;
input Name: $8. Address_Line: $26. ZipCode ;
datalines;
MICHELLE,"10597 NW 7 TER MIAMI",33172
NICOLE,"10597 NW 7 TER DORAL",33172
MARIA,"10597 NW 7 TER WEST MIAMI",33172
ANTONIO,"10597 NW 7 TER SWEETWATER",33172
;
run;

proc sql;
create table Address_Join as
select t1.*, t2.*
from Address_Mock as t1 left join ZipCodes_Clean as t2 on t1.ZipCode=t2.ZipCode;
quit;


data Address_Loop;
set Address_Join;

array cols {*} city1-city130 ;

do i=1 to 130;
if FINDW(Address_Line,cols{i}) then New_Address_Line=TRANWRD(Address_Line,cols{i},"");
end;
&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;SPAN&gt;I'll be very appreciative if&amp;nbsp;anyone sees the error in the code or I'm open to trying alternative code. Thanks in advance!&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 07 Feb 2018 21:22:59 GMT</pubDate>
    <dc:creator>mc</dc:creator>
    <dc:date>2018-02-07T21:22:59Z</dc:date>
    <item>
      <title>Can an array of columns be used in FINDW and TRANWRD?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-an-array-of-columns-be-used-in-FINDW-and-TRANWRD/m-p/435075#M108037</link>
      <description>&lt;P&gt;Hi all! I am in the process in completing physical address cleanup. One of the issues I want to clean is removing the city name in the address line. I have created mock data, via datalines, to illustrate example data in a table called "Address_Mock". I have a table called "ZipCodes_Clean" which contains all U.S. states and territories zip codes with all the possible cities attributed to those zip codes in columns named City1 to City130. The following is an example&amp;nbsp;from the table:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Zipcode State Country WorldRegion City1 City2 City3 City4 City5 City6 City7 City8 City9 City10&lt;BR /&gt;33172 FL US NA MIAMI DORAL DORAL BRANCH SWEETWATER WEST MIAMI&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am using two SAS functions (FINDW and TRANWRD)&amp;nbsp;in a conditional IF-THEN statement to locate if the city is in the address line, create new&amp;nbsp;column containing the address line with the city replaced by blank space. However, the final result has been blank result in the new column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is the mock code of how I am currently trying to resolve the issue:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Address_Mock;
infile datalines dsd;
input Name: $8. Address_Line: $26. ZipCode ;
datalines;
MICHELLE,"10597 NW 7 TER MIAMI",33172
NICOLE,"10597 NW 7 TER DORAL",33172
MARIA,"10597 NW 7 TER WEST MIAMI",33172
ANTONIO,"10597 NW 7 TER SWEETWATER",33172
;
run;

proc sql;
create table Address_Join as
select t1.*, t2.*
from Address_Mock as t1 left join ZipCodes_Clean as t2 on t1.ZipCode=t2.ZipCode;
quit;


data Address_Loop;
set Address_Join;

array cols {*} city1-city130 ;

do i=1 to 130;
if FINDW(Address_Line,cols{i}) then New_Address_Line=TRANWRD(Address_Line,cols{i},"");
end;
&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;SPAN&gt;I'll be very appreciative if&amp;nbsp;anyone sees the error in the code or I'm open to trying alternative code. Thanks in advance!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Feb 2018 21:22:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-an-array-of-columns-be-used-in-FINDW-and-TRANWRD/m-p/435075#M108037</guid>
      <dc:creator>mc</dc:creator>
      <dc:date>2018-02-07T21:22:59Z</dc:date>
    </item>
    <item>
      <title>Re: Can an array of columns be used in FINDW and TRANWRD?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-an-array-of-columns-be-used-in-FINDW-and-TRANWRD/m-p/435112#M108046</link>
      <description>&lt;P&gt;You might speed things up a bit with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i = 1 to 130 while(not missing(cols{i}));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Feb 2018 23:20:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-an-array-of-columns-be-used-in-FINDW-and-TRANWRD/m-p/435112#M108046</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-02-07T23:20:57Z</dc:date>
    </item>
    <item>
      <title>Re: Can an array of columns be used in FINDW and TRANWRD?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-an-array-of-columns-be-used-in-FINDW-and-TRANWRD/m-p/435164#M108063</link>
      <description>&lt;P&gt;I would propose a few changes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* City names must be ordered by decreasing length (i.e. WEST MIAMI before MIAMI) */
data zip;
infile datalines truncover;
input Zipcode (State Country WorldRegion City1 City2 City3 City4 City5 City6 City7 City8 City9 City10) (&amp;amp;:$20.);
datalines;
33172 FL  US  NA  WEST MIAMI  MIAMI  DORAL BRANCH  DORAL  SWEETWATER 
;

data Addr;
infile datalines dsd;
input Name: $8. Line: $26. ZipCode ;
datalines;
MICHELLE,"10597 NW 7 TER MIAMI",33172
NICOLE,"10597 NW 7 TER DORAL",33172
MARIA,"10597 NW 7 TER WEST MIAMI",33172
ANTONIO,"10597 NW 7 TER SWEETWATER",33172
;

proc sql;
create table A as
select *
from Addr as t1 left join Zip as t2 on t1.ZipCode=t2.ZipCode;
quit;

/* Remove city names at the end of address lines */
data B;
set A;

array cols {*} city1-city130 ;

do i=1 to dim(cols) while(not missing(cols{i}));
    pos = length(line)-length(cols{i})+1;
    if pos &amp;gt; 2 then
        if notalpha(substr(line,pos-1,1)) then
            if substr(line, pos) = cols{i} then do;
                line = substr(line,1,pos-2);
                leave;
                end;
    end;
drop pos city: i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Feb 2018 05:44:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-an-array-of-columns-be-used-in-FINDW-and-TRANWRD/m-p/435164#M108063</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-02-08T05:44:17Z</dc:date>
    </item>
    <item>
      <title>Re: Can an array of columns be used in FINDW and TRANWRD?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-an-array-of-columns-be-used-in-FINDW-and-TRANWRD/m-p/435447#M108194</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Great&amp;nbsp;modifications to the&amp;nbsp;code! I was able to get the desired outcome. Thanks for noting&amp;nbsp;to organize the city names by descending character length;&amp;nbsp;it is definitely required in order to use the&amp;nbsp;positional approach to remove the city names.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Feb 2018 21:20:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-an-array-of-columns-be-used-in-FINDW-and-TRANWRD/m-p/435447#M108194</guid>
      <dc:creator>mc</dc:creator>
      <dc:date>2018-02-08T21:20:08Z</dc:date>
    </item>
  </channel>
</rss>

