<?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: Trying to get rid of cumbersome if logic in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332346#M74791</link>
    <description>&lt;P&gt;Perhaps you are looking for the SELECT-WHEN statement. An explanation and examples are available in the article &lt;A href="http://blogs.sas.com/content/iml/2016/06/20/select-when-sas-data-step.html" target="_self"&gt;"The SELECT statement in the SAS DATA step."&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 13 Feb 2017 21:05:58 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2017-02-13T21:05:58Z</dc:date>
    <item>
      <title>Trying to get rid of cumbersome if logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332342#M74788</link>
      <description>&lt;P&gt;I'm attempting to &amp;nbsp;transform my if logic into something more concise. The sample I provide is much smaller than the real data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines;
  input list $ 1-3 id $ 5-7 price $ 9-10;
  datalines;
BAC BB8 4a
BAC 22C  
BAC DDD 5b
CBA     6c
CBA 22A 4b
CBA 10B 4b
CBA 11A 5f
DAC 2CC  
DAC 11C 2e
DAC     3e
;
run;


data want;
  set have;
  if list='BAC' and id='BB8' and price='4a' then code='BBB';
  if list='BAC' and id='22C' and price=' ' then code='CCC';
  if list='BAC' and id='DDD' and price='5b' then code='DDD';
  if list='CBA' and id=' ' and price='6c' then code='FFF';
  if list='CBA' and id='22A' and price='4b' then code='FFF';
  if list='CBA' and id='10B' and price='4b' then code='GGG';
  if list='CBA' and id='11A' and price='5f' then code='HHH';
  if list='DAC' and id='2CC' and price=' ' then code='III';
  if list='DAC' and id='11C' and price='2e' then code='BBB';
  if list='DAC' and id=' ' and price='3e' then code='BBB';
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So I use those if statements to create a new field. But I don't want to have tons of if statements for every variation - I'd rather automate it somehow. Is this possible?&lt;/P&gt;</description>
      <pubDate>Mon, 13 Feb 2017 20:59:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332342#M74788</guid>
      <dc:creator>JediApprentice</dc:creator>
      <dc:date>2017-02-13T20:59:41Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to get rid of cumbersome if logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332346#M74791</link>
      <description>&lt;P&gt;Perhaps you are looking for the SELECT-WHEN statement. An explanation and examples are available in the article &lt;A href="http://blogs.sas.com/content/iml/2016/06/20/select-when-sas-data-step.html" target="_self"&gt;"The SELECT statement in the SAS DATA step."&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Feb 2017 21:05:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332346#M74791</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-02-13T21:05:58Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to get rid of cumbersome if logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332359#M74798</link>
      <description>&lt;P&gt;That depends. Do you have the logic for your mappings somewhere? It has to come from somewhere.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Creating a table you can join to as a lookup.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One refactoring that you should look into, is combining all conditions into one where the outcome is the same.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ie all conditions that map to BBB should have a single IF/THEN condition. In your example you have three different IF statements for this single value.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Feb 2017 21:23:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332359#M74798</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-02-13T21:23:28Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to get rid of cumbersome if logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332360#M74799</link>
      <description>&lt;P&gt;That depends. Do you have the logic for your mappings somewhere? It has to come from somewhere.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Creating a table you can join to as a lookup.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One refactoring that you should look into, is combining all conditions into one where the outcome is the same.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ie all conditions that map to BBB should have a single IF/THEN condition. In your example you have three different IF statements for this single value.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Feb 2017 21:23:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332360#M74799</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-02-13T21:23:30Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to get rid of cumbersome if logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332364#M74802</link>
      <description>&lt;P&gt;You could make a dataset RECODES with all the combinatinos of list/id/price and resulting code.&amp;nbsp; Then use it as a hash lookup table in a subsequent data set, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data recodes;
  input (list id price code) ($char4.);
datalines;
BAC BB8 4a  BBB 
BAC 22C     CCC 
BAC DDD 5b  DDD 
CBA     6c  FFF 
CBA 22A 4b  FFF 
CBA 10B 4b  GGG 
CBA 11A 5f  HHH 
DAC 2CC     III 
DAC 11C 2e  BBB 
DAC     3e  BBB 
run;

data want (drop=rc);
  set have;
  if _n_=1 then do;
    if 0 then set recodes;
    declare hash h (dataset:'recodes');
      h.definekey('list','id','price');
      h.definedata('code');
      h.definedone();&lt;BR /&gt;  end;   /* Added after &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17429"&gt;@LaurieF&lt;/a&gt;'s note */
  rc=h.find();
  if rc^=0 then code=' ';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The variable RC is set to zero with a successful lookup.&amp;nbsp; If it's not a zero then that combinatino of list/id/price was not in the recodes dataset, so code should be set to missing.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2017 03:06:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332364#M74802</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-14T03:06:13Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to get rid of cumbersome if logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332380#M74805</link>
      <description>&lt;P&gt;Yes, I do have a mapping document with all the codes that correspond with each line of logic.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Feb 2017 22:08:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332380#M74805</guid>
      <dc:creator>JediApprentice</dc:creator>
      <dc:date>2017-02-13T22:08:52Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to get rid of cumbersome if logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332386#M74806</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; And, in addition to the elegant HASH approach, a slightly different approach with a user-defined format for a lookup list is how I was taught to do it in the golden olden days before HASH. (If we already had a table with the values we needed for the format, then we used the CNTLIN technique with PROC FORMAT to make the list.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;cynthia&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/7210i981002B6281C6417/image-size/original?v=1.0&amp;amp;px=-1" alt="format_for_lookup_list.png" title="format_for_lookup_list.png" border="0" /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Feb 2017 22:20:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332386#M74806</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2017-02-13T22:20:38Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to get rid of cumbersome if logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332390#M74808</link>
      <description>&lt;P&gt;Or old-fashioned and simpler to code and understand, but probably much slower than a hash table:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE(drop=CODE)
     CODE(index=(A=(LIST ID PRICE )));
  input LIST $ 1-3 ID $ 5-7 PRICE $ 9-10 CODE $ 12-14;
  cards;
BAC BB8 4a BBB 
BAC 22C    CCC 
BAC DDD 5b DDD 
CBA     6c FFF 
CBA 22A 4b FFF 
CBA 10B 4b GGG 
CBA 11A 5f HHH 
DAC 2CC    III 
DAC 11C 2e BBB 
DAC     3e BBB 
run;

data WANT ;
  set HAVE;
  set CODE key=A;
  if _IORC_ = %sysrc(_dsenom) then _ERROR_=0;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Feb 2017 22:34:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332390#M74808</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-02-13T22:34:03Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to get rid of cumbersome if logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332452#M74831</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;…with an 'end;' statement to terminate the 'if _n_ = 1 then do;', of course!&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2017 02:49:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332452#M74831</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-02-14T02:49:22Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to get rid of cumbersome if logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332456#M74832</link>
      <description>Thx.  Done.</description>
      <pubDate>Tue, 14 Feb 2017 03:07:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/332456#M74832</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-14T03:07:00Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to get rid of cumbersome if logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/340542#M77844</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;How would I do a join on a table for a lookup? - I have a table with all of the values and corresponding codes in it.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 18:37:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-get-rid-of-cumbersome-if-logic/m-p/340542#M77844</guid>
      <dc:creator>JediApprentice</dc:creator>
      <dc:date>2017-03-13T18:37:10Z</dc:date>
    </item>
  </channel>
</rss>

