<?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 How to avoid using if as much as I can? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-avoid-using-if-as-much-as-I-can/m-p/723813#M224677</link>
    <description>&lt;P&gt;Hi all SAS Users,&lt;/P&gt;
&lt;P&gt;After reading a couple of discussion from the forum, people normally try to replace if by where or else because &lt;STRONG&gt;if&lt;/STRONG&gt; consumes quite a bit cpu or else.&lt;/P&gt;
&lt;P&gt;I want to ask how to replace if in my case here, and is there any general rule to do so. At least I know in some case we can use where instead of &lt;STRONG&gt;if&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dollar_volume;
	set work.filter_;
	If raw_return ne . then
	raw_return_=abs(raw_return);
	by gviidkey;
		Comment trading volume &amp;lt;share oustanding =&amp;gt;trading volume as missing;
		if cshtrd &amp;gt; cshoc then cshtrd=.;
		Comment trading volume*unadjusted price (converted to USD)&amp;lt; 100$ then set missing ;
		if cshtrd*prccd_abs_ &amp;lt;100 and n(cshtrd,prccd_abs_)=2 /*This code is still under suspicious*/
		then dollar_vol=.; /*This code is still under suspicious*/
		else dollar_vol=cshtrd*prccd_abs_; /*This code is still under suspicious*/
	label 
		dollar_vol=daily dollar volume
		raw_return_= abs of raw_return
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And I am wondering is there any specific document that mentions ways to replace &lt;STRONG&gt;if&lt;/STRONG&gt; ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks and warm regards.&lt;/P&gt;</description>
    <pubDate>Fri, 05 Mar 2021 09:03:25 GMT</pubDate>
    <dc:creator>Phil_NZ</dc:creator>
    <dc:date>2021-03-05T09:03:25Z</dc:date>
    <item>
      <title>How to avoid using if as much as I can?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-avoid-using-if-as-much-as-I-can/m-p/723813#M224677</link>
      <description>&lt;P&gt;Hi all SAS Users,&lt;/P&gt;
&lt;P&gt;After reading a couple of discussion from the forum, people normally try to replace if by where or else because &lt;STRONG&gt;if&lt;/STRONG&gt; consumes quite a bit cpu or else.&lt;/P&gt;
&lt;P&gt;I want to ask how to replace if in my case here, and is there any general rule to do so. At least I know in some case we can use where instead of &lt;STRONG&gt;if&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dollar_volume;
	set work.filter_;
	If raw_return ne . then
	raw_return_=abs(raw_return);
	by gviidkey;
		Comment trading volume &amp;lt;share oustanding =&amp;gt;trading volume as missing;
		if cshtrd &amp;gt; cshoc then cshtrd=.;
		Comment trading volume*unadjusted price (converted to USD)&amp;lt; 100$ then set missing ;
		if cshtrd*prccd_abs_ &amp;lt;100 and n(cshtrd,prccd_abs_)=2 /*This code is still under suspicious*/
		then dollar_vol=.; /*This code is still under suspicious*/
		else dollar_vol=cshtrd*prccd_abs_; /*This code is still under suspicious*/
	label 
		dollar_vol=daily dollar volume
		raw_return_= abs of raw_return
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And I am wondering is there any specific document that mentions ways to replace &lt;STRONG&gt;if&lt;/STRONG&gt; ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks and warm regards.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 09:03:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-avoid-using-if-as-much-as-I-can/m-p/723813#M224677</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-03-05T09:03:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to avoid using if as much as I can?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-avoid-using-if-as-much-as-I-can/m-p/723818#M224682</link>
      <description>&lt;P&gt;&lt;STRONG&gt;WHERE&lt;/STRONG&gt; is used &lt;U&gt;only&lt;/U&gt; to select observations from the &lt;STRONG&gt;input&lt;/STRONG&gt; and skip the not required.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;IF&lt;/STRONG&gt; can be used to select observations for &lt;STRONG&gt;output&lt;/STRONG&gt; but in most cases to select observations for more specific programing.&lt;/P&gt;
&lt;P&gt;In some cases it may be easier to code and use: &lt;STRONG&gt;SELECT&amp;nbsp;&lt;/STRONG&gt;statement with &lt;STRONG&gt;WHEN.&amp;nbsp;&lt;/STRONG&gt; Check documentation for examples.&lt;/P&gt;
&lt;P&gt;Using &lt;STRONG&gt;SQL&lt;/STRONG&gt; you have &lt;STRONG&gt;CASE&lt;/STRONG&gt; clause - similar to the sas datastep &lt;STRONG&gt;SELECT&lt;/STRONG&gt;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 09:20:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-avoid-using-if-as-much-as-I-can/m-p/723818#M224682</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-03-05T09:20:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to avoid using if as much as I can?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-avoid-using-if-as-much-as-I-can/m-p/723869#M224707</link>
      <description>&lt;P&gt;Consider this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _NULL_;
  X=.;  Y=.;  Z=X*y; putlog "NOTE: " _ALL_;
  X=2;  Y=.;  Z=X*y; putlog "NOTE: " _ALL_;
  X=.;  Y=3;  Z=X*y; putlog "NOTE: " _ALL_;
  stop;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Because of the way numbers multiply with missing values, I believe there is no need to use the n() function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also you could avoid multiplying twice, consider the difference in these cases:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data;
/*Case 1*/
if cshtrd*prccd_abs_ &amp;lt;100 and n(cshtrd,prccd_abs_)=2 /* 1 multiplication */
  then dollar_vol=.;
  else dollar_vol=cshtrd*prccd_abs_; /* 2nd multiplication, conditional on if*/

/*Case 2*/
dollar_vol=cshtrd*prccd_abs_; /* only 1 multiplication */
if dollar_vol &amp;lt;100 
  then dollar_vol=.; 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Mar 2021 14:19:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-avoid-using-if-as-much-as-I-can/m-p/723869#M224707</guid>
      <dc:creator>PhilC</dc:creator>
      <dc:date>2021-03-05T14:19:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to avoid using if as much as I can?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-avoid-using-if-as-much-as-I-can/m-p/723871#M224708</link>
      <description>&lt;P&gt;You are also you leaving your self open for misinterpretation since you have not prepared example data.&amp;nbsp; We cannot help you as well as we might if you withhold the structure of the dataset.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 14:18:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-avoid-using-if-as-much-as-I-can/m-p/723871#M224708</guid>
      <dc:creator>PhilC</dc:creator>
      <dc:date>2021-03-05T14:18:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to avoid using if as much as I can?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-avoid-using-if-as-much-as-I-can/m-p/723872#M224709</link>
      <description>&lt;P&gt;WHERE selects observations for processing; observations that do not match the condition never make it into the data step.&lt;/P&gt;
&lt;P&gt;IF works while an observation is processed.&lt;/P&gt;
&lt;P&gt;A Subsetting IF terminates processing of the current observation, without performing the implicit OUTPUT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your IFs are all non-subsetting, so they can't be replaced by a WHERE.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 14:14:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-avoid-using-if-as-much-as-I-can/m-p/723872#M224709</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-03-05T14:14:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to avoid using if as much as I can?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-avoid-using-if-as-much-as-I-can/m-p/723883#M224716</link>
      <description>&lt;P&gt;A thought, I don't know this -- but, is it that the BY statement implicitly assigns values, and also performs "IF" operations that if you did not use the BY statement you would increase performance on large sets?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only two uses of the BY statement I can see here is that it 1) asserts a sort on GVIIDKEY and 2) conveys the properties of being sorted by GVIIDKEY in the metadata for DOLLAR_VOLUME.&amp;nbsp; If one can assert that the datasets is already sorted prior to the run of the data step, would it be better (faster) to not use the BY statement, but to relay the property of being sorted, explicitly, via the SORTEDBY dataset option on DOLLAR_VOLUME?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dollar_volume (sortedby=gviidkey);
	set work.filter_;
	If raw_return ne . then
	raw_return_=abs(raw_return);
/*	by gviidkey; */
		Comment trading volume &amp;lt;share oustanding =&amp;gt;trading volume as missing;
		if cshtrd &amp;gt; cshoc then cshtrd=.;
		Comment trading volume*unadjusted price (converted to USD)&amp;lt; 100$ then set missing ;
		if cshtrd*prccd_abs_ &amp;lt;100 and n(cshtrd,prccd_abs_)=2 /*This code is still under suspicious*/
		then dollar_vol=.; /*This code is still under suspicious*/
		else dollar_vol=cshtrd*prccd_abs_; /*This code is still under suspicious*/
	label 
		dollar_vol=daily dollar volume
		raw_return_= abs of raw_return
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 15:12:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-avoid-using-if-as-much-as-I-can/m-p/723883#M224716</guid>
      <dc:creator>PhilC</dc:creator>
      <dc:date>2021-03-05T15:12:20Z</dc:date>
    </item>
  </channel>
</rss>

