<?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: A more efficent way of achieving same result as &amp;quot;calculated&amp;quot; proc SQL in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/A-more-efficent-way-of-achieving-same-result-as-quot-calculated/m-p/80587#M23211</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Using max(calculated carry(n-1)-avail(n), 0) as carry(n) length=8 should already improve efficiency a lot but it may require nesting if calculated is not supported in the aggregate function.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I suppose you can kill calculated by nesting the 50 calculations but if I recall, sql only supports 32 nesting level and it would be quite dense to write the syntax, even with the use of macros. Instead, I propose the data step approach below.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you are not bound to SQL, data step will allow you to save from typing calculated everywhere&lt;/P&gt;&lt;P&gt;%macro simpleloop;&lt;/P&gt;&lt;P&gt;data w2lcrdg;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set &amp;amp;syslast;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; length&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %do i=1 %to 50;&amp;nbsp;&amp;nbsp; carry&amp;amp;i 8.&amp;nbsp; %end;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %do i=1 %to 50;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; carry&amp;amp;i = max(0, carry%eval(&amp;amp;i-1) - avail&amp;amp;i);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drop %do i=1 %to 50; avail&amp;amp;i&amp;nbsp; %end;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Vincent&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 16 Jul 2013 14:35:32 GMT</pubDate>
    <dc:creator>Vince28_Statcan</dc:creator>
    <dc:date>2013-07-16T14:35:32Z</dc:date>
    <item>
      <title>A more efficent way of achieving same result as "calculated" proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/A-more-efficent-way-of-achieving-same-result-as-quot-calculated/m-p/80584#M23208</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I currently am creating a table using proc SQL. The table has 50 columns representing days 1 - 50. All the columns use the calcualted function to get the following logic:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Result of column 2 is based on calculation in column 1&lt;/P&gt;&lt;P&gt;Result of column 3 is based on calculation in column 2&lt;/P&gt;&lt;P&gt;Result of column 4 is based on calculation in column 3&lt;/P&gt;&lt;P&gt;Result of column 5 is based on calculation in column 4&lt;/P&gt;&lt;P&gt;etc&lt;/P&gt;&lt;P&gt;Result of column 50 is based on calculation in column 49&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is incredibly slow to run and no doubt in efficient.&amp;nbsp; Is there a better way of doing this?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Tom&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 16 Jul 2013 13:25:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/A-more-efficent-way-of-achieving-same-result-as-quot-calculated/m-p/80584#M23208</guid>
      <dc:creator>qwererty</dc:creator>
      <dc:date>2013-07-16T13:25:54Z</dc:date>
    </item>
    <item>
      <title>Re: A more efficent way of achieving same result as "calculated" proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/A-more-efficent-way-of-achieving-same-result-as-quot-calculated/m-p/80585#M23209</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Tom,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Tell us more, and perhaps provide an example.&amp;nbsp; You may, or may not, be able to improve it depending on what you are trying to do.&amp;nbsp; If your calculation is a simple arithmetic operation, it can readily be improved.&amp;nbsp; If it involves summary statistics, it may be much more difficult.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Doc Muhlbaier&lt;/P&gt;&lt;P&gt;Duke&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 16 Jul 2013 13:33:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/A-more-efficent-way-of-achieving-same-result-as-quot-calculated/m-p/80585#M23209</guid>
      <dc:creator>Doc_Duke</dc:creator>
      <dc:date>2013-07-16T13:33:32Z</dc:date>
    </item>
    <item>
      <title>Re: A more efficent way of achieving same result as "calculated" proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/A-more-efficent-way-of-achieving-same-result-as-quot-calculated/m-p/80586#M23210</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A extract of code as follows (all the missing lines contain exactly the same logic as pattern below):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Proc SQL;&lt;/P&gt;&lt;P&gt;create table work.W2LCRDG as&lt;/P&gt;&lt;P&gt;select&lt;/P&gt;&lt;P&gt;domai,&lt;/P&gt;&lt;P&gt;carry0,&lt;/P&gt;&lt;P&gt;(Case whe carry0 - Avail1 &amp;lt; 0 the 0 else carry0 - Avail1 end) as carry1 legth = 8,&lt;/P&gt;&lt;P&gt;(Case whe calculated carry1 - Avail2 &amp;lt; 0 the 0 else calculated carry1 - Avail2 end) as carry2 legth = 8,&lt;/P&gt;&lt;P&gt;(Case whe calculated carry2 - Avail3 &amp;lt; 0 the 0 else calculated carry2 - Avail3 end) as carry3 legth = 8,&lt;/P&gt;&lt;P&gt;(Case whe calculated carry3 - Avail4 &amp;lt; 0 the 0 else calculated carry3 - Avail4 end) as carry3 legth = 8&lt;/P&gt;&lt;P&gt;etc&lt;/P&gt;&lt;P&gt;from &amp;amp;SYSLAST;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Tom&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 16 Jul 2013 13:42:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/A-more-efficent-way-of-achieving-same-result-as-quot-calculated/m-p/80586#M23210</guid>
      <dc:creator>qwererty</dc:creator>
      <dc:date>2013-07-16T13:42:38Z</dc:date>
    </item>
    <item>
      <title>Re: A more efficent way of achieving same result as "calculated" proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/A-more-efficent-way-of-achieving-same-result-as-quot-calculated/m-p/80587#M23211</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Using max(calculated carry(n-1)-avail(n), 0) as carry(n) length=8 should already improve efficiency a lot but it may require nesting if calculated is not supported in the aggregate function.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I suppose you can kill calculated by nesting the 50 calculations but if I recall, sql only supports 32 nesting level and it would be quite dense to write the syntax, even with the use of macros. Instead, I propose the data step approach below.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you are not bound to SQL, data step will allow you to save from typing calculated everywhere&lt;/P&gt;&lt;P&gt;%macro simpleloop;&lt;/P&gt;&lt;P&gt;data w2lcrdg;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set &amp;amp;syslast;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; length&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %do i=1 %to 50;&amp;nbsp;&amp;nbsp; carry&amp;amp;i 8.&amp;nbsp; %end;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %do i=1 %to 50;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; carry&amp;amp;i = max(0, carry%eval(&amp;amp;i-1) - avail&amp;amp;i);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drop %do i=1 %to 50; avail&amp;amp;i&amp;nbsp; %end;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Vincent&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 16 Jul 2013 14:35:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/A-more-efficent-way-of-achieving-same-result-as-quot-calculated/m-p/80587#M23211</guid>
      <dc:creator>Vince28_Statcan</dc:creator>
      <dc:date>2013-07-16T14:35:32Z</dc:date>
    </item>
    <item>
      <title>Re: A more efficent way of achieving same result as "calculated" proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/A-more-efficent-way-of-achieving-same-result-as-quot-calculated/m-p/80588#M23212</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That looks like the job for an array in a data step.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Same solution as above, just no macro code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data w2lcrdg;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set &amp;amp;syslast;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; length carry1-carry50 8.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; array&amp;nbsp; carry (50) carry1-carry50&amp;nbsp;&amp;nbsp; ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; array avail(50) avail1-avail50;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do i=1 to 50;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; carry(i) = max(0, carry(i-1) - avail(i));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drop avail:&amp;nbsp; ;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 16 Jul 2013 14:49:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/A-more-efficent-way-of-achieving-same-result-as-quot-calculated/m-p/80588#M23212</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2013-07-16T14:49:29Z</dc:date>
    </item>
  </channel>
</rss>

