<?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: How to sum IF the variables exist in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-to-sum-IF-the-variables-exist/m-p/455770#M70039</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114875"&gt;@Mahtalii&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hello All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to do a simple sum() function over some variables in my tables. And since I have so many tables, I want to write one macro code for all of them.&amp;nbsp;The problem is, not all the tables have all the variables! And if you run sum(a1, a2, a3) while for example, "a1" doesn't exist in the table, you'll get an error!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You do not get an error in a data step.&amp;nbsp;If you use sum or addition with a variable that is not previously defined you will get a NOTE in the log that the variable is uninitialized and add a variable of that name to the output data set if any&amp;nbsp;with a missing value but not an error. Of course then you have added an undocumented variable or more to a data set.&lt;/P&gt;
&lt;P&gt;So I have to assume you are attempting this in Proc SQL or possibly elsewhere.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might show the macro you are currently attempting and an example of use to see if a modification is possible to the existing code. The general approach would require accessing the dataset to check the existence of the variables in the set, determine if it is numeric in the set and then return a reduced set of variables to be referenced in an (assumed) SUM function call.&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;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 19 Apr 2018 22:44:44 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2018-04-19T22:44:44Z</dc:date>
    <item>
      <title>How to sum IF the variables exist</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-sum-IF-the-variables-exist/m-p/455769#M70038</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to do a simple sum() function over some variables in my tables. And since I have so many tables, I want to write one macro code for all of them.&amp;nbsp;The problem is, not all the tables have all the variables! And if you run sum(a1, a2, a3) while for example, "a1" doesn't exist in the table, you'll get an error!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wonder if there is any way to tell SAS to sum(&lt;SPAN&gt;a1, a2, a3&lt;/SPAN&gt;) for those that exist in the table.&lt;/P&gt;&lt;P&gt;For example, if "a1" doesn't exist, then sum(&lt;SPAN&gt;a1, a2, a3&lt;/SPAN&gt;) should give me sum(&lt;SPAN&gt;a2, a3&lt;/SPAN&gt;). And if none of them exist then missing value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestion is much appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers,&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Apr 2018 22:15:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-sum-IF-the-variables-exist/m-p/455769#M70038</guid>
      <dc:creator>Mahtalii</dc:creator>
      <dc:date>2018-04-19T22:15:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum IF the variables exist</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-sum-IF-the-variables-exist/m-p/455770#M70039</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114875"&gt;@Mahtalii&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hello All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to do a simple sum() function over some variables in my tables. And since I have so many tables, I want to write one macro code for all of them.&amp;nbsp;The problem is, not all the tables have all the variables! And if you run sum(a1, a2, a3) while for example, "a1" doesn't exist in the table, you'll get an error!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You do not get an error in a data step.&amp;nbsp;If you use sum or addition with a variable that is not previously defined you will get a NOTE in the log that the variable is uninitialized and add a variable of that name to the output data set if any&amp;nbsp;with a missing value but not an error. Of course then you have added an undocumented variable or more to a data set.&lt;/P&gt;
&lt;P&gt;So I have to assume you are attempting this in Proc SQL or possibly elsewhere.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might show the macro you are currently attempting and an example of use to see if a modification is possible to the existing code. The general approach would require accessing the dataset to check the existence of the variables in the set, determine if it is numeric in the set and then return a reduced set of variables to be referenced in an (assumed) SUM function call.&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Apr 2018 22:44:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-sum-IF-the-variables-exist/m-p/455770#M70039</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-19T22:44:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum IF the variables exist</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-sum-IF-the-variables-exist/m-p/455775#M70042</link>
      <description>&lt;P&gt;Do you have a naming convention to your variables? If so, like in your sample, you can use variable shortcuts.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Sum(of a:);

Sum(of a1-a3);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114875"&gt;@Mahtalii&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hello All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to do a simple sum() function over some variables in my tables. And since I have so many tables, I want to write one macro code for all of them.&amp;nbsp;The problem is, not all the tables have all the variables! And if you run sum(a1, a2, a3) while for example, "a1" doesn't exist in the table, you'll get an error!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I wonder if there is any way to tell SAS to sum(&lt;SPAN&gt;a1, a2, a3&lt;/SPAN&gt;) for those that exist in the table.&lt;/P&gt;
&lt;P&gt;For example, if "a1" doesn't exist, then sum(&lt;SPAN&gt;a1, a2, a3&lt;/SPAN&gt;) should give me sum(&lt;SPAN&gt;a2, a3&lt;/SPAN&gt;). And if none of them exist then missing value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any suggestion is much appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&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;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Apr 2018 23:41:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-sum-IF-the-variables-exist/m-p/455775#M70042</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-04-19T23:41:46Z</dc:date>
    </item>
  </channel>
</rss>

