<?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: Country Code Format Lookup in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Country-Code-Format-Lookup/m-p/250268#M47177</link>
    <description>&lt;P&gt;I assume that the OP doens't have access to compete lists of valid zip codes, hence this half way validation.&lt;/P&gt;
&lt;P&gt;If there are many other validations in your process, I'm thinking a data equality&amp;nbsp;product, like DataFlux, could be of interest.&lt;/P&gt;
&lt;P&gt;Another way that&amp;nbsp;may work (untested) is to use translate() to convert the zip codes to the pattern, and then do the&amp;nbsp;lookup (join/merge) with your pattern&amp;nbsp;data set, and the test for equality.&lt;/P&gt;</description>
    <pubDate>Tue, 16 Feb 2016 09:26:02 GMT</pubDate>
    <dc:creator>LinusH</dc:creator>
    <dc:date>2016-02-16T09:26:02Z</dc:date>
    <item>
      <title>Country Code Format Lookup</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Country-Code-Format-Lookup/m-p/250264#M47174</link>
      <description>&lt;P&gt;I have two datasets. One with &lt;STRONG&gt;all&lt;/STRONG&gt; the countries and respective postal code formats as follows (note: N means a number, A means an alphabet):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dataset1:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Country &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; Format&lt;/P&gt;
&lt;P&gt;Albania &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; NNNN&lt;/P&gt;
&lt;P&gt;Argentina &amp;nbsp; &amp;nbsp; &amp;nbsp;ANNNNAAA&lt;/P&gt;
&lt;P&gt;Canada &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;ANA NAN&lt;/P&gt;
&lt;P&gt;United States NNNNN&lt;/P&gt;
&lt;P&gt;........and so on&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Other dataset with &lt;STRONG&gt;some&lt;/STRONG&gt;&amp;nbsp;counties and postal codes as follows:&lt;/P&gt;
&lt;P&gt;dataset2:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Country &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Zip_Code&lt;/P&gt;
&lt;P&gt;United States &amp;nbsp; &amp;nbsp; &amp;nbsp; 55344&lt;/P&gt;
&lt;P&gt;United States &amp;nbsp; &amp;nbsp; &amp;nbsp;TW18 4BQ&lt;/P&gt;
&lt;P&gt;Canada &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;V6C 2X8&lt;/P&gt;
&lt;P&gt;France &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;31360&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I need to accomplish is I need to lookup countries and zip codes in dataset 2 to their respective format in dataset 1 and flag if it is in improper format. Desired output as below:&lt;/P&gt;
&lt;P&gt;Country &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Zip_Code &amp;nbsp; Improper_Flag&lt;/P&gt;
&lt;P&gt;United States &amp;nbsp; &amp;nbsp; &amp;nbsp; 55344 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; N&lt;/P&gt;
&lt;P&gt;United States &amp;nbsp; &amp;nbsp; &amp;nbsp;TW18 4BQ &amp;nbsp; &amp;nbsp;Y&lt;/P&gt;
&lt;P&gt;Canada &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;V6C 2X8 &amp;nbsp; &amp;nbsp; &amp;nbsp; N&lt;/P&gt;
&lt;P&gt;France &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;31360 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;N&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help is greatly appreciated. Thank you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2016 08:54:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Country-Code-Format-Lookup/m-p/250264#M47174</guid>
      <dc:creator>sasmaverick</dc:creator>
      <dc:date>2016-02-16T08:54:19Z</dc:date>
    </item>
    <item>
      <title>Re: Country Code Format Lookup</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Country-Code-Format-Lookup/m-p/250266#M47175</link>
      <description>&lt;P&gt;Why is the format so important here? &amp;nbsp;It would seem logical to me to ensure the post codes are within the list of Postcodes given rather than format, if they are within that list then they are clean and in the correct format, if they are not within that list they are incorrect. &amp;nbsp;Checking the fomat doesn't check that the post code is valid?&lt;/P&gt;
&lt;P&gt;If you have to then a simple routine to create a coded variable and merge that: &amp;nbsp;(Do please note for the future how I have created test data in the form of a datastep):&lt;/P&gt;
&lt;PRE&gt;data codes;
  infile datalines dlm=",";
  length country format $20;
  input country $ format $;
datalines;
Albania,NNNN
Argentina,ANNNNAAA
Canada,ANA NAN
United States,NNNNN
;
run;

data have (drop=i chr);
  infile datalines dlm=",";
  length country zipcode format $20;
  input country $ zipcode $;
  do i=1 to 20;
    chr=char(zipcode,i);
    if chr=" " then substr(format,i,1)=chr;
    if chr ne " " and lengthn(compress(chr," ","a"))=0 then substr(format,i,1)="A";
    if chr ne " " and lengthn(compress(chr," ","d"))=0 then substr(format,i,1)="N"; 
  end;    
datalines;
United States,55344
United States,TW18 4BQ
Canada,V6C 2X8
France,31360
;
run;

proc sql;
  create table WANT as
  select  A.*,
          case when not exists(select distinct COUNTRY from CODES where COUNTRY=A.COUNTRY and FORMAT=A.FORMAT) then "N"
               else "Y" end as IMPROPER_FLAG
  from    HAVE A;
quit;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Feb 2016 09:21:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Country-Code-Format-Lookup/m-p/250266#M47175</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-02-16T09:21:23Z</dc:date>
    </item>
    <item>
      <title>Re: Country Code Format Lookup</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Country-Code-Format-Lookup/m-p/250268#M47177</link>
      <description>&lt;P&gt;I assume that the OP doens't have access to compete lists of valid zip codes, hence this half way validation.&lt;/P&gt;
&lt;P&gt;If there are many other validations in your process, I'm thinking a data equality&amp;nbsp;product, like DataFlux, could be of interest.&lt;/P&gt;
&lt;P&gt;Another way that&amp;nbsp;may work (untested) is to use translate() to convert the zip codes to the pattern, and then do the&amp;nbsp;lookup (join/merge) with your pattern&amp;nbsp;data set, and the test for equality.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2016 09:26:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Country-Code-Format-Lookup/m-p/250268#M47177</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-02-16T09:26:02Z</dc:date>
    </item>
    <item>
      <title>Re: Country Code Format Lookup</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Country-Code-Format-Lookup/m-p/250273#M47178</link>
      <description>&lt;P&gt;Here is an example using regular expressions, the code translates the A and N into the corresponding expression and then uses PRXMATCH to test&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ctry_format;
  infile cards dlm=",";
  input
    country : $32.
    zip_format : $32.
  ;
  length prx_expression $ 128;
  prx_expression = "/";

  drop i;
  do i = 1 to length(zip_format);
    select (char(zip_format, i));
      when ("N") do;
        prx_expression = cats(prx_expression, "\d");
      end;

      when ("A") do;
        prx_expression = cats(prx_expression, "[A-Z]");
      end;

      when (" ") do;
        prx_expression = cats(prx_expression, "\x20");
      end;
    end;
  end;

  prx_expression = cats(prx_expression, "/");
  cards;
Canada,ANA NAN
France,NNNNN
Switzerland,NNNN
United States,NNNNN
;

data ctry_have;
  infile cards dlm=",";
  input
    country : $32.
    zip : $32.
  ;
  cards;
Canada,A6C 2X8
Canada,A6C-2X8
France,31360
Switzerland,123
United States,55344
United States,TW18 4BQ
;

data want;
  merge ctry_format ctry_have;
  by country;
  prxmatch = ( prxmatch(prx_expression, trim(zip)) &amp;gt; 0 );
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bruno&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2016 09:50:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Country-Code-Format-Lookup/m-p/250273#M47178</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2016-02-16T09:50:35Z</dc:date>
    </item>
    <item>
      <title>Re: Country Code Format Lookup</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Country-Code-Format-Lookup/m-p/250276#M47179</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm=",";
length country zip format $20;
input country $ zip $;
retain Rx;
if _N_=1 then Rx=rxparse("$U to A,$D to N"); 
format= rxchange(rx, 1000, zip); 
drop Rx;
cards;
Canada,A6C 2X8
Canada,A6C-2X8
France,31360
Switzerland,123
United States,55344
United States,TW18 4BQ
;run;

data codes;
  infile cards dlm=",";
  input country : $20. code : $20. ;
  cards;
Canada,ANA NAN
France,NNNNN
Switzerland,NNNN
United States,NNNNN
;
run;
data want;
  merge have codes;
  by country;
 if trim(format)= trim(code) then Improper_Flag='N';
 else Improper_Flag='Y';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Feb 2016 10:47:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Country-Code-Format-Lookup/m-p/250276#M47179</guid>
      <dc:creator>mohamed_zaki</dc:creator>
      <dc:date>2016-02-16T10:47:41Z</dc:date>
    </item>
    <item>
      <title>Re: Country Code Format Lookup</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Country-Code-Format-Lookup/m-p/250490#M47231</link>
      <description>&lt;P&gt;It looks like Perl Regular Expression thing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ctry_format;
  infile cards dlm=",";
  input
    country : $32.
    zip_format : $32.
  ;
  length prx $ 200;
  prx =zip_format ;
  prx =tranwrd(strip(prx ),'A','[a-z]');
  prx =tranwrd(strip(prx ),'N','\d');
  prx =tranwrd(strip(prx ),' ','\s');
  cards;
Canada,ANA NAN
France,NNNNN
Switzerland,NNNN
United States,NNNNN
;
run;
data ctry_have;
  infile cards dlm=",";
  input
    country : $32.
    zip : $32.
  ;
  cards;
Canada,A6C 2X8
Canada,A6C-2X8
France,31360
Switzerland,123
United States,55344
United States,TW18 4BQ
;

data want;
  merge ctry_format ctry_have;
  by country;
if not missing(prx) then do;
  if prxmatch(cats('/^',prx,'$/i'),strip(zip)) then Improper_Flag='N';
   else Improper_Flag='Y';
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Feb 2016 02:57:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Country-Code-Format-Lookup/m-p/250490#M47231</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-02-17T02:57:15Z</dc:date>
    </item>
    <item>
      <title>Re: Country Code Format Lookup</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Country-Code-Format-Lookup/m-p/250521#M47239</link>
      <description>Thanks a lot! Works for me</description>
      <pubDate>Wed, 17 Feb 2016 07:05:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Country-Code-Format-Lookup/m-p/250521#M47239</guid>
      <dc:creator>sasmaverick</dc:creator>
      <dc:date>2016-02-17T07:05:49Z</dc:date>
    </item>
    <item>
      <title>Re: Country Code Format Lookup</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Country-Code-Format-Lookup/m-p/250523#M47240</link>
      <description>&lt;P&gt;Thanks a lot Xia! Your approach works too!&lt;/P&gt;</description>
      <pubDate>Wed, 17 Feb 2016 07:06:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Country-Code-Format-Lookup/m-p/250523#M47240</guid>
      <dc:creator>sasmaverick</dc:creator>
      <dc:date>2016-02-17T07:06:56Z</dc:date>
    </item>
  </channel>
</rss>

