<?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: %DO %UNTIL in PROC SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/564175#M158222</link>
    <description>&lt;P&gt;Sorry for the mess.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've got this manager with a bugdet of $861,512,982.00&amp;nbsp; and another table with list of clients and their balances&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TABLE A&lt;/P&gt;&lt;P&gt;M1 Budget=&amp;nbsp;$861,512,982.00&lt;/P&gt;&lt;P&gt;MID = 952&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TABLE B&lt;/P&gt;&lt;P&gt;MID = 952&lt;/P&gt;&lt;P&gt;C1 BAL=1,590&lt;/P&gt;&lt;P&gt;C2 BAL=2,590&lt;/P&gt;&lt;P&gt;C3 BAL=150,000.00&lt;/P&gt;&lt;P&gt;C4 BAL=500,000.00&lt;/P&gt;&lt;P&gt;....................................&lt;/P&gt;&lt;P&gt;And I need to sum the balances of the clients until the total sum reach&amp;nbsp; or its near to 861,512,982.00, and the two tables only joined by MID column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this help to explain what I am looking for.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 06 Jun 2019 15:48:15 GMT</pubDate>
    <dc:creator>rodrichiez</dc:creator>
    <dc:date>2019-06-06T15:48:15Z</dc:date>
    <item>
      <title>%DO %UNTIL in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/563825#M158094</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to find a way to solved this: I need to create a table that contains all the clients that reach the 80% of the budget grouped by manager.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro ochenta ();

proc sql noprint; select budget_80 into :valor from work.QUERY_FOR_BALANCES1 where officer_id='735' and Budget_Category_Desc="Captaciones";
%put &amp;amp;valor;

%DO %until (&amp;amp;valor=&amp;amp;valor);
CREATE TABLE Clients as
SELECT t2.Customer_Id, T2.Current_Balance_Dop, SUM(t1.Current_Balance_Dop) as Total_Balance  FROM work.balances T1
left join (select t2.Customer_Id, t2.Current_Balance_Dop
FROM work.balances T2 where t2.Budget_Category_Desc="Captaciones" and t2.officer_id='735'
and t2.Current_Balance_Dop NOT =0) T2 on t1.Customer_Id=T2.Customer_Id
where t1.Budget_Category_Desc="Captaciones" and t1.officer_id='735'
group by t2.Customer_Id, t2.Current_Balance_Dop
having (CALCULATED Total_Balance) &amp;lt;= &amp;amp;valor;

%end;

%mend;
%ochenta()&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;P&gt;But the result is a list of all the clients in that manager id.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jun 2019 18:44:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/563825#M158094</guid>
      <dc:creator>rodrichiez</dc:creator>
      <dc:date>2019-06-05T18:44:59Z</dc:date>
    </item>
    <item>
      <title>Re: %DO %UNTIL in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/563878#M158113</link>
      <description>&lt;P&gt;I need all the clients from table A that the sum of their balances reach the 80% of the manager budget on table B.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jun 2019 20:18:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/563878#M158113</guid>
      <dc:creator>rodrichiez</dc:creator>
      <dc:date>2019-06-05T20:18:01Z</dc:date>
    </item>
    <item>
      <title>Re: %DO %UNTIL in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/563952#M158146</link>
      <description>&lt;P&gt;Your question is unclear and your code is a mess and you supply no data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&amp;gt; I need all the clients from table A that the sum of their balances reach the 80% of the manager budget on table B.;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Like this?&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;data CLIENTS;
  CL=1; BAL=2; output;
  CL=1; BAL=2; output;
  CL=2; BAL=3; output;
run;
data BUDGET;
  MGR='a'; CL=1; BUDGET=5; output;
  MGR='b'; CL=2; BUDGET=4; output;
run;
proc sql;
   select a.*, b.MGR, sum(BAL) as SUM
   from CLIENTS a, BUDGET b
   where a.CL=b.CL 
   group by MGR
   having calculated SUM &amp;gt;= BUDGET*.8;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="left"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r b header" scope="col"&gt;CL&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;BAL&lt;/TH&gt;
&lt;TH class="l b header" scope="col"&gt;MGR&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;SUM&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="l data"&gt;a&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="l data"&gt;a&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2019 03:44:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/563952#M158146</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-06-06T03:44:36Z</dc:date>
    </item>
    <item>
      <title>Re: %DO %UNTIL in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/563967#M158152</link>
      <description>&lt;P&gt;Before using macro code, you need tested and error-free code. There is hardly any reason to use macro-code at all, except for automation or process-flow-control. The statement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%DO %until (&amp;amp;valor=&amp;amp;valor);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will always be true, so your loop stops after the first iteration.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your problem seems to be solvable with a single select-statement, maybe using having. But i need to see what you have and what you expect as result to suggest code.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2019 05:10:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/563967#M158152</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-06-06T05:10:39Z</dc:date>
    </item>
    <item>
      <title>Re: %DO %UNTIL in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/564175#M158222</link>
      <description>&lt;P&gt;Sorry for the mess.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've got this manager with a bugdet of $861,512,982.00&amp;nbsp; and another table with list of clients and their balances&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TABLE A&lt;/P&gt;&lt;P&gt;M1 Budget=&amp;nbsp;$861,512,982.00&lt;/P&gt;&lt;P&gt;MID = 952&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TABLE B&lt;/P&gt;&lt;P&gt;MID = 952&lt;/P&gt;&lt;P&gt;C1 BAL=1,590&lt;/P&gt;&lt;P&gt;C2 BAL=2,590&lt;/P&gt;&lt;P&gt;C3 BAL=150,000.00&lt;/P&gt;&lt;P&gt;C4 BAL=500,000.00&lt;/P&gt;&lt;P&gt;....................................&lt;/P&gt;&lt;P&gt;And I need to sum the balances of the clients until the total sum reach&amp;nbsp; or its near to 861,512,982.00, and the two tables only joined by MID column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this help to explain what I am looking for.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2019 15:48:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/564175#M158222</guid>
      <dc:creator>rodrichiez</dc:creator>
      <dc:date>2019-06-06T15:48:15Z</dc:date>
    </item>
    <item>
      <title>Re: %DO %UNTIL in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/564185#M158224</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/150360"&gt;@rodrichiez&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Sorry for the mess.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've got this manager with a bugdet of $861,512,982.00&amp;nbsp; and another table with list of clients and their balances&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;TABLE A&lt;/P&gt;
&lt;P&gt;M1 Budget=&amp;nbsp;$861,512,982.00&lt;/P&gt;
&lt;P&gt;MID = 952&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;TABLE B&lt;/P&gt;
&lt;P&gt;MID = 952&lt;/P&gt;
&lt;P&gt;C1 BAL=1,590&lt;/P&gt;
&lt;P&gt;C2 BAL=2,590&lt;/P&gt;
&lt;P&gt;C3 BAL=150,000.00&lt;/P&gt;
&lt;P&gt;C4 BAL=500,000.00&lt;/P&gt;
&lt;P&gt;....................................&lt;/P&gt;
&lt;P&gt;And I need to sum the balances of the clients until the total sum reach&amp;nbsp; or its near to 861,512,982.00, and the two tables only joined by MID column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this help to explain what I am looking for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Sounds like you mean you want to allocate your budget to clients until you reach 80% of the budgeted amount.&amp;nbsp; What about the rest of the projects/clients for that manager?&amp;nbsp; &amp;nbsp;Is there any order that you want pick the "winners" in the assignment?&amp;nbsp; Is there a date variable to use?&amp;nbsp; Do you want to add them largest to smallest?&amp;nbsp; What happens if the next client would push the total over 80% (or over 100%)?&amp;nbsp; Do you want to just skip that one and try the next one?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;SQL is probably NOT the right tool for this.&amp;nbsp; There are optimization procedures that SAS has (if you have the right products licensed.)&lt;/P&gt;
&lt;P&gt;Otherwise just do it with a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2019 16:18:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/564185#M158224</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-06-06T16:18:23Z</dc:date>
    </item>
    <item>
      <title>Re: %DO %UNTIL in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/564264#M158255</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are my answers:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;What about the rest of the projects/clients for that manager?&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I just need the clients that represents the 80% of the budget.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;Is there any order that you want pick the "winners" in the assignment?&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The ones that has the greater current balance.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Is there a date variable to use?&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I filtered the table to the last available date.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Do you want to add them largest to smallest?&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Yes.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;What happens if the next client would push the total over 80% (or over 100%)?&amp;nbsp; Do you want to just skip that one and try the next one?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;It doesn't matter if the 80% percent is in one or two clients, we want to know how many customers represent that percentage, but the total sum can't exceed the 80% of the budget for that manager.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I tried with data step as you said but I'm not very familiar with.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2019 18:55:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/564264#M158255</guid>
      <dc:creator>rodrichiez</dc:creator>
      <dc:date>2019-06-06T18:55:04Z</dc:date>
    </item>
    <item>
      <title>Re: %DO %UNTIL in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/564296#M158269</link>
      <description>&lt;P&gt;Doesn't look very hard to me. Let's adjust your sample data so that the total costs are more in line with the budget.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
 input mid budget;
cards;
952 800000.00
;
data B;
  input mid client $ cost ;
cards;
952 C1 1590
952 C2 2590
952 C3 150000
952 C4 500000
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So just combine the two so you can see the budget value and generate a running total.&amp;nbsp; Then you can check if the running total has exceeded any fraction of the budget you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge a b ;
  by mid;
  running_total+cost;
  if first.mid then running_total=cost;
  percent = 100*running_total/budget;
  over = percent &amp;gt;= 80 ;
run;

proc print;
 format budget cost running_total comma14.2 ;
 format percent 7.2 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;                                                     running_
Obs    mid      budget      client       cost         total       percent    over

 1     952    800,000.00      C1        1,590.00      1,590.00      0.20       0
 2     952    800,000.00      C2        2,590.00      4,180.00      0.52       0
 3     952    800,000.00      C3      150,000.00    154,180.00     19.27       0
 4     952    800,000.00      C4      500,000.00    654,180.00     81.77       1&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Jun 2019 20:35:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/564296#M158269</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-06-06T20:35:54Z</dc:date>
    </item>
    <item>
      <title>Re: %DO %UNTIL in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/564436#M158323</link>
      <description>&lt;P&gt;Thank you Tom.&lt;BR /&gt;I need to learn more about Data Step.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2019 11:44:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/DO-UNTIL-in-PROC-SQL/m-p/564436#M158323</guid>
      <dc:creator>rodrichiez</dc:creator>
      <dc:date>2019-06-07T11:44:56Z</dc:date>
    </item>
  </channel>
</rss>

