<?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: Making If statement more efficient. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396306#M95667</link>
    <description>&lt;P&gt;Hard to tell what the logic is without either specifications or data, but couldn't you turn this loop around.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=3 %to 12 %by 3; 
if datepart(date)&amp;gt;=intnx('month', "&amp;amp;x_date"d,%eval(-&amp;amp;i+1), 'B')
 and datepart(data_danych)&amp;lt;=intnx('month', "&amp;amp;x_date"d,0,'E') then do;
  if  kod_3 = '1001' then do;
    var1_&amp;amp;i.m=round(amount*exch_rete,0.01);
  end;
...
end;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;It looks like you are dividing things into quarters and updating a series of quarter based varaibles.&lt;/P&gt;
&lt;P&gt;Couldn't you just calculate the quarter from the date and then generate the index?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 15 Sep 2017 13:03:21 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-09-15T13:03:21Z</dc:date>
    <item>
      <title>Making If statement more efficient.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396178#M95616</link>
      <description>&lt;P&gt;Hi, is there a way to make if statement more efficient? I ve got about 80 mln obs with over 80 if statments in loop, processing this dataset takes couple of hours? Belowe you will find scrap of the code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%Macro test(x_date)
data want;
set have;
		%do i=3 %to 12 %by 3; 
			if datepart(date)&amp;gt;=intnx('month', "&amp;amp;x_date"d,%eval(-&amp;amp;i+1), 'B') and datepart(data_danych)&amp;lt;=intnx('month', "&amp;amp;x_date"d,0,'E') and kod_3 = '1001' then do;
		  var1_&amp;amp;i.m=round(amount*exch_rete,0.01);
			end;

			else if datepart(date)&amp;gt;=intnx('month', "&amp;amp;x_date"d,%eval(-&amp;amp;i+1), 'B') and datepart(data_danych)&amp;lt;=intnx('month', "&amp;amp;x_date"d,0,'E') and kod_3 = '1201' then do;
			var2_w_UP_&amp;amp;i.m=round(amount*exch_rate,0.01);
			end;

			else if datepart(date)&amp;gt;=intnx('month', "&amp;amp;x_date"d,%eval(-&amp;amp;i+1), 'B') and datepart(data_danych)&amp;lt;=intnx('month', "&amp;amp;x_date"d,0,'E') and kod_3 in ('1041','1241') then do;
			 var3_&amp;amp;i.m=round(amount*exch_rate,0.01);
			end;

........
%end;

run;
%mend;
test(31AUG2017);&lt;/CODE&gt;&lt;/PRE&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;Thank you.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 07:06:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396178#M95616</guid>
      <dc:creator>Matt3</dc:creator>
      <dc:date>2017-09-15T07:06:46Z</dc:date>
    </item>
    <item>
      <title>Re: Making If statement more efficient.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396182#M95619</link>
      <description>&lt;P&gt;Why do you want to do this in a macro? Why not do it with just the data step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You say you have 80 different if statements, but are the two first conditions in your if statements always the same like they are below, so only the third condition differ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 07:21:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396182#M95619</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-09-15T07:21:32Z</dc:date>
    </item>
    <item>
      <title>Re: Making If statement more efficient.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396186#M95620</link>
      <description>&lt;P&gt;All conditions are differ but variables are always the same, actualy I do this in macro, due to &amp;amp;i macro variable.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 07:36:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396186#M95620</guid>
      <dc:creator>Matt3</dc:creator>
      <dc:date>2017-09-15T07:36:07Z</dc:date>
    </item>
    <item>
      <title>Re: Making If statement more efficient.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396189#M95622</link>
      <description>&lt;P&gt;Try this.....&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data KOD_3_VALUES;
  infile datalines;
  input cond $30.;
datalines;
='1001'
='1201'
in ('1041','1241')
;
run;


data _null_;
  set KOD_3_VALUES end=obs;
  call symput(compress("cond"||_n_), cond);
  if obs then call symput('totobs',_n_);
run;

%macro TestMyConditions;
  %do i=1 %to &amp;amp;totobs.;
    %if %eval(&amp;amp;i=1) %then %put if kod_3 %bquote(&amp;amp;&amp;amp;cond&amp;amp;i);
       %else  %if %eval(&amp;amp;i&amp;gt;1) %then %put if kod_3 %bquote(&amp;amp;&amp;amp;cond&amp;amp;i);
  %end;
%mend;

%TestMyConditions;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 07:39:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396189#M95622</guid>
      <dc:creator>ShiroAmada</dc:creator>
      <dc:date>2017-09-15T07:39:02Z</dc:date>
    </item>
    <item>
      <title>Re: Making If statement more efficient.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396190#M95623</link>
      <description>&lt;P&gt;In my opinion, you should not use macro programming if it is not necessary &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have the same pattern in all your if statements, where the two first conditions are the same, there is no need to test these two conditions in every if statement. Instead do something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=3 %to 12 %by 3; 
	if datepart(date)&amp;gt;=intnx('month', "&amp;amp;x_date"d,%eval(-&amp;amp;i+1), 'B') and datepart(data_danych)&amp;lt;=intnx('month', "&amp;amp;x_date"d,0,'E') then do;
		if kod_3 = '1001' then var1_&amp;amp;i.m=round(amount*exch_rete,0.01);
		else if kod_3 = '1201' then var2_w_UP_&amp;amp;i.m=round(amount*exch_rate,0.01);
		else if kod_3 in ('1041','1241') then var3_&amp;amp;i.m=round(amount*exch_rate,0.01);
	end;
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Sep 2017 07:38:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396190#M95623</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-09-15T07:38:28Z</dc:date>
    </item>
    <item>
      <title>Re: Making If statement more efficient.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396192#M95625</link>
      <description>&lt;P&gt;And this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if kod_3 = '1001' then var1_&amp;amp;i.m=round(amount*exch_rete,0.01);
else if kod_3 = '1201' then var2_w_UP_&amp;amp;i.m=round(amount*exch_rate,0.01);
else if kod_3 in ('1041','1241') then var3_&amp;amp;i.m=round(amount*exch_rate,0.01);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;looks much better when coded as&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select (kod_3);
  when ('1001') var1_&amp;amp;i.m=round(amount*exch_rete,0.01);
  when ('1201') var2_w_UP_&amp;amp;i.m=round(amount*exch_rate,0.01);
  when ('1041','1241') var3_&amp;amp;i.m=round(amount*exch_rate,0.01);
  otherwise;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and might even perform better.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 07:57:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396192#M95625</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-09-15T07:57:06Z</dc:date>
    </item>
    <item>
      <title>Re: Making If statement more efficient.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396202#M95628</link>
      <description>&lt;P&gt;It would help a lot if you actually showed some test data (in the form of a datastep using the code window {i}). &amp;nbsp;It looks to me like using arrays, i.e. a reference to a groups of variables, would be far simpler than doing lots of if statements. &amp;nbsp;The code you have shown doesn't really show any differences?? &amp;nbsp;One other option, which will likely make your life a lot easier is to normalise the data, i.e. have the various variables going down the page rather than across, you would need a lot of loops and arrays that way. &amp;nbsp;Again, post test data so we can work on it, and show what you want out. &amp;nbsp;Otherwise we are just guessing.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 08:12:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396202#M95628</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-09-15T08:12:50Z</dc:date>
    </item>
    <item>
      <title>Re: Making If statement more efficient.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396281#M95657</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/127617"&gt;@Matt3&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;IF / THEN runs pretty quickly. &amp;nbsp;The CPU hog here is the number of functions you are calling. &amp;nbsp;You can reduce them considerably by making a few changes;&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;dp = datepart(date);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now you never need another DATEPART function in the rest of your code ... just refer to the variable DP.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%do i=3 %to 12 %by 3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;range_begins = intnx('month', "&amp;amp;x_date"d, %eval(-&amp;amp;i+1), 'B');&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;range_ends = intnx('month', "&amp;amp;x_date"d, 0, 'E');&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;...&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Again, you don't need any additional INTNX functions. &amp;nbsp;Just refer to the variables RANGE_BEGINS and RANGE_ENDS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A much smaller savings&amp;nbsp;could then be achieved by nesting the IF / THEN conditions. &amp;nbsp;For example, you could try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if range_begins &amp;lt;= dp &amp;lt;= range_ends then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if kod_3 = '1001' then ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;else if kod_3 = '1201' then ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;else etc. ...&lt;/P&gt;
&lt;P&gt;end;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 11:38:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396281#M95657</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-09-15T11:38:42Z</dc:date>
    </item>
    <item>
      <title>Re: Making If statement more efficient.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396306#M95667</link>
      <description>&lt;P&gt;Hard to tell what the logic is without either specifications or data, but couldn't you turn this loop around.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=3 %to 12 %by 3; 
if datepart(date)&amp;gt;=intnx('month', "&amp;amp;x_date"d,%eval(-&amp;amp;i+1), 'B')
 and datepart(data_danych)&amp;lt;=intnx('month', "&amp;amp;x_date"d,0,'E') then do;
  if  kod_3 = '1001' then do;
    var1_&amp;amp;i.m=round(amount*exch_rete,0.01);
  end;
...
end;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;It looks like you are dividing things into quarters and updating a series of quarter based varaibles.&lt;/P&gt;
&lt;P&gt;Couldn't you just calculate the quarter from the date and then generate the index?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 13:03:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396306#M95667</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-09-15T13:03:21Z</dc:date>
    </item>
    <item>
      <title>Re: Making If statement more efficient.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396354#M95684</link>
      <description>&lt;P&gt;Don't call functions to recalculate on every observations values that are constants.&lt;/P&gt;
&lt;P&gt;Don't call functions you don't need.&lt;/P&gt;
&lt;P&gt;So you would change something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=3 %to 12 %by 3; 
if datepart(date)&amp;gt;=intnx('month', "&amp;amp;x_date"d,%eval(-&amp;amp;i+1), 'B')
 and datepart(data_danych)&amp;lt;=intnx('month', "&amp;amp;x_date"d,0,'E') then do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=3 %to 12 %by 3; 
 %let lower=%sysfunc(intnx(dtmonth,"&amp;amp;x_date:00:00"dt,-&amp;amp;i+1,b));
 %let upper=%sysfunc(intnx(dtmonth,"&amp;amp;x_date:00:00"dt,0,e));
if date &amp;gt;= &amp;amp;lower and date_danych &amp;lt;= &amp;amp;upper then do;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So calculate the constants that are based on macro variables since they are know before the data step is even compiled.&lt;/P&gt;
&lt;P&gt;Also calculate the constants as datetime values instead of date values so that you no longer need to call the DATEPART() function multiple times on each observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2017 14:43:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-If-statement-more-efficient/m-p/396354#M95684</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-09-15T14:43:54Z</dc:date>
    </item>
  </channel>
</rss>

