<?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: Calculated values in PROC SQL in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Calculated-values-in-PROC-SQL/m-p/129631#M35340</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I agree with you. It's only a copy of the value. But yet, since I want to allow for two possible syntaxes for my parameter, I would have liked to use a single code for managing the two syntaxes. Now, if I want to be robust, I will have to use&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (c) + 0&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;just to make sure, but this does not seem to me very elegant, more a workaround.&lt;/P&gt;&lt;DIV id="bikatoc" style="display: none;"&gt;&lt;A class="control"&gt;close&lt;/A&gt;&lt;DIV class="toc"&gt;&lt;UL&gt;&lt;/UL&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 04 May 2013 00:42:16 GMT</pubDate>
    <dc:creator>bissjoe</dc:creator>
    <dc:date>2013-05-04T00:42:16Z</dc:date>
    <item>
      <title>Calculated values in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculated-values-in-PROC-SQL/m-p/129629#M35338</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a strange behavior from SAS.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Consider the three following steps:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data F1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do nom=1 to 10;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = ceil(ranuni(123)*1000);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; c = ( ranuni(123) &amp;gt; 0.4 );&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table F2 as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; nom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (c) as c2,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; case&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when calculated c2 &amp;gt; 0 then x**2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; as x2&lt;/P&gt;&lt;P&gt;&amp;nbsp; from F1;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table F3 as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; nom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (c)+0 as c3,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; case&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when calculated c3 &amp;gt; 0 then x**2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; as x3&lt;/P&gt;&lt;P&gt;&amp;nbsp; from F1;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The step creating F2 generates the error message:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ERROR: The following columns were not found as CALCULATED references in the immediate query: c2.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But the step creating F3 does not generate an error.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My example was simplified. In the real context, variable c would be replaced by a parameter's value, which could be a Boolean expression or a numeric variable. In either case, any value not being zero nor null would trigger a TRUE, otherwise a FALSE. This in principle works with a Boolean condition (which returns 1 for true, and 0 for false), except that PROC SQL does not c2 as a CALCULATED value. In fact, the alternative code&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table F2 as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; nom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (c) as c2,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; case&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when c2 &amp;gt; 0 then x**2&amp;nbsp; /* CALCULATED is not specified! */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; as x2&lt;/P&gt;&lt;P&gt;&amp;nbsp; from F1;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;works. The problem is, I want a common codebase for both cases for my parameter.&lt;/P&gt;&lt;DIV id="bikatoc" style="display: none;"&gt;&lt;A class="control"&gt;close&lt;/A&gt;&lt;DIV class="toc"&gt;&lt;UL&gt;&lt;/UL&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 May 2013 22:42:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculated-values-in-PROC-SQL/m-p/129629#M35338</guid>
      <dc:creator>bissjoe</dc:creator>
      <dc:date>2013-05-03T22:42:41Z</dc:date>
    </item>
    <item>
      <title>Re: Calculated values in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculated-values-in-PROC-SQL/m-p/129630#M35339</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi.&lt;/P&gt;&lt;P&gt;I think the issues is that in the first instance - code below, you are not CALCULATING anything, you are just saving &lt;EM&gt;c&lt;/EM&gt; &lt;EM&gt;as&lt;/EM&gt; &lt;EM&gt;c2&lt;/EM&gt;....but if you were to use&amp;nbsp; &lt;EM&gt;sum(c) as c2&lt;/EM&gt;, or whatever other function...then the newly created &lt;EM&gt;c2&lt;/EM&gt; variable would actually hold a &lt;EM&gt;calculated&lt;/EM&gt; value.Do you know what I mean?&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table F2 as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; nom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;STRONG&gt;(c) as c2,&amp;nbsp; &lt;BR /&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; case&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when&lt;STRONG&gt; calculated c2&lt;/STRONG&gt; &amp;gt; 0 then x**2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; as x2&lt;/P&gt;&lt;P&gt;&amp;nbsp; from F1;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I hope this helps.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 03 May 2013 23:11:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculated-values-in-PROC-SQL/m-p/129630#M35339</guid>
      <dc:creator>AncaTilea</dc:creator>
      <dc:date>2013-05-03T23:11:42Z</dc:date>
    </item>
    <item>
      <title>Re: Calculated values in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculated-values-in-PROC-SQL/m-p/129631#M35340</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I agree with you. It's only a copy of the value. But yet, since I want to allow for two possible syntaxes for my parameter, I would have liked to use a single code for managing the two syntaxes. Now, if I want to be robust, I will have to use&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (c) + 0&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;just to make sure, but this does not seem to me very elegant, more a workaround.&lt;/P&gt;&lt;DIV id="bikatoc" style="display: none;"&gt;&lt;A class="control"&gt;close&lt;/A&gt;&lt;DIV class="toc"&gt;&lt;UL&gt;&lt;/UL&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 04 May 2013 00:42:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculated-values-in-PROC-SQL/m-p/129631#M35340</guid>
      <dc:creator>bissjoe</dc:creator>
      <dc:date>2013-05-04T00:42:16Z</dc:date>
    </item>
    <item>
      <title>Re: Calculated values in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculated-values-in-PROC-SQL/m-p/129632#M35341</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The only other alternative I can see is to repeat the expression for calculating c2 twice in your query :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table F2 as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; nom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;STRONG&gt;&amp;lt;Expression&amp;gt; as c2,&amp;nbsp; &lt;BR /&gt;&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; case&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when&lt;STRONG&gt; (&amp;lt;Expression&amp;gt;)&lt;/STRONG&gt; &amp;gt; 0 then x**2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; as x2&lt;/P&gt;&lt;P&gt;&amp;nbsp; from F1;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It is not the pinnacle of elegance but it doesn't look like a quirky trick either, IMHO. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 04 May 2013 01:14:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculated-values-in-PROC-SQL/m-p/129632#M35341</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2013-05-04T01:14:31Z</dc:date>
    </item>
    <item>
      <title>Re: Calculated values in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculated-values-in-PROC-SQL/m-p/129633#M35342</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Forget the CALCULATED keyword, just repeat the expression.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;STRONG style="font-style: inherit; font-family: inherit;"&gt;(c) as c2,&amp;nbsp; &lt;BR /&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; case&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when&lt;STRONG style="font-style: inherit; font-family: inherit;"&gt; (c)&lt;/STRONG&gt; &amp;gt; 0 then x**2&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 04 May 2013 01:43:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculated-values-in-PROC-SQL/m-p/129633#M35342</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2013-05-04T01:43:48Z</dc:date>
    </item>
    <item>
      <title>Re: Calculated values in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculated-values-in-PROC-SQL/m-p/129634#M35343</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That is indeed one of the workarounds I consider. The other is to write&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table F3 as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; nom,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (c)+0 as c3,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; case&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when calculated c3 &amp;gt; 0 then x**2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; as x3&lt;/P&gt;&lt;P&gt;&amp;nbsp; from F1;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm still not decided which I should choose. The second one has the (dubious?) advantage to be faster, since I do not have to evaluate again the value c, but it may be a very, very tiny performance penalty to use the second one (only very complex Boolean conditions would impede on performance).&lt;/P&gt;&lt;DIV id="bikatoc" style="display: none;"&gt;&lt;A class="control"&gt;close&lt;/A&gt;&lt;DIV class="toc"&gt;&lt;UL&gt;&lt;/UL&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 04 May 2013 03:11:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculated-values-in-PROC-SQL/m-p/129634#M35343</guid>
      <dc:creator>bissjoe</dc:creator>
      <dc:date>2013-05-04T03:11:35Z</dc:date>
    </item>
  </channel>
</rss>

