<?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: calculating sum and counts in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculating-sum-and-counts/m-p/508119#M136426</link>
    <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;Currently I am using the below macros to achieve this. But is there a simpler way in sas to do this ?&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS has written code to do this, added many many many useful features, then debugged it, documented it, and said code has been proven effective in a bazillion, two hundred thousand and 48 real world applications.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC FREQ will do the counting for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC MEANS/PROC SUMMARY will compute means for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Don't write your own code to do this.&lt;/P&gt;</description>
    <pubDate>Sun, 28 Oct 2018 15:01:05 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2018-10-28T15:01:05Z</dc:date>
    <item>
      <title>calculating sum and counts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-sum-and-counts/m-p/508102#M136415</link>
      <description>&lt;P&gt;Hi , I am currently trying to calculate count of not null attributes and their sum and store it in another dataset. For example, If i have dataset work.students which has attributes ID, Name, age, fees etc. I need to calculate the count if ID, Name, age and fees where ID is not null and sum of fees where ID is not null (no groupings - just the total count and sum). My output needs to be like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;attribute_name&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; approach&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; value&lt;/P&gt;&lt;P&gt;id&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; count&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 100&lt;/P&gt;&lt;P&gt;name&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; count&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;/P&gt;&lt;P&gt;age&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;count&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;/P&gt;&lt;P&gt;fees&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;count&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;/P&gt;&lt;P&gt;fees&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sum&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1,50,450&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently I am using the below macros to achieve this. But is there a simpler way in sas to do this ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%macro basetable(lib,dst,att);&lt;BR /&gt;proc sql;&lt;BR /&gt;select count(&amp;amp;att.) into :cnt&lt;BR /&gt;from &amp;amp;lib..&amp;amp;dstname.;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;data work.base_dst_completeness;&lt;BR /&gt;table_name = "&amp;amp;dst." ;&lt;BR /&gt;approach = "count";&lt;BR /&gt;length attribute_name $50;&lt;BR /&gt;attribute_name = "&amp;amp;att.";&lt;BR /&gt;value = &amp;amp;cnt.;&lt;/P&gt;&lt;P&gt;run;&lt;BR /&gt;%mend basetable;&lt;/P&gt;&lt;P&gt;%basetable(work,student,id);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro calculate_value(part,att,varb,miss);&lt;BR /&gt;/* check condition if part is sum or count */&lt;/P&gt;&lt;P&gt;%if &amp;amp;part. = sum %then %do;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table work.temp_dst_1 as&lt;BR /&gt;select "sum" as approach,&lt;BR /&gt;"&amp;amp;att." as attribute_name length=50,&lt;BR /&gt;&amp;amp;part.(&amp;amp;att.) as value&lt;BR /&gt;from work.students&lt;BR /&gt;where &amp;amp;varb. is &amp;amp;miss.;&lt;BR /&gt;quit;&lt;BR /&gt;%put "calculate sum";&lt;BR /&gt;%end;&lt;BR /&gt;%else %do;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table work.temp_dst_1 as&lt;BR /&gt;select&amp;nbsp;'count' as approach,&lt;BR /&gt;"&amp;amp;att." as attribute_name length=50,&lt;BR /&gt;&amp;amp;part.(&amp;amp;att.) as value&lt;BR /&gt;from work.students&lt;BR /&gt;where &amp;amp;varb. is &amp;amp;miss.;&lt;BR /&gt;quit;&lt;BR /&gt;%put "calculate count ";&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;/* append the dataset with the base dataset */&lt;BR /&gt;proc append base = work.base_dst_completeness data=work.temp_dst_1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%mend calculate_value;&lt;/P&gt;&lt;P&gt;%calculate_value(count,name,id,not null);&lt;BR /&gt;%calculate_value(count,age,id,not null);&lt;BR /&gt;%calculate_value(count,fees,id,not null);&lt;BR /&gt;%calculate_value(sum,fees,fees,not null);&lt;/P&gt;</description>
      <pubDate>Sun, 28 Oct 2018 11:50:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-sum-and-counts/m-p/508102#M136415</guid>
      <dc:creator>Jagadeesh2907</dc:creator>
      <dc:date>2018-10-28T11:50:09Z</dc:date>
    </item>
    <item>
      <title>Re: calculating sum and counts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-sum-and-counts/m-p/508105#M136416</link>
      <description>&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;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table  vars as select name from dictionary.columns where libname='WORK' and memname='STUDENTS';
select name into: vars1 separated by ' ' from vars;&lt;BR /&gt;quit;

data _null_;
set vars;
call execute('proc sql;create table '||name||' as select "'|| name||'" as name, count('||name||') as value, "count" as approach from work.students;quit;');
if lowcase(name)='fees' then call execute('proc sql;create table sum_'||name||' as select "'|| name||'" as name, sum('||name||') as value, "sum" as approach from work.students;quit;');
run;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set &amp;amp;vars1 sum:;&lt;BR /&gt;run;&lt;BR /&gt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Oct 2018 12:41:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-sum-and-counts/m-p/508105#M136416</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2018-10-28T12:41:13Z</dc:date>
    </item>
    <item>
      <title>Re: calculating sum and counts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-sum-and-counts/m-p/508119#M136426</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;Currently I am using the below macros to achieve this. But is there a simpler way in sas to do this ?&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS has written code to do this, added many many many useful features, then debugged it, documented it, and said code has been proven effective in a bazillion, two hundred thousand and 48 real world applications.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC FREQ will do the counting for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC MEANS/PROC SUMMARY will compute means for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Don't write your own code to do this.&lt;/P&gt;</description>
      <pubDate>Sun, 28 Oct 2018 15:01:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-sum-and-counts/m-p/508119#M136426</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-10-28T15:01:05Z</dc:date>
    </item>
    <item>
      <title>Re: calculating sum and counts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-sum-and-counts/m-p/508637#M136632</link>
      <description>&lt;P&gt;Thank you PaigeMiller,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried Proc Means - Yes it help but only for numeric values and proc freq which provides a data level frequency than a attribute level count which is my need. That is why i wrote that macro. However, i felt my macro can be converted into a single one as i use first macro for creating a dataset to which i can append other datasets from second macro. Can you give me any tips ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 10:55:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-sum-and-counts/m-p/508637#M136632</guid>
      <dc:creator>Jagadeesh2907</dc:creator>
      <dc:date>2018-10-30T10:55:08Z</dc:date>
    </item>
    <item>
      <title>Re: calculating sum and counts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-sum-and-counts/m-p/508639#M136633</link>
      <description>&lt;P&gt;PROC FREQ will do what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, you have to show us a portion of your actual input data if you want a more detailed reply. As stated above, there is no need to write your own macro here. I consider it an extreme waste of time to write a macro to replicate the functionality of PROC MEANS and PROC FREQ.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 11:02:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-sum-and-counts/m-p/508639#M136633</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-10-30T11:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: calculating sum and counts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-sum-and-counts/m-p/513243#M138279</link>
      <description>&lt;P&gt;Hi PaigeMiller,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your reply. Sorry for the late response as i was away. My requirement is pretty simple. If a data set has 5 attributes, i want the count of all 5 attributes (excluding the nulls for that attribute) and sum of only numeric attribute from that 5 attributes. If 2 out of 5 are numeric, i need both count and sum of the attributes.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Eg: If dataset has 5 attributes and 200 records,&lt;/P&gt;&lt;P&gt;attribute1 (Character column) - count = 200 (My Key column)&lt;/P&gt;&lt;P&gt;attribute 2 (character) - count = 180 (20 are null values)&lt;/P&gt;&lt;P&gt;attribute3 (numeric) - count = 200 sum = 4500 (no nulls and sum calculated)&lt;/P&gt;&lt;P&gt;etc.&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, 15 Nov 2018 10:11:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-sum-and-counts/m-p/513243#M138279</guid>
      <dc:creator>Jagadeesh2907</dc:creator>
      <dc:date>2018-11-15T10:11:23Z</dc:date>
    </item>
  </channel>
</rss>

