<?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: PROC SQL: report the count of column averages in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/663835#M78946</link>
    <description>&lt;P&gt;Most of us won't open attachments, or cannot open attachments, as they can be a security threat. You should include your SAS program in the post instead of as an attachment. Use the running man icon to include your SAS code.&lt;/P&gt;</description>
    <pubDate>Sun, 21 Jun 2020 11:59:15 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-06-21T11:59:15Z</dc:date>
    <item>
      <title>PROC SQL: report the count of column averages</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/663823#M78943</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input BAT$ CAT DAT FAT$ GAT HAT;
CARDS;
as 6 .5 ez 2 12
es 3 .7 az 7 5
os 11 .2 yz 9 3
us 13 .4 uz 11 8
is 2 .8 iz 1 10
ys 5 .6 oz 4 13
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hi everyone.&lt;BR /&gt;First, I thank everyone who takes time to consider this exercise and provide feedback on the solution. Ok, here goes my challenge.&lt;BR /&gt;&lt;BR /&gt;1. I need to accomplish this in one "proc SQL" procedure in SAS.&lt;BR /&gt;2. The product of the procedure should be a macro variable storing a single value. Just that. No revised/final table.&lt;BR /&gt;3. The stored value in the macro variable should be a simple count of the column averages of a source table "have" like the one attached.&lt;BR /&gt;4. The column averages that qualify for being counted should be those above a pre-determined threshold value (in the case of this example, the threshold = 1).&lt;BR /&gt;5. (Note:) Not all of the table columns are numeric, and obviously the non-numeric columns should not be counted.&lt;BR /&gt;&lt;BR /&gt;In the example image attached only 3 columns (CAT, GAT, HAT) averages qualify for counting, thus the macro variable takes the value: 3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are the inputs for source dataset "have":&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input BAT$ CAT DAT FAT$ GAT HAT;&lt;BR /&gt;CARDS;&lt;BR /&gt;as 6 .5 ez 2 12&lt;BR /&gt;es 3 .7 az 7 5&lt;BR /&gt;os 11 .2 yz 9 3&lt;BR /&gt;us 13 .4 uz 11 8&lt;BR /&gt;is 2 .8 iz 1 10&lt;BR /&gt;ys 5 .6 oz 4 13&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Thanks once more. Regards.&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jun 2020 12:36:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/663823#M78943</guid>
      <dc:creator>SAS_Illyrian</dc:creator>
      <dc:date>2020-06-21T12:36:00Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL: report the count of column averages</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/663825#M78944</link>
      <description>&lt;P&gt;Please supply example data in usable form. A &lt;STRONG&gt;picture&lt;/STRONG&gt; of a &lt;STRONG&gt;handwritten note&lt;/STRONG&gt; in a &lt;STRONG&gt;PDF&lt;/STRONG&gt; is &lt;STRONG&gt;NOT USABLE&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;Post your dataset as a data step with datalines, which we can copy into our SAS environment and submit.&lt;/P&gt;
&lt;P&gt;Use the "little running man" button to post your code.&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jun 2020 10:28:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/663825#M78944</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-21T10:28:59Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL: report the count of column averages</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/663830#M78945</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry for any inconvenience. Following your advice, I updated the thread with the necessary input.&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jun 2020 11:36:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/663830#M78945</guid>
      <dc:creator>SAS_Illyrian</dc:creator>
      <dc:date>2020-06-21T11:36:47Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL: report the count of column averages</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/663835#M78946</link>
      <description>&lt;P&gt;Most of us won't open attachments, or cannot open attachments, as they can be a security threat. You should include your SAS program in the post instead of as an attachment. Use the running man icon to include your SAS code.&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jun 2020 11:59:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/663835#M78946</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-06-21T11:59:15Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL: report the count of column averages</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/664150#M78959</link>
      <description>&lt;P&gt;In SAS SQL, you can do it fast and dirty:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;A logical value is equivalent to a 1 or a 0, so you can just sum them&lt;/LI&gt;
&lt;LI&gt;The SUM function can be used to sum stuff in columns, not just between rows&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let threshold=1;&lt;BR /&gt;proc sql noprint;
  select sum(avg(cat)&amp;gt;&amp;amp;threshold,avg(dat)&amp;gt;&amp;amp;threshold,avg(gat)&amp;gt;&amp;amp;threshold,avg(hat)&amp;gt;&amp;amp;threshold) into :count trimmed
  from have;
quit;

%put &amp;amp;count;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In standard SQL, you will have to use a CASE...WHEN instead, to convert the logical values to numerics:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let Threshold=1;
proc sql noprint;
  select 
    case when avg(cat)&amp;gt;&amp;amp;threshold then 1 else 0 end
    +case when avg(dat)&amp;gt;&amp;amp;threshold then 1 else 0 end
    +case when avg(gat)&amp;gt;&amp;amp;threshold then 1 else 0 end
    +case when avg(hat)&amp;gt;&amp;amp;threshold then 1 else 0 end
    into :count trimmed
  from have;
quit;

%put &amp;amp;count;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you need to generate the names of the numeric columns automatically, that can be done using DICTIONARY.COLUMNS:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let Threshold=1;&lt;BR /&gt;&lt;BR /&gt;proc sql noprint;
  select cats("case when avg(",name,")&amp;gt;&amp;amp;threshold then 1 else 0 end") 
    into :expression separated by '+'
  from dictionary.columns
  where libname='WORK' and memname='HAVE' and type='num';
  select &amp;amp;expression into :count trimmed
  from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Jun 2020 06:43:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/664150#M78959</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-06-23T06:43:44Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL: report the count of column averages</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/664156#M78961</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt; ,&lt;/P&gt;&lt;P&gt;Thanks the prompt reply with your approach.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can the threshold be coded as a real interval:&lt;/P&gt;&lt;P&gt;-0.8 &amp;lt; threshold &amp;lt; 0.3&lt;/P&gt;&lt;P&gt;...??&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jun 2020 07:48:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/664156#M78961</guid>
      <dc:creator>SAS_Illyrian</dc:creator>
      <dc:date>2020-06-23T07:48:03Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL: report the count of column averages</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/664162#M78962</link>
      <description>&lt;P&gt;The easiest way to code the stuff may be a small macro, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test_avg(var);
  case when -0.8&amp;lt;avg(&amp;amp;var)&amp;lt;0.3 then 1 else 0 end
%mend;

proc sql noprint;
  select cats('%test_avg(',name,')') 
    into :expression separated by '+'
  from dictionary.columns
  where libname='WORK' and memname='HAVE' and type='num';
  select &amp;amp;expression into :count trimmed
  from have;
quit;

%put &amp;amp;count;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Just put whatever criteria you need into the macro. And do NOT put a semicolon in the macro, is is an open statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This also has the advantage that the macro variable &amp;amp;expression does not become so long, meaning that you do not risk overflow (total text longer than maximum allowed macro variable length) even if you have several thousand numeric variables on your table.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jun 2020 08:07:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/664162#M78962</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-06-23T08:07:17Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL: report the count of column averages</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/664164#M78963</link>
      <description>&lt;P&gt;I have a slightly different take, but the same direction:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input BAT$ CAT DAT FAT$ GAT HAT;
CARDS;
as 6 .5 ez 2 12
es 3 .7 az 7 5
os 11 .2 yz 9 3
us 13 .4 uz 11 8
is 2 .8 iz 1 10
ys 5 .6 oz 4 13
;

%let treshold=1;

proc sql noprint;
select "avg(" !! strip(name) !! ")&amp;gt;&amp;amp;treshold" into :averages separated by ","
from dictionary.columns
where libname = "WORK" and memname = "HAVE" and type = "num";
select
  sum(&amp;amp;averages)
  into :want
from have
;
quit;

%put &amp;amp;=want;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Jun 2020 08:16:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/664164#M78963</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-23T08:16:15Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL: report the count of column averages</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/664233#M78964</link>
      <description>&lt;P&gt;The above code did the trick for me, Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;; short and elegant.&lt;BR /&gt;I thank all other thread participants as well.&lt;BR /&gt;&lt;BR /&gt;The solution was needed to retreive the number of non-intercept coefficients that met a threshold criterion out of a "proc Varmax" estimators' output dataset, in order to use it to calculate the Adjusted R^2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Untitled.jpg" style="width: 610px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/46511iC2D927C885080981/image-size/large?v=v2&amp;amp;px=999" role="button" title="Untitled.jpg" alt="Untitled.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Hope this solution helps many others with their work.&lt;BR /&gt;&lt;BR /&gt;All the best to everyone.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jun 2020 12:21:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-report-the-count-of-column-averages/m-p/664233#M78964</guid>
      <dc:creator>SAS_Illyrian</dc:creator>
      <dc:date>2020-06-23T12:21:06Z</dc:date>
    </item>
  </channel>
</rss>

