<?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: Efficient code for searching through a list in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Efficient-code-for-searching-through-a-list/m-p/350012#M81311</link>
    <description>&lt;P&gt;A couple of notes on things to consider. &amp;nbsp;Your original program generated 1800 IF/THEN statements. &amp;nbsp;It would run faster if you added the word ELSE to 1799 of those statements. &amp;nbsp;That could be done by adding this line after the %do i= statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if &amp;amp;i &amp;gt; 1 or &amp;amp;j &amp;gt; 1 %then else;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And the hashing approach is a good one. &amp;nbsp;If I knew how to program it, I would have. &amp;nbsp;It can be speeded up by using the trick that I used in the format solution, getting the loop to end earlier once a match is found:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;do _n_=1 to dim(a_dx) &lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;until (flag=1)&lt;/FONT&gt;&lt;/STRONG&gt;;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 14 Apr 2017 11:08:20 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-04-14T11:08:20Z</dc:date>
    <item>
      <title>Efficient code for searching through a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-code-for-searching-through-a-list/m-p/349898#M81272</link>
      <description>&lt;P&gt;I have a SAS database with a single variable and 450 observations, each one corresponding to an ICD-9 diagnosis code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also have a large healthcare claims database in which each observation is a claim with up to 4 diagnosis code fields (dx1-dx4).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have written code that sets all the 450 observations into global variables. &amp;nbsp;In a macro, I set up a 4-element array consisting of dx1-dx4, and then use a do loop to set an indicator to 1 if any of the elements of the array match the 450 diagnosis codes. &amp;nbsp;If 1, then the observation will output. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code, however, is taking an extremely long time &amp;nbsp;to run and I am wondering if there is a more efficient alternative. &amp;nbsp;Here it is below. Note that the 450 global variables are called dxcode1, dxcode2...dxcode450.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let code = dxcode;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro search;&lt;/P&gt;
&lt;P&gt;data out;&lt;/P&gt;
&lt;P&gt;set claims;&lt;/P&gt;
&lt;P&gt;array a_dx dx1-dx4;&lt;/P&gt;
&lt;P&gt;flag = 0;&lt;/P&gt;
&lt;P&gt;%do j = 1 %to 450;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%do i = 1 %to 4;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if a_dx(&amp;amp;i) = "&amp;amp;&amp;amp;&amp;amp;code.&amp;amp;j." then flag = 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; %end;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;if flag &amp;gt; 0 then output;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;%mend search;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 22:40:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-code-for-searching-through-a-list/m-p/349898#M81272</guid>
      <dc:creator>chuakp</dc:creator>
      <dc:date>2017-04-13T22:40:25Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient code for searching through a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-code-for-searching-through-a-list/m-p/349903#M81277</link>
      <description>&lt;P&gt;I think that something like the following will run a lot quicker:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data claims;
  input dx1-dx4;
  cards;
1 2 3 4
1 3 4 5
1 4 5 6
1 7 8 9
;

%macro search(dxcode);
  data out;
    set claims;
    array a_dx dx1-dx4;
    flag = 0;
    if &amp;amp;dxcode in a_dx then do;
      flag=1;
      output;
    end;
  run;
run;
%mend search;

%search(5)
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 22:55:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-code-for-searching-through-a-list/m-p/349903#M81277</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-04-13T22:55:36Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient code for searching through a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-code-for-searching-through-a-list/m-p/349911#M81282</link>
      <description>&lt;P&gt;Or load the 450 data set into a temporary array and a single loop with no macro.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/f052b5223fecca066b1f" target="_blank"&gt;https://gist.github.com/statgeek/f052b5223fecca066b1f&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 23:59:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-code-for-searching-through-a-list/m-p/349911#M81282</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-04-13T23:59:10Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient code for searching through a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-code-for-searching-through-a-list/m-p/349931#M81294</link>
      <description>&lt;P&gt;Here's an alternative that skips creating macro variables. &amp;nbsp;I'll assume that the 450 observations contained a variable named DXCODE, but the code can easily be modified to use the proper names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data codelist2;&lt;/P&gt;
&lt;P&gt;set codelist end=done;&lt;/P&gt;
&lt;P&gt;fmtname='$found';&lt;/P&gt;
&lt;P&gt;start = dxcode;&lt;/P&gt;
&lt;P&gt;label='1';&lt;/P&gt;
&lt;P&gt;output;&lt;/P&gt;
&lt;P&gt;if done;&lt;/P&gt;
&lt;P&gt;hlo='O';&lt;/P&gt;
&lt;P&gt;label='0';&lt;/P&gt;
&lt;P&gt;output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc format cntlin=codelist2;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This gives you a format that translates each of the 450 codes into "1" and anything else into "0". &amp;nbsp;To keep the code simple, I'll make FLAG a character variable. &amp;nbsp;Not too difficult to change it to numeric though.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once you have the format, the rest is pretty straightforward.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array dx {4};&lt;/P&gt;
&lt;P&gt;do i=1 to 4 until (flag='1');&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;flag = put(dx{i}, $found.);&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;if flag='1';&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2017 01:14:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-code-for-searching-through-a-list/m-p/349931#M81294</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-04-14T01:14:12Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient code for searching through a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-code-for-searching-through-a-list/m-p/349934#M81295</link>
      <description>&lt;P&gt;I think you can try to use SQL for this problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Suppose dataset "lookup" is the one with&amp;nbsp;&lt;SPAN&gt;450 observations, and dataset "healthcare" is the large healthcare claims database you have.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;lookup has a variable called var1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;healthcare has 4 variables called dx1-dx4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then you can create a dataset "selected" outputing all selected observations from healthcare. You might need to replace with your own dataset name or variable names.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc sql;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;create table selected as&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;select *&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;from&amp;nbsp;&lt;SPAN&gt;healthcare&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;where dx1 = any (select &lt;SPAN&gt;var1&lt;/SPAN&gt; from&amp;nbsp;&lt;SPAN&gt;lookup) or&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dx2 = any (select var1 from&amp;nbsp;lookup)&amp;nbsp;or&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dx3 = any (select var1 from&amp;nbsp;lookup) or&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dx4&amp;nbsp;= any (select var1 from&amp;nbsp;lookup)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2017 01:34:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-code-for-searching-through-a-list/m-p/349934#M81295</guid>
      <dc:creator>CiCi</dc:creator>
      <dc:date>2017-04-14T01:34:06Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient code for searching through a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-code-for-searching-through-a-list/m-p/349959#M81299</link>
      <description>&lt;P&gt;Hi, Here is one easy and simple way. I hope I have understood your requirement:-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;/*Dataset_450 has the variable with 450 obs and claims has variables dx1-dx4*/&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;if (_n_ = 1) then do;&lt;BR /&gt;if 0 then set dataset_450;&lt;BR /&gt;declare hash myhash(dataset: "dataset_450(keep=variable_450");&lt;BR /&gt;rc = myhash.definekey('variable_450');&lt;BR /&gt;myhash.definedone();&lt;BR /&gt;end;&lt;BR /&gt;set claims;&lt;BR /&gt;array a_dx(*) dx1-dx4;&lt;BR /&gt;do _n_=1 to dim(a_dx);&lt;BR /&gt;temp=a_dx(_n_);&lt;BR /&gt;if myhash.check(key:temp) = 0 then flag=1;&lt;BR /&gt;end;&lt;BR /&gt;if flag=1 then output;&lt;BR /&gt;drop temp;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Naveen Srinivasan&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2017 05:33:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-code-for-searching-through-a-list/m-p/349959#M81299</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-04-14T05:33:33Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient code for searching through a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-code-for-searching-through-a-list/m-p/350012#M81311</link>
      <description>&lt;P&gt;A couple of notes on things to consider. &amp;nbsp;Your original program generated 1800 IF/THEN statements. &amp;nbsp;It would run faster if you added the word ELSE to 1799 of those statements. &amp;nbsp;That could be done by adding this line after the %do i= statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if &amp;amp;i &amp;gt; 1 or &amp;amp;j &amp;gt; 1 %then else;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And the hashing approach is a good one. &amp;nbsp;If I knew how to program it, I would have. &amp;nbsp;It can be speeded up by using the trick that I used in the format solution, getting the loop to end earlier once a match is found:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;do _n_=1 to dim(a_dx) &lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;until (flag=1)&lt;/FONT&gt;&lt;/STRONG&gt;;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2017 11:08:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-code-for-searching-through-a-list/m-p/350012#M81311</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-04-14T11:08:20Z</dc:date>
    </item>
  </channel>
</rss>

