<?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: aggraegative fields-How to write more efficient program in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/473068#M121348</link>
    <description>Thank you.&lt;BR /&gt;What happens when a customer has some loans with different grades?&lt;BR /&gt;The customer will be counted in each of tye grades.&lt;BR /&gt;It is the final desired output&lt;BR /&gt;</description>
    <pubDate>Mon, 25 Jun 2018 17:08:08 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2018-06-25T17:08:08Z</dc:date>
    <item>
      <title>aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472778#M121253</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I work in a bank as a credit risk analyst.&lt;/P&gt;&lt;P&gt;I want to talk about &amp;nbsp;a monthly &amp;nbsp;loans data set .&lt;/P&gt;&lt;P&gt;In this table there are new loans that customers takes from the bank.&lt;/P&gt;&lt;P&gt;As you can see in the data it might happen that a customer takes more than one loan.&lt;/P&gt;&lt;P&gt;The target is to calculate 5 aggraegative fields:&lt;/P&gt;&lt;P&gt;"No_Loans" &amp;nbsp; is the number of loans (number of rows in the table)&lt;/P&gt;&lt;P&gt;"No_Customers" &amp;nbsp;is the number of customers who took a loan&lt;/P&gt;&lt;P&gt;"Sum_Loan" &amp;nbsp;is the sum of loans (in us dollars)&lt;/P&gt;&lt;P&gt;"Weighted_interest" &amp;nbsp;is the weighted interest average (Weighed by sum of loans)&lt;/P&gt;&lt;P&gt;"Pct_Sum_Loan" &amp;nbsp;is pct of sum of loans in each category&lt;/P&gt;&lt;P&gt;I know to calculate these fields in a long process.&lt;/P&gt;&lt;P&gt;My question is If someone have a suggestion how to calculate it in a shorter code?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data tbl1;&lt;BR /&gt;input ID 1&lt;BR /&gt;Loan_ID 3&lt;BR /&gt;Sum_Loan 5-8&lt;BR /&gt;Interest&lt;BR /&gt;date_Loan :ddmmyy10.&lt;BR /&gt;date_Finish :ddmmyy10.&lt;BR /&gt;Loan_Model&lt;BR /&gt;grade;&lt;BR /&gt;format date_Loan date_Finish date9.;&lt;BR /&gt;Cards;&lt;BR /&gt;1 1 100 2.4 01/06/2018 01/06/2023 3312 4&lt;BR /&gt;1 2 200 2.6 02/06/2018 02/06/2021 3312 5&lt;BR /&gt;1 3 300 2.3 05/06/2018 05/06/2025 3313 5&lt;BR /&gt;2 4 400 3.1 05/06/2018 05/06/2020 3312 6&lt;BR /&gt;2 5 500 2.9 05/06/2018 05/06/2021 3313 5&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;create table Output1 as&lt;BR /&gt;select grade,&lt;BR /&gt;count(*) as No_Loans,&lt;BR /&gt;count(distinct ID) as No_Customers,&lt;BR /&gt;sum(Sum_Loan) as Sum_Loan&lt;BR /&gt;from tbl1&lt;BR /&gt;group by grade&lt;BR /&gt;;&lt;BR /&gt;QUIT;&lt;BR /&gt;/*Calculate weighted average of interest by sum Loans*/&lt;BR /&gt;PROC SQL;&lt;BR /&gt;create table Output2 as&lt;BR /&gt;select grade,&lt;BR /&gt;sum(Interest*Sum_Loan)/sum(Sum_Loan) as Weighted_interest&lt;BR /&gt;from tbl1&lt;BR /&gt;group by grade&lt;BR /&gt;;&lt;BR /&gt;QUIT;&lt;BR /&gt;/*Calculate Percent Tamhil*/&lt;BR /&gt;PROC SQL;&lt;BR /&gt;create table Output3 as&lt;BR /&gt;select grade,&lt;BR /&gt;Sum_Loan/sum(Sum_Loan) as Pct_Sum_Loan format=percent9.1&lt;BR /&gt;from Output1&lt;BR /&gt;;&lt;BR /&gt;QUIT;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;create table Output4 as&lt;BR /&gt;select a.*,b.Weighted_interest,c.Pct_Sum_Loan&lt;BR /&gt;from Output1 as a&lt;BR /&gt;left join Output2 as b&lt;BR /&gt;on a.grade=b.grade&lt;BR /&gt;left join Output3 as c&lt;BR /&gt;on a.grade=c.grade&lt;BR /&gt;;&lt;BR /&gt;QUIT;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;create table Output5 as&lt;BR /&gt;select sum(No_Loans) as No_Loans,&lt;BR /&gt;sum(No_Customers) as No_Customers,&lt;BR /&gt;sum(Sum_Loan) as Sum_Loan,&lt;BR /&gt;sum(Weighted_interest*Pct_Sum_Loan) as Weighted_interest,&lt;BR /&gt;sum(Pct_Sum_Loan) as Pct_Sum_Loan format=percent9.1&lt;BR /&gt;from Output4&lt;BR /&gt;;&lt;BR /&gt;QUIT;&lt;BR /&gt;Data FinalOutput(drop=grade rename=(grade_Char=grade));&lt;BR /&gt;Retain grade_Char;&lt;BR /&gt;Set Output4 Output5;&lt;BR /&gt;length grade_Char $3.;&lt;BR /&gt;grade_Char=put(grade,2.);&lt;BR /&gt;If grade=. then grade_Char='All';&lt;BR /&gt;Run;&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>Sun, 24 Jun 2018 12:09:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472778#M121253</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-06-24T12:09:22Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472781#M121254</link>
      <description>&lt;P&gt;All of these can be computed by PROC SUMMARY (with the WEIGHT statement if needed).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is my experience that PROC SUMMARY will do it faster than PROC SQL, and I believe that all of your computations can be done in a single PROC SUMMARY call (although I didn't specifically verify this).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Furthermore, there are times when doing things in SQL produces the WRONG results, specifically, you have&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;sum(Interest*Sum_Loan)/sum(Sum_Loan) as Weighted_interest&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the value of interest is missing for an observation, then the above produces the wrong answer, where a different number of observations is used in the numerator than the denominator (which will not happen in PROC SUMMARY, which gives the right answer in this situation)&lt;/P&gt;</description>
      <pubDate>Sun, 24 Jun 2018 13:05:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472781#M121254</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-06-24T13:05:14Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472782#M121255</link>
      <description>&lt;P&gt;Thanks for your reply.&lt;/P&gt;&lt;P&gt;I know Proc summary but it didn't work for me in order to get all calculated fields.&lt;/P&gt;&lt;P&gt;May you please send a code and show me that all the required fields are calculated?&lt;/P&gt;&lt;P&gt;I think that it is better to see a code that is working then say that something is working without show it&lt;/P&gt;&lt;P&gt;Joe&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Jun 2018 13:17:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472782#M121255</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-06-24T13:17:32Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472785#M121256</link>
      <description>&lt;P&gt;EXAMPLE:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=tbl1;
    class grade;
    var id sum_loan;
    var interest/weight=sum_loan;
    output out=sums n(id)=no_loans sum(sum_loan)=sum_loan mean(interest)=weighted_interest;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 24 Jun 2018 13:30:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472785#M121256</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-06-24T13:30:27Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472814#M121270</link>
      <description>&lt;P&gt;Nice example &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;! But counting IDs may give the wrong result. One should count the non missing sum_loan.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token function"&gt;n&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(sum_loan&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;no_loans&lt;/P&gt;</description>
      <pubDate>Sun, 24 Jun 2018 19:55:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472814#M121270</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-06-24T19:55:04Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472819#M121271</link>
      <description>It is great but in your code you didn't calculate number of distinct customers in each category&lt;BR /&gt;</description>
      <pubDate>Sun, 24 Jun 2018 20:18:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472819#M121271</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-06-24T20:18:22Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472826#M121274</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;It is great but in your code you didn't calculate number of distinct customers in each category&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, I leave that as an exercise for you, after all I did say it was an EXAMPLE.(Hint: run PROC SUMMARY twice)&lt;/P&gt;</description>
      <pubDate>Sun, 24 Jun 2018 21:25:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472826#M121274</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-06-24T21:25:10Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472869#M121288</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;You also didn't calculate "Pct_Sum_Loan".&lt;/P&gt;&lt;P&gt;And also didn't calculate "No_Customers"&lt;/P&gt;&lt;P&gt;As you can see you are not able to calculate all the required fields in one step.&lt;/P&gt;&lt;P&gt;So, the way that I calculated by using proc sql's was not less efficient&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jun 2018 04:54:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472869#M121288</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-06-25T04:54:12Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472872#M121291</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;As you can see the code that you wrote didn't have some elements:&lt;/P&gt;&lt;P&gt;You also didn't calculate "Pct_Sum_Loan".&lt;/P&gt;&lt;P&gt;You didn't calculate "No_Customers".&lt;/P&gt;&lt;P&gt;In the total row instead of null value we need to have the word "All".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jun 2018 05:29:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472872#M121291</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-06-25T05:29:01Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472910#M121302</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 (boldface added):&lt;BR /&gt;
&lt;P&gt;As you can see you are not able to &lt;STRONG&gt;calculate all the required fields in one step&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;So, the way that I calculated by using proc sql's was not less efficient&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you insist on doing all calculations in one step (which might indeed improve efficiency), you can write a single &lt;EM&gt;data step&lt;/EM&gt;. Obviously, by going through all observations&amp;nbsp;of TBL1 you can gather the information necessary to calculate the aggregate values. Having tools such as DOW loops (see many conference papers), arrays and hash objects at your disposal, it is possible to compute and buffer the final variable values until they are written to the output dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For this approach it would be helpful,&amp;nbsp;but not mandatory, to have TBL1 sorted or indexed by GRADE beforehand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you familiar with the data step techniques mentioned above? If so, just give it a try. Feel free to come back with specific questions if you get stuck.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jun 2018 13:54:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472910#M121302</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-06-25T13:54:26Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472924#M121304</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;As you can see the code that you wrote didn't have some elements:&lt;/P&gt;
&lt;P&gt;You also didn't calculate "Pct_Sum_Loan".&lt;/P&gt;
&lt;P&gt;You didn't calculate "No_Customers".&lt;/P&gt;
&lt;P&gt;In the total row instead of null value we need to have the word "All".&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;As you can see, I said I was giving you an EXAMPLE and never intended to do all your work for you. Use this as a learning opportunity and see if you can figure it out yourself.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jun 2018 11:03:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472924#M121304</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-06-25T11:03:47Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472927#M121306</link>
      <description>&lt;P&gt;The problem with doing it in a data step is error checking. You have to make the code smart enough to handle missings properly (which the SQL shown does not do), and the code must be smart enough to handle different groups, and there may be other issues as well. SAS has already written code to do these calculations properly, with all the necessary error checking and handling of groups, and that code is PROC MEANS or PROC SUMMARY. I really don't see a need to write your own code to compute sums and means.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jun 2018 11:07:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472927#M121306</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-06-25T11:07:58Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472992#M121327</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sure, I fully agree. I wouldn't have preferred that more complicated data step solution over the obvious and simple combination of PROC SUMMARY and PROC SQL results either. I just wanted to point out that it &lt;EM&gt;could&lt;/EM&gt; be done &lt;EM&gt;if&lt;/EM&gt; one &lt;EM&gt;insisted&lt;/EM&gt; on putting it all into a single step. In terms of performance, the multi-threading capabilities of PROC SUMMARY and PROC SQL can&amp;nbsp;be an advantage over the data step (if supported by hardware).&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jun 2018 13:47:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472992#M121327</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-06-25T13:47:21Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472997#M121329</link>
      <description>&lt;P&gt;As far as I can tell, the original question did not insist on a single process step; and I certainly don't insist on that. It's hard for me to imagine a reason why anyone would insist on this, unless it was a homework assignment, which is not the case here.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jun 2018 13:50:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/472997#M121329</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-06-25T13:50:57Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/473000#M121330</link>
      <description>The task is to see if there is a more usefull way to create exactly same output as I created .&lt;BR /&gt;Until now nobody sent a usefull code that calculate all required fields in less steps</description>
      <pubDate>Mon, 25 Jun 2018 13:54:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/473000#M121330</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-06-25T13:54:14Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/473003#M121331</link>
      <description>All is fine. I wrote my code and it worked very well.&lt;BR /&gt;I just wanted to know if there is a better or more usefull way to write the code.&lt;BR /&gt;I thought that it can be usefull to all forum members.&lt;BR /&gt;</description>
      <pubDate>Mon, 25 Jun 2018 13:59:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/473003#M121331</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-06-25T13:59:32Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/473044#M121340</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 work in a bank as a credit risk analyst.&lt;/P&gt;
&lt;P&gt;I want to talk about &amp;nbsp;a monthly &amp;nbsp;loans data set .&lt;/P&gt;
&lt;P&gt;In this table there are new loans that customers takes from the bank.&lt;/P&gt;
&lt;P&gt;As you can see in the data it might happen that a customer takes more than one loan.&lt;/P&gt;
&lt;P&gt;The target is to calculate 5 aggraegative fields:&lt;/P&gt;
&lt;P&gt;"No_Loans" &amp;nbsp; is the number of loans (number of rows in the table)&lt;/P&gt;
&lt;P&gt;"No_Customers" &amp;nbsp;is the number of customers who took a loan&lt;/P&gt;
&lt;P&gt;"Sum_Loan" &amp;nbsp;is the sum of loans (in us dollars)&lt;/P&gt;
&lt;P&gt;"Weighted_interest" &amp;nbsp;is the weighted interest average (Weighed by sum of loans)&lt;/P&gt;
&lt;P&gt;"Pct_Sum_Loan" &amp;nbsp;is pct of sum of loans in each category&lt;/P&gt;
&lt;P&gt;I know to calculate these fields in a long process.&lt;/P&gt;
&lt;P&gt;My question is If someone have a suggestion how to calculate it in a shorter code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data tbl1;&lt;BR /&gt;input ID 1&lt;BR /&gt;Loan_ID 3&lt;BR /&gt;Sum_Loan 5-8&lt;BR /&gt;Interest&lt;BR /&gt;date_Loan :ddmmyy10.&lt;BR /&gt;date_Finish :ddmmyy10.&lt;BR /&gt;Loan_Model&lt;BR /&gt;grade;&lt;BR /&gt;format date_Loan date_Finish date9.;&lt;BR /&gt;Cards;&lt;BR /&gt;1 1 100 2.4 01/06/2018 01/06/2023 3312 4&lt;BR /&gt;1 2 200 2.6 02/06/2018 02/06/2021 3312 5&lt;BR /&gt;1 3 300 2.3 05/06/2018 05/06/2025 3313 5&lt;BR /&gt;2 4 400 3.1 05/06/2018 05/06/2020 3312 6&lt;BR /&gt;2 5 500 2.9 05/06/2018 05/06/2021 3313 5&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;create table Output1 as&lt;BR /&gt;select grade,&lt;BR /&gt;count(*) as No_Loans,&lt;BR /&gt;count(distinct ID) as No_Customers,&lt;BR /&gt;sum(Sum_Loan) as Sum_Loan&lt;BR /&gt;from tbl1&lt;BR /&gt;group by grade&lt;BR /&gt;;&lt;BR /&gt;QUIT;&lt;BR /&gt;/*Calculate weighted average of interest by sum Loans*/&lt;BR /&gt;PROC SQL;&lt;BR /&gt;create table Output2 as&lt;BR /&gt;select grade,&lt;BR /&gt;sum(Interest*Sum_Loan)/sum(Sum_Loan) as Weighted_interest&lt;BR /&gt;from tbl1&lt;BR /&gt;group by grade&lt;BR /&gt;;&lt;BR /&gt;QUIT;&lt;BR /&gt;/*Calculate Percent Tamhil*/&lt;BR /&gt;PROC SQL;&lt;BR /&gt;create table Output3 as&lt;BR /&gt;select grade,&lt;BR /&gt;Sum_Loan/sum(Sum_Loan) as Pct_Sum_Loan format=percent9.1&lt;BR /&gt;from Output1&lt;BR /&gt;;&lt;BR /&gt;QUIT;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;create table Output4 as&lt;BR /&gt;select a.*,b.Weighted_interest,c.Pct_Sum_Loan&lt;BR /&gt;from Output1 as a&lt;BR /&gt;left join Output2 as b&lt;BR /&gt;on a.grade=b.grade&lt;BR /&gt;left join Output3 as c&lt;BR /&gt;on a.grade=c.grade&lt;BR /&gt;;&lt;BR /&gt;QUIT;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;create table Output5 as&lt;BR /&gt;select sum(No_Loans) as No_Loans,&lt;BR /&gt;sum(No_Customers) as No_Customers,&lt;BR /&gt;sum(Sum_Loan) as Sum_Loan,&lt;BR /&gt;sum(Weighted_interest*Pct_Sum_Loan) as Weighted_interest,&lt;BR /&gt;sum(Pct_Sum_Loan) as Pct_Sum_Loan format=percent9.1&lt;BR /&gt;from Output4&lt;BR /&gt;;&lt;BR /&gt;QUIT;&lt;BR /&gt;Data FinalOutput(drop=grade rename=(grade_Char=grade));&lt;BR /&gt;Retain grade_Char;&lt;BR /&gt;Set Output4 Output5;&lt;BR /&gt;length grade_Char $3.;&lt;BR /&gt;grade_Char=put(grade,2.);&lt;BR /&gt;If grade=. then grade_Char='All';&lt;BR /&gt;Run;&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;Your Topic title involves "How to write more efficient program" but then you specify "how to calculate it in a shorter code". Which are not the same thing. Efficiency could mean "reduced run time", "reduced disk space usage" or "reduced memory usage" or perhaps even "easy to understand and maintain code".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"Shorter code" does not necessarily do any of the above. I have seen some very short code code did some very interesting things. But minor changes to the data "broke" the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your list of requirements does not describe the role of the variable GRADE which is used in multiple places. So we're going to have a hard time with combining "no_loans" "no_customers" if grade affects these counts. What happens when a customer has some loans with different grades?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Additionally a question arises of do you want a Report at the end of this that people read or a data set to feed into another process?&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jun 2018 15:29:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/473044#M121340</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-25T15:29:47Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/473068#M121348</link>
      <description>Thank you.&lt;BR /&gt;What happens when a customer has some loans with different grades?&lt;BR /&gt;The customer will be counted in each of tye grades.&lt;BR /&gt;It is the final desired output&lt;BR /&gt;</description>
      <pubDate>Mon, 25 Jun 2018 17:08:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/473068#M121348</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-06-25T17:08:08Z</dc:date>
    </item>
    <item>
      <title>Re: aggraegative fields-How to write more efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/473089#M121356</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;Thank you.&lt;BR /&gt;What happens when a customer has some loans with different grades?&lt;BR /&gt;The customer will be counted in each of tye grades.&lt;BR /&gt;It is the final desired output&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Note that you left that out of your requirements:&lt;/P&gt;
&lt;PRE&gt;As you can see in the data it might happen that a customer takes more than one loan.

The target is to calculate 5 aggraegative fields:

"No_Loans"   is the number of loans (number of rows in the table)

"No_Customers"  is the number of customers who took a loan

"Sum_Loan"  is the sum of loans (in us dollars)

"Weighted_interest"  is the weighted interest average (Weighed by sum of loans)

"Pct_Sum_Loan"  is pct of sum of loans in each category

&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jun 2018 18:04:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/aggraegative-fields-How-to-write-more-efficient-program/m-p/473089#M121356</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-25T18:04:10Z</dc:date>
    </item>
  </channel>
</rss>

