<?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 need help with exclusion in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/need-help-with-exclusion/m-p/651242#M195373</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset called "coupon_data" which has 5 coupon code columns for each prod ID.&amp;nbsp; There are some invalid coupon code in this dataset that I need to remove. I also have a list of invalid coupon codes and I am calling it as "invalid_codes". Basically I want compare my&amp;nbsp;"coupon_data" against&amp;nbsp; "invalid_codes" and if match found I want to remove that code from the coupon column.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am fairly new to hashing/array concept and i am sure you can judge that from my below experimental code. I know i am doing something wrong but i couldn't figure out&amp;nbsp; and I am not getting the desired output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could someone please help? Thanks.&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 coupon_data;
input @1 prod_ID :$6. @8 coupon1 $5. @14 coupon2 $5.  @20 coupon3 $5. @26 coupon4 $5. @32 coupon5 $5.; 
infile cards missover ;
cards;
132522 KCB33 CKM90 LKJ89 LKU09 NM789
258798 OUI00 UIT78 POU89 CC909 
548998 NM789       PI980 VBV46 JKO09
109743 YUO90 JJI99 POU89       MOP09
;
run;

Data invalid_codes;
input inv_coupon $5.;
infile cards;
cards;
LKJ89
CC909
MOP09
YUO90
;
Quit;


data valid_codes;
  set coupon_data;
  array cpn [5] coupon1-coupon5;
  array outcpn [5] $5;
  if _N_=1 then do;
    dcl hash H(dataset:'invalid_codes');
    H.definekey('inv_coupon');
    H.definedone(); 
    if 0 then set invalid_codes;
  end;
  do i=1 to 5;
    coupon=cpn[i];   
    if H.find()=0 then do;     
      outcpn[i]=cpn[i];
     end;
  end;
  drop  coupon coupon1-coupon5 i inv_coupon;
rename outcpn1-outcpn5 = coupon1-coupon5;	
run;


&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Desired output:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;prod_ID&lt;/TD&gt;&lt;TD&gt;coupon1&lt;/TD&gt;&lt;TD&gt;coupon2&lt;/TD&gt;&lt;TD&gt;coupon3&lt;/TD&gt;&lt;TD&gt;coupon4&lt;/TD&gt;&lt;TD&gt;coupon5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;132522&lt;/TD&gt;&lt;TD&gt;KCB33&lt;/TD&gt;&lt;TD&gt;CKM90&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;LKU09&lt;/TD&gt;&lt;TD&gt;NM789&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;258798&lt;/TD&gt;&lt;TD&gt;OUI00&lt;/TD&gt;&lt;TD&gt;UIT78&lt;/TD&gt;&lt;TD&gt;POU89&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;548998&lt;/TD&gt;&lt;TD&gt;NM789&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;PI980&lt;/TD&gt;&lt;TD&gt;VBV46&lt;/TD&gt;&lt;TD&gt;JKO09&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;109743&lt;/TD&gt;&lt;TD&gt;YUO90&lt;/TD&gt;&lt;TD&gt;JJI99&lt;/TD&gt;&lt;TD&gt;POU89&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
    <pubDate>Wed, 27 May 2020 23:28:34 GMT</pubDate>
    <dc:creator>Cbob03</dc:creator>
    <dc:date>2020-05-27T23:28:34Z</dc:date>
    <item>
      <title>need help with exclusion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-with-exclusion/m-p/651242#M195373</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset called "coupon_data" which has 5 coupon code columns for each prod ID.&amp;nbsp; There are some invalid coupon code in this dataset that I need to remove. I also have a list of invalid coupon codes and I am calling it as "invalid_codes". Basically I want compare my&amp;nbsp;"coupon_data" against&amp;nbsp; "invalid_codes" and if match found I want to remove that code from the coupon column.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am fairly new to hashing/array concept and i am sure you can judge that from my below experimental code. I know i am doing something wrong but i couldn't figure out&amp;nbsp; and I am not getting the desired output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could someone please help? Thanks.&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 coupon_data;
input @1 prod_ID :$6. @8 coupon1 $5. @14 coupon2 $5.  @20 coupon3 $5. @26 coupon4 $5. @32 coupon5 $5.; 
infile cards missover ;
cards;
132522 KCB33 CKM90 LKJ89 LKU09 NM789
258798 OUI00 UIT78 POU89 CC909 
548998 NM789       PI980 VBV46 JKO09
109743 YUO90 JJI99 POU89       MOP09
;
run;

Data invalid_codes;
input inv_coupon $5.;
infile cards;
cards;
LKJ89
CC909
MOP09
YUO90
;
Quit;


data valid_codes;
  set coupon_data;
  array cpn [5] coupon1-coupon5;
  array outcpn [5] $5;
  if _N_=1 then do;
    dcl hash H(dataset:'invalid_codes');
    H.definekey('inv_coupon');
    H.definedone(); 
    if 0 then set invalid_codes;
  end;
  do i=1 to 5;
    coupon=cpn[i];   
    if H.find()=0 then do;     
      outcpn[i]=cpn[i];
     end;
  end;
  drop  coupon coupon1-coupon5 i inv_coupon;
rename outcpn1-outcpn5 = coupon1-coupon5;	
run;


&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Desired output:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;prod_ID&lt;/TD&gt;&lt;TD&gt;coupon1&lt;/TD&gt;&lt;TD&gt;coupon2&lt;/TD&gt;&lt;TD&gt;coupon3&lt;/TD&gt;&lt;TD&gt;coupon4&lt;/TD&gt;&lt;TD&gt;coupon5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;132522&lt;/TD&gt;&lt;TD&gt;KCB33&lt;/TD&gt;&lt;TD&gt;CKM90&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;LKU09&lt;/TD&gt;&lt;TD&gt;NM789&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;258798&lt;/TD&gt;&lt;TD&gt;OUI00&lt;/TD&gt;&lt;TD&gt;UIT78&lt;/TD&gt;&lt;TD&gt;POU89&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;548998&lt;/TD&gt;&lt;TD&gt;NM789&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;PI980&lt;/TD&gt;&lt;TD&gt;VBV46&lt;/TD&gt;&lt;TD&gt;JKO09&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;109743&lt;/TD&gt;&lt;TD&gt;YUO90&lt;/TD&gt;&lt;TD&gt;JJI99&lt;/TD&gt;&lt;TD&gt;POU89&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Wed, 27 May 2020 23:28:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-with-exclusion/m-p/651242#M195373</guid>
      <dc:creator>Cbob03</dc:creator>
      <dc:date>2020-05-27T23:28:34Z</dc:date>
    </item>
    <item>
      <title>Re: need help with exclusion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-with-exclusion/m-p/651243#M195374</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/207568"&gt;@Cbob03&lt;/a&gt;&amp;nbsp; &amp;nbsp;Good try albeit requires minor and lazy correction imho&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
Data coupon_data;
input @1 prod_ID :$6. @8 coupon1 $5. @14 coupon2 $5.  @20 coupon3 $5. @26 coupon4 $5. @32 coupon5 $5.; 
infile cards missover ;
cards;
132522 KCB33 CKM90 LKJ89 LKU09 NM789
258798 OUI00 UIT78 POU89 CC909 
548998 NM789       PI980 VBV46 JKO09
109743 YUO90 JJI99 POU89       MOP09
;
run;

Data invalid_codes;
input inv_coupon $5.;
infile cards;
cards;
LKJ89
CC909
MOP09
YUO90
;

data valid_codes;
  set coupon_data;
  array cpn  coupon1-coupon5;
  if _N_=1 then do;
    dcl hash H(dataset:'invalid_codes');
    H.definekey('inv_coupon');
    H.definedone(); 
    if 0 then set invalid_codes;
  end;
  do over cpn;
    if H.check(key:cpn)=0 then call missing(cpn);
  end;
  drop inv_coupon;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 May 2020 00:00:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-with-exclusion/m-p/651243#M195374</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-28T00:00:54Z</dc:date>
    </item>
    <item>
      <title>Re: need help with exclusion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-with-exclusion/m-p/651244#M195375</link>
      <description>&lt;P&gt;Lookup formats work well too:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
 value $Invalid_Codes
'LKJ89' = 'I'
'CC909' = 'I'
'MOP09' = 'I'
'YUO90' = 'I'
' '     = 'I'
other   = 'V'
;
run;

data valid_codes;
  set coupon_data;
  array cpn [5] coupon1-coupon5;
  array outcpn [5] $5;
  do i=1 to 5;   
    if put(cpn[i], $Invalid_Codes.) = 'V' then outcpn[i]=cpn[i];
  end;
  drop   coupon1-coupon5 i ;
  rename outcpn1-outcpn5 = coupon1-coupon5;	
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 May 2020 00:04:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-with-exclusion/m-p/651244#M195375</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2020-05-28T00:04:02Z</dc:date>
    </item>
    <item>
      <title>Re: need help with exclusion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-with-exclusion/m-p/651248#M195377</link>
      <description>&lt;P&gt;An existential lookup should be performed with the CHECK method.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;data valid_codes;
  set coupon_data;
  array cpn [5] coupon1-coupon5;

  if _N_=1 then do;
    dcl hash H(dataset:'invalid_codes');
    H.definekey('inv_coupon');
    H.definedone(); 
    if 0 then set invalid_codes;
  end;

  do i=1 to 5;
    if H.check(key:cpn[i]) eq 0 then cpn[i] = '';  * coupon code found in hash of invalids, clear it;
  end;

  drop i inv_coupon;
run;&lt;/PRE&gt;
&lt;P&gt;View result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="RichardADeVenezia_0-1590624700952.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/40060iF5370D50D2A738F0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="RichardADeVenezia_0-1590624700952.png" alt="RichardADeVenezia_0-1590624700952.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 May 2020 00:12:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-with-exclusion/m-p/651248#M195377</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-05-28T00:12:01Z</dc:date>
    </item>
    <item>
      <title>Re: need help with exclusion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-with-exclusion/m-p/651284#M195393</link>
      <description>&lt;P&gt;As an alternative a custom Informat approach. You didn't say how many codes you may have so I show an approach to turn the invalid codes data set into a CNTLIN data set for use by Proc Format. There are some pretty specific rules about the names of the variables used by Proc Format and finding the approriate values for the TYPE (invalue or value, character or numeric) and the HLO column (used to set upper/lower inclusion boundaries or the "other" property) aren't always the easiest. Sometimes just create a cntlout data set with a small number of values.&lt;/P&gt;
&lt;P&gt;Then use the new informat to read the data set the value to blank for the invalid codes.&lt;/P&gt;
&lt;PRE&gt;Data invalid_codes;
input inv_coupon $5.;
infile cards;
cards;
LKJ89
CC909
MOP09
YUO90
;
data cntlin;
   set invalid_codes end=eof;
   fmtname='COUPON';
   default=5;
   length=5;
   type='J';
   start=inv_coupon;
   /* label needs to be long enough to hold _same_ below*/
   length label $ 6; 
   label=' ';
   output;
   if eof then do;
   /* what to do for "valid" codes*/
      call missing(start,label);
      HLO='O';
      label='_SAME_';
      output;
   end;
run;

proc format library=work  cntlin=cntlin ;
run;

Data coupon_data;
input @1 prod_ID :$6. @8 coupon1 $COUPON. @14 coupon2 $COUPON.  @20 coupon3 $COUPON.
     @26 coupon4 $COUPON. @32 coupon5 $COUPON.; 
infile cards missover ;
cards;
132522 KCB33 CKM90 LKJ89 LKU09 NM789
258798 OUI00 UIT78 POU89 CC909 
548998 NM789       PI980 VBV46 JKO09
109743 YUO90 JJI99 POU89       MOP09
;
run;&lt;/PRE&gt;
&lt;P&gt;The "other" statement with the HLO at the end likely isn't needed but I include it to show how to set such where it may be needed. A similar approach could be used with a list of VALID codes and an _error_ condition, set a bit differently, that would tell you when invalid values were encountered. So you could have 1) valid codes pass through with the _same_, known invalid codes get set to missing and an _error_ for values not on either. That way you would know whether to add it the newly encountered value to one of the sets (update the format&amp;nbsp; and reread the data with the new format).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the actual list of invalid codes is as small as you show it may be easier just to use Proc format code.&lt;/P&gt;
&lt;PRE&gt;proc format library=work cntlout=work.cntlout1;
invalue $coupon (default=5)
"LKJ89","CC909","MOP09","YUO90" = ' '
other = _same_
;
run;&lt;/PRE&gt;
&lt;P&gt;I have several projects where there is just enough flux in things like "site identifier codes" that I have Informats that do the _same_, set to missing and have an _error_ setting to tell me when some one sneaks a new code in so I can get all of the related information such as location, long name and other properties I use.&lt;/P&gt;
&lt;P&gt;The modified code to have "valid" , reset and _error_ could look like&lt;/P&gt;
&lt;PRE&gt;proc format library=work cntlout=work.cntlout1;
invalue $coupon (default=5)
"LKJ89","CC909","MOP09","YUO90" = ' '
"CKM90","JJI99","JKO09","KCB33","LKU09","NM789",
"OUI00","PI980","POU89","UIT78","VBV46" = _same_

other = _error_;
;
run;&lt;/PRE&gt;
&lt;P&gt;The _same_ are the valid codes.&lt;/P&gt;
&lt;P&gt;For an example, use this version of the informat after adding a new code that does not appear in the invalid codes or your previous data and rerun the data step reading it after updating the informat (or using a new informat name)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;IF however your invalid code list was per Prod_id , meaning the code might be valid for one product but not another, then the HASH would be the way to go as the Informat approach only uses single values. Not saying it couldn't be done but the complexity would not be worth the effort.&lt;/P&gt;</description>
      <pubDate>Thu, 28 May 2020 05:02:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-with-exclusion/m-p/651284#M195393</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-28T05:02:19Z</dc:date>
    </item>
    <item>
      <title>Re: need help with exclusion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-with-exclusion/m-p/651930#M195650</link>
      <description>Thank you very much!</description>
      <pubDate>Sat, 30 May 2020 06:20:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-with-exclusion/m-p/651930#M195650</guid>
      <dc:creator>Cbob03</dc:creator>
      <dc:date>2020-05-30T06:20:28Z</dc:date>
    </item>
    <item>
      <title>Re: need help with exclusion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-with-exclusion/m-p/651931#M195651</link>
      <description>Thank you for your response. I have lot many invalid codes to compare against so hash approach worked for my problem. Thanks again for your time!</description>
      <pubDate>Sat, 30 May 2020 06:24:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-with-exclusion/m-p/651931#M195651</guid>
      <dc:creator>Cbob03</dc:creator>
      <dc:date>2020-05-30T06:24:07Z</dc:date>
    </item>
  </channel>
</rss>

