<?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 tabulate to calculate-weighted avg,sum,N,PCT in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-tabulate-to-calculate-weighted-avg-sum-N-PCT/m-p/486634#M126653</link>
    <description>&lt;P&gt;Sometimes, you can get the job done in less programming time if you don't insist the result has to come from one PROC or one DATA step. If you are willing to use two (or more) PROCs or DATA steps, then the answer comes relatively quickly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;UNTESTED CODE&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=loans_tbl1806;
    class score;
    var interest/weight=sum_loan;
    var sum_loan;
    output out=_stats_ mean(interest)=mean_interest n(sum_loan)=n_loan sum(sum_loan)=;
run;

data want;
    if _n_=1 then set _stats_(where=(_type_=0) rename=(sum_loan=total_sum_loan n_loan=total_n_loan)
        drop=mean_interest score);
    set _stats_(where=(_type_=1));
    percent_of_sum_loan=sum_loan/total_sum_loan;
    percent_of_n_loan=n_loan/total_n_loan;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 14 Aug 2018 12:48:38 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2018-08-14T12:48:38Z</dc:date>
    <item>
      <title>proc tabulate to calculate-weighted avg,sum,N,PCT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-tabulate-to-calculate-weighted-avg-sum-N-PCT/m-p/486541#M126608</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I have a row data of loans taken by customers.&lt;/P&gt;&lt;P&gt;I need to calculate for each score (category variable)the following 5&amp;nbsp;measurements:&lt;/P&gt;&lt;P&gt;1-weighted average of interest &amp;nbsp;(weight by sum of loan)&lt;/P&gt;&lt;P&gt;2-sum of loan&lt;/P&gt;&lt;P&gt;3-number of loans&lt;/P&gt;&lt;P&gt;4-percent from total of sum of loans&lt;/P&gt;&lt;P&gt;5-&lt;SPAN&gt;percent from total of&amp;nbsp;number of loans&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;I want to use proc tabulate because in proc means we cannot calculate&amp;nbsp;&lt;SPAN&gt;percent from total in one step&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I would like to see please proc tabulate code that calculate all 5&amp;nbsp;&lt;/SPAN&gt;measurements&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;thanks&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Jowl&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Loans_tbl1806;
input customer score $  sum_loan  interest;
cards;
1  a  100  5
2  a  200  4
3  a  300  4
4  b  400  3.5
5  b  500  2.8
6  c  100  5
7  c  200  4
8  c  300  4
9  c   400  3.5
10 c  1500  2.8
;
run;&lt;/CODE&gt;&lt;/PRE&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>Tue, 14 Aug 2018 10:49:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-tabulate-to-calculate-weighted-avg-sum-N-PCT/m-p/486541#M126608</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-08-14T10:49:19Z</dc:date>
    </item>
    <item>
      <title>Re: proc tabulate to calculate-weighted avg,sum,N,PCT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-tabulate-to-calculate-weighted-avg-sum-N-PCT/m-p/486625#M126649</link>
      <description>&lt;P&gt;1. You can only weigh everything or nothing in one proc tabulate step&lt;/P&gt;
&lt;P&gt;2. Show the expected output&lt;/P&gt;
&lt;P&gt;3. Why not use SQL?&lt;/P&gt;</description>
      <pubDate>Tue, 14 Aug 2018 12:05:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-tabulate-to-calculate-weighted-avg-sum-N-PCT/m-p/486625#M126649</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-08-14T12:05:08Z</dc:date>
    </item>
    <item>
      <title>Re: proc tabulate to calculate-weighted avg,sum,N,PCT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-tabulate-to-calculate-weighted-avg-sum-N-PCT/m-p/486634#M126653</link>
      <description>&lt;P&gt;Sometimes, you can get the job done in less programming time if you don't insist the result has to come from one PROC or one DATA step. If you are willing to use two (or more) PROCs or DATA steps, then the answer comes relatively quickly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;UNTESTED CODE&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=loans_tbl1806;
    class score;
    var interest/weight=sum_loan;
    var sum_loan;
    output out=_stats_ mean(interest)=mean_interest n(sum_loan)=n_loan sum(sum_loan)=;
run;

data want;
    if _n_=1 then set _stats_(where=(_type_=0) rename=(sum_loan=total_sum_loan n_loan=total_n_loan)
        drop=mean_interest score);
    set _stats_(where=(_type_=1));
    percent_of_sum_loan=sum_loan/total_sum_loan;
    percent_of_n_loan=n_loan/total_n_loan;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Aug 2018 12:48:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-tabulate-to-calculate-weighted-avg-sum-N-PCT/m-p/486634#M126653</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-08-14T12:48:38Z</dc:date>
    </item>
    <item>
      <title>Re: proc tabulate to calculate-weighted avg,sum,N,PCT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-tabulate-to-calculate-weighted-avg-sum-N-PCT/m-p/486709#M126685</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I have a row data of loans taken by customers.&lt;/P&gt;
&lt;P&gt;I need to calculate for each score (category variable)the following 5&amp;nbsp;measurements:&lt;/P&gt;
&lt;P&gt;1-weighted average of interest &amp;nbsp;(weight by sum of loan)&lt;/P&gt;
&lt;P&gt;2-sum of loan&lt;/P&gt;
&lt;P&gt;3-number of loans&lt;/P&gt;
&lt;P&gt;4-percent from total of sum of loans&lt;/P&gt;
&lt;P&gt;5-&lt;SPAN&gt;percent from total of&amp;nbsp;number of loans&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;I want to use proc tabulate because in proc means we cannot calculate&amp;nbsp;&lt;SPAN&gt;percent from total in one step&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I would like to see please proc tabulate code that calculate all 5&amp;nbsp;&lt;/SPAN&gt;measurements&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;thanks&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Jowl&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Loans_tbl1806;
input customer score $  sum_loan  interest;
cards;
1  a  100  5
2  a  200  4
3  a  300  4
4  b  400  3.5
5  b  500  2.8
6  c  100  5
7  c  200  4
8  c  300  4
9  c   400  3.5
10 c  1500  2.8
;
run;&lt;/CODE&gt;&lt;/PRE&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;While proc tabulate can create some percentages of sums or ns of other variables anything involving a "weight" would apply to all calculations and what ever you may mean by "weight by sum of loan" is likely not possible. In face, I am not sure exactly what you may mean by that "weight by sum of loan" and would need to see a worked out example by hand to tell what is needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Aug 2018 15:01:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-tabulate-to-calculate-weighted-avg-sum-N-PCT/m-p/486709#M126685</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-08-14T15:01:18Z</dc:date>
    </item>
  </channel>
</rss>

