<?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: Find factor that reduce amount by defined amount in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600318#M173489</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;The metric to calculate the value of new "Mis" field for each customer&amp;nbsp; is&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;&lt;SPAN class="token function"&gt;New_Mis=min&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;Use&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;Factor&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;Mis&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/LI-CODE&gt;
&lt;P&gt;Where can I see this formula in your code please?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;My code focuses on finding the desired value of variable &lt;FONT face="courier new,courier"&gt;Factor&lt;/FONT&gt;. You can then use this value in another DATA (or PROC SQL) step to compute &lt;FONT face="courier new,courier"&gt;New_Mis&lt;/FONT&gt; with your formula. You had done this already, so I assumed that you don't need help with this step. To quote your original post:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:
&lt;P&gt;The question is how to find the factor that will&amp;nbsp; reduce&amp;nbsp; SUM of MIS&amp;nbsp; by 200 .&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The intended reduction (200) is stored in macro variable &lt;FONT face="courier new,courier"&gt;threshold&lt;/FONT&gt; in my code. The factor in question is eventually the only value in dataset WANT. You can apply your macro &lt;FONT face="courier new,courier"&gt;mmacro&lt;/FONT&gt; to confirm that (or: to &lt;EM&gt;check if&lt;/EM&gt;) this factor does solve your problem (and I would recommend doing so). The output dataset of&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30622"&gt;@gamotte&lt;/a&gt;'s code (HISTORY) is more informative as it contains other interesting quantities as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&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;P&gt;The factor should be apply on "Use" field and not on "Mis" field&lt;/P&gt;
&lt;P&gt;In the code that you sent&amp;nbsp; I think that by mistake factor is applied on "Mis" field&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I'm not sure where you see this. The only formula containing &lt;FONT face="courier new,courier"&gt;factor&lt;/FONT&gt; in my code is&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;factor=(s-&amp;amp;threshold)/t;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;where &lt;FONT face="courier new,courier"&gt;s&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;t&lt;/FONT&gt; are certain (partial) sums of &lt;FONT face="courier new,courier"&gt;Mis&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;Use&lt;/FONT&gt; values, respectively. You could rewrite this formula (mathematically) as&lt;/P&gt;
&lt;PRE&gt;s - factor*t = &amp;amp;threshold&lt;/PRE&gt;
&lt;P&gt;Now you see that &lt;FONT face="courier new,courier"&gt;factor&lt;/FONT&gt; is applied to &lt;FONT face="courier new,courier"&gt;t&lt;/FONT&gt;, i.e., to a sum of &lt;STRONG&gt;&lt;EM&gt;&lt;FONT face="courier new,courier"&gt;Use&lt;/FONT&gt;&lt;/EM&gt;&lt;/STRONG&gt; values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for providing more background information about your question. I guess that among a million customers some didn't use their credit card at all, so &lt;FONT face="courier new,courier"&gt;Use&lt;/FONT&gt; would be zero for them, right? As mentioned previously, my current code cannot handle this case appropriately because it computes ratios with &lt;FONT face="courier new,courier"&gt;Use&lt;/FONT&gt; in the denominator. As a quick fix I suggest the following changes to the SELECT statement in the PROC SQL step creating view &lt;FONT face="courier new,courier"&gt;vtemp&lt;/FONT&gt;:&lt;/P&gt;
&lt;PRE&gt;select mis, &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;max(use,1e-50) as&lt;/STRONG&gt;&lt;/FONT&gt; use, round(mis/&lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;calculated&lt;/FONT&gt;&lt;/STRONG&gt; use,1e-12) as r&lt;/PRE&gt;
&lt;P&gt;That is, &lt;FONT face="courier new,courier"&gt;use=0&lt;/FONT&gt; would effectively be replaced by a minuscule &lt;FONT face="courier new,courier"&gt;use=10**-50&lt;/FONT&gt; in the calculation, so that the division doesn't fail. It is still assumed that &lt;EM&gt;negative&lt;/EM&gt; values of &lt;FONT face="courier new,courier"&gt;use&lt;/FONT&gt; do not occur.&lt;/P&gt;</description>
    <pubDate>Wed, 30 Oct 2019 10:35:06 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2019-10-30T10:35:06Z</dc:date>
    <item>
      <title>Find factor that reduce amount by defined amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600013#M173325</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I would like to ask a question please.&lt;/P&gt;
&lt;P&gt;I have a data set with 3 fields: ID,Mis,Use&lt;/P&gt;
&lt;P&gt;I need to calculate a new field called&amp;nbsp;&amp;nbsp;Mis_New by following metric:&lt;/P&gt;
&lt;P&gt;Mis_New= min(Use*Factor,Mis);&lt;/P&gt;
&lt;P&gt;The question is how to find the factor that will&amp;nbsp; reduce&amp;nbsp; SUM of MIS&amp;nbsp; by 200 .&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;If I use Factor=1.2&amp;nbsp; then I will reduce&amp;nbsp;SUM of MIS&amp;nbsp; from 5780&amp;nbsp; to&amp;nbsp; 5330 (difference is 450...but required difference is 200)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any idea?&lt;/P&gt;
&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
Data tttbl1;
input ID  Mis  Use;
cards;
1 100 90
2 150 130
3 800 300
4 250 200
5 1100 1000
6 950 900
7 890 800
8 250 240
9 590 530
10 700 600
;
Run;


Data tttbl2;
SET tttbl1;
Mis_New= min((Use*1.2),Mis);
Run;



PROC SQL;
	create table summary1  as
	select sum(Mis) as Sum_Mis ,
		   sum(Mis_New) as Sum_Mis_New,
		   calculated Sum_Mis-calculated Sum_Mis_New as dif
	from tttbl2 
;
QUIT;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt; &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 11:48:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600013#M173325</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-10-29T11:48:16Z</dc:date>
    </item>
    <item>
      <title>Re: Find factor that reduce amount by defined amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600021#M173329</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you are looking for something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   if _n_ = 1 then do until(eof);
      set tttbl1 end = eof;
      sum + Mis;
   end;
   fac = (sum - 200) / sum;
   do until(last);
      set tttbl1 end = last;
      new = min(Use, ceil(Mis * fac));
      output;
   end;
drop fac sum;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Oct 2019 11:14:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600021#M173329</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-10-29T11:14:43Z</dc:date>
    </item>
    <item>
      <title>Re: Find factor that reduce amount by defined amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600022#M173330</link>
      <description>&lt;P&gt;Thank you&lt;/P&gt;
&lt;P&gt;I run your code but I don't find the answer.....what is the factor value that reduce by 200?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 11:22:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600022#M173330</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-10-29T11:22:24Z</dc:date>
    </item>
    <item>
      <title>Re: Find factor that reduce amount by defined amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600026#M173332</link>
      <description>&lt;P&gt;Is there a more clever way to do it?&lt;/P&gt;
&lt;P&gt;I found that factor=2 is the solution.&lt;/P&gt;
&lt;P&gt;Is there a better way to run the simulation?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
Data tttbl1;
input ID  Mis  Use;
cards;
1 100 90
2 150 130
3 800 300
4 250 200
5 1100 1000
6 950 900
7 890 800
8 250 240
9 590 530
10 700 600
;
Run;


 %macro mmacro(factor,run);
Data tttbl2_&amp;amp;run.;
SET tttbl1;
Mis_New= min((Use*&amp;amp;factor.),Mis);
Run;


PROC SQL;
	create table SummaryData&amp;amp;run.  as
	select &amp;amp;factor. as factor,
	       &amp;amp;run. as run,
           sum(Mis) as Sum_Mis ,
		   sum(Mis_New) as Sum_Mis_New,
		   calculated Sum_Mis-calculated Sum_Mis_New as Reduced_amount
	from tttbl2_&amp;amp;run. 
;
QUIT;
%mend;

%mmacro(1.05,1);
%mmacro(1.1,2);
%mmacro(1.15,3);
%mmacro(1.2,4);
%mmacro(1.25,5);
%mmacro(1.3,6);
%mmacro(1.4,7);
%mmacro(1.45,8);
%mmacro(1.5,9);
%mmacro(1.55,10);
%mmacro(1.6,11);
%mmacro(1.65,12);
%mmacro(1.7,13);
%mmacro(1.75,14);
%mmacro(1.8,15);
%mmacro(1.85,16);
%mmacro(1.9,17);
%mmacro(1.95,18);
%mmacro(2.0,19);
%mmacro(2.05,20);
%mmacro(2.1,21);



Data Simulations;
Set SummaryData:;
By factor;
Run;



 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Oct 2019 11:42:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600026#M173332</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-10-29T11:42:40Z</dc:date>
    </item>
    <item>
      <title>Re: Find factor that reduce amount by defined amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600072#M173362</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Edit&lt;/STRONG&gt; Removed my second answer as i am not sure it really made sense.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like this ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mmacro(factor,run);

    data tttbl2_&amp;amp;run.;
        set tttbl1;
        Mis_New= min((Use*&amp;amp;factor.),Mis);
        sum_Mis+Mis;
        sum_Mis_New+Mis_New;
        Reduced_amount=sum_Mis-sum_Mis_New;
        call symputx("Reduced_amount",Reduced_amount,"G");
    run;

%mend;

%let MAXRUN=100;
%let TARGET=200;
%let PREC=1.0e-06;

Data tttbl1;
input ID  Mis  Use;
cards;
1 100 90
2 150 130
3 800 300
4 250 200
5 1100 1000
6 950 900
7 890 800
8 250 240
9 590 530
10 700 600
;
Run;

data history;
    stop=0;
    
    minfactor=1; maxfactor=10;

    do run=1 by 1 while(not stop and run&amp;lt;&amp;amp;MAXRUN.);

        if run=&amp;amp;MAXRUN. then put "WARNING: maximum number of runs reached";
        
        factor=(minfactor+maxfactor)/2;

        rc=dosubl(cats('%mmacro(',factor,',',run,');'));

        Reduced_amount=symget("Reduced_amount");

        put Reduced_amount=;

        if abs(Reduced_amount-&amp;amp;TARGET.)&amp;lt;&amp;amp;PREC. then stop=1;
        else if Reduced_amount&amp;lt;&amp;amp;TARGET. then do;
            action="The reduced amount is too small =&amp;gt; we decrease the factor";
            maxfactor_old=maxfactor;
            maxfactor=factor;
        end;
        else do;
            action="The reduced amount is too big =&amp;gt; we increase the factor";
            minfactor_old=minfactor;
            minfactor=factor;
        end;

        output;
    end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 15:37:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600072#M173362</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-10-29T15:37:09Z</dc:date>
    </item>
    <item>
      <title>Re: Find factor that reduce amount by defined amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600082#M173365</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I missed your specification.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 14:43:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600082#M173365</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-10-29T14:43:47Z</dc:date>
    </item>
    <item>
      <title>Re: Find factor that reduce amount by defined amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600099#M173374</link>
      <description>Is this reduce by 200 related to a specific value of Use or in general or given the value of Use?&lt;BR /&gt;&lt;BR /&gt;The formula doesn't appear complicated and I would assume that there may be a mathematical solution using some basic algebra, although simulations will work.</description>
      <pubDate>Tue, 29 Oct 2019 15:31:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600099#M173374</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-10-29T15:31:22Z</dc:date>
    </item>
    <item>
      <title>Re: Find factor that reduce amount by defined amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600103#M173377</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now that you've got an algorithm&amp;nbsp;&lt;EM&gt;approximating&lt;/EM&gt; the factor &lt;EM&gt;numerically&lt;/EM&gt;, let me add an approach aiming at a &lt;EM&gt;direct calculation&lt;/EM&gt;&amp;nbsp;(good point,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;!):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let threshold=200;

proc sql;
create view vtemp as
select mis, use, round(mis/use,1e-12) as r
from tttbl1
order by r;

create table totals as
select sum(mis) as s, sum(use) as t
from tttbl1;
quit;

data want(keep=factor);
if _n_=1 then set totals;
do until(last.r);
  set vtemp;
  by r;
  if first.r &amp;amp; s-r*t&amp;lt;=&amp;amp;threshold then do;
    factor=(s-&amp;amp;threshold)/t;
    output;
    stop;
  end;
  s+(-mis);
  t+(-use);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: Modifications would be necessary if &lt;FONT face="courier new,courier"&gt;use=0&lt;/FONT&gt; was a possible value in dataset TTTBL1.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 15:47:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600103#M173377</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-10-29T15:47:35Z</dc:date>
    </item>
    <item>
      <title>Re: Find factor that reduce amount by defined amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600111#M173380</link>
      <description>I can only give you one like unfortunately.</description>
      <pubDate>Tue, 29 Oct 2019 16:20:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600111#M173380</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-10-29T16:20:12Z</dc:date>
    </item>
    <item>
      <title>Re: Find factor that reduce amount by defined amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600114#M173383</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30622"&gt;@gamotte&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I can only give you one like unfortunately.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Thanks! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Honestly, I'm not quite satisfied with my solution. I mentioned "&lt;EM&gt;direct calculation&lt;/EM&gt;," but this was actually an overstatement: In fact, the algorithm steps through the vertices of the graph of a convex, decreasing, piecewise linear function (from left to right) until the graph crosses the threshold. Only then a direct calculation is performed (intersection of the respective line segment with the horizontal threshold line). I haven't tested the program yet on a large dataset where your nice approximation might be more efficient because it doesn't care about the line segments, but just the function values. Also, as mentioned, the current version of my program wouldn't work if &lt;FONT face="courier new,courier"&gt;use=0&lt;/FONT&gt; for some observation.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 16:41:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600114#M173383</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-10-29T16:41:49Z</dc:date>
    </item>
    <item>
      <title>Re: Find factor that reduce amount by defined amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600249#M173451</link>
      <description>&lt;P&gt;The metric to calculate the value of new "Mis" field for each customer&amp;nbsp; is&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token function"&gt;New_Mis=min&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;Use&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;Factor&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;Mis&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Where can I see this formula in your code please?&lt;/P&gt;
&lt;P&gt;The factor should be apply on "Use" field and not on "Mis" field&lt;/P&gt;
&lt;P&gt;In the code that you sent&amp;nbsp; I think that by mistake factor is applied on "Mis" field&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2019 05:36:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600249#M173451</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-10-30T05:36:00Z</dc:date>
    </item>
    <item>
      <title>Re: Find factor that reduce amount by defined amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600253#M173452</link>
      <description>&lt;P&gt;Sorry,&lt;/P&gt;
&lt;P&gt;I don't see where you end the macro that you defined and don't see where you run it.&lt;/P&gt;
&lt;P&gt;The metric to calculate the value of new "Mis" field for each customer&amp;nbsp; is&amp;nbsp;&lt;/P&gt;
&lt;PRE class="  language-sas"&gt;&lt;CODE class="  language-sas"&gt;New_Mis&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;min&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;Use&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token procnames"&gt;Factor&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;Mis&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Where did you use it in the code please?&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2019 05:15:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600253#M173452</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-10-30T05:15:35Z</dc:date>
    </item>
    <item>
      <title>Re: Find factor that reduce amount by defined amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600259#M173453</link>
      <description>&lt;P&gt;The issue is like that.&lt;/P&gt;
&lt;P&gt;Customers in bank has :&lt;/P&gt;
&lt;P&gt;CREDIT CARD LINE&amp;nbsp; (This is the maximum amount that they are allowed to use).&lt;/P&gt;
&lt;P&gt;CREDIT USE (This is the actual amount that customer use in credit card).&lt;/P&gt;
&lt;P&gt;As you understand&amp;nbsp;CREDIT USE should be most of times lower than&amp;nbsp;CREDIT CARD LINE.&lt;/P&gt;
&lt;P&gt;Let's say that&amp;nbsp; in the bank there are 1 million customers and total&amp;nbsp;CREDIT CARD LINE is 10&amp;nbsp;billions Dollar.&lt;/P&gt;
&lt;P&gt;Now let's say that there is requirement to reduce Total&amp;nbsp;CREDIT CARD LINE amount by 2&amp;nbsp;&amp;nbsp;billions Dollar.&lt;/P&gt;
&lt;P&gt;Let's say that the metric that was agreed is that for each customer in the bank the NEW CREDIT CARD LINE will be calculated by:&lt;/P&gt;
&lt;PRE class="  language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token operator"&gt;NEW CREDIT CARD LINE=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;min&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(CREDIT USE&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token procnames"&gt;Factor&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;Current_CREDIT_CARD_LINE&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The target is to calculate the Factor that give us the desired reduce of&amp;nbsp;2&amp;nbsp;&amp;nbsp;billions Dollar.&lt;/P&gt;
&lt;P&gt;Moreover, better to have a chart that show us for each value of factor what will be the reduced amount&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;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2019 05:25:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600259#M173453</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-10-30T05:25:04Z</dc:date>
    </item>
    <item>
      <title>Re: Find factor that reduce amount by defined amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600288#M173463</link>
      <description>&lt;P&gt;I don't know about efficiency but it surely seems clever to me the way you recompute the reduced amount&lt;/P&gt;
&lt;P&gt;variable at each step without having to re-read the whole dataset.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2019 08:54:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600288#M173463</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-10-30T08:54:38Z</dc:date>
    </item>
    <item>
      <title>Re: Find factor that reduce amount by defined amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600293#M173466</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro only contains one data step that compute the reduced amount for a given factor :&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;%macro mmacro(factor,run);

    data tttbl2_&amp;amp;run.;
        set tttbl1;
        Mis_New= min((Use*&amp;amp;factor.),Mis);
        sum_Mis+Mis;
        sum_Mis_New+Mis_New;
        Reduced_amount=sum_Mis-sum_Mis_New;
        call symputx("Reduced_amount",Reduced_amount,"G");
    run;

%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can see that it contains your formula&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;Mis_New= min((Use*&amp;amp;factor.),Mis);&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This macro is then used in the data history step, inside the do loop, for each tried value of the factor :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;rc&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;dosubl&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;cats&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'%mmacro('&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token procnames"&gt;factor&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;','&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;');'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dosubl is used since the reduced amount is computed in a separate data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2019 09:02:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600293#M173466</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-10-30T09:02:30Z</dc:date>
    </item>
    <item>
      <title>Re: Find factor that reduce amount by defined amount</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600318#M173489</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;The metric to calculate the value of new "Mis" field for each customer&amp;nbsp; is&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;&lt;SPAN class="token function"&gt;New_Mis=min&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;Use&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;Factor&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;Mis&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/LI-CODE&gt;
&lt;P&gt;Where can I see this formula in your code please?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;My code focuses on finding the desired value of variable &lt;FONT face="courier new,courier"&gt;Factor&lt;/FONT&gt;. You can then use this value in another DATA (or PROC SQL) step to compute &lt;FONT face="courier new,courier"&gt;New_Mis&lt;/FONT&gt; with your formula. You had done this already, so I assumed that you don't need help with this step. To quote your original post:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:
&lt;P&gt;The question is how to find the factor that will&amp;nbsp; reduce&amp;nbsp; SUM of MIS&amp;nbsp; by 200 .&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The intended reduction (200) is stored in macro variable &lt;FONT face="courier new,courier"&gt;threshold&lt;/FONT&gt; in my code. The factor in question is eventually the only value in dataset WANT. You can apply your macro &lt;FONT face="courier new,courier"&gt;mmacro&lt;/FONT&gt; to confirm that (or: to &lt;EM&gt;check if&lt;/EM&gt;) this factor does solve your problem (and I would recommend doing so). The output dataset of&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30622"&gt;@gamotte&lt;/a&gt;'s code (HISTORY) is more informative as it contains other interesting quantities as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&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;P&gt;The factor should be apply on "Use" field and not on "Mis" field&lt;/P&gt;
&lt;P&gt;In the code that you sent&amp;nbsp; I think that by mistake factor is applied on "Mis" field&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I'm not sure where you see this. The only formula containing &lt;FONT face="courier new,courier"&gt;factor&lt;/FONT&gt; in my code is&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;factor=(s-&amp;amp;threshold)/t;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;where &lt;FONT face="courier new,courier"&gt;s&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;t&lt;/FONT&gt; are certain (partial) sums of &lt;FONT face="courier new,courier"&gt;Mis&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;Use&lt;/FONT&gt; values, respectively. You could rewrite this formula (mathematically) as&lt;/P&gt;
&lt;PRE&gt;s - factor*t = &amp;amp;threshold&lt;/PRE&gt;
&lt;P&gt;Now you see that &lt;FONT face="courier new,courier"&gt;factor&lt;/FONT&gt; is applied to &lt;FONT face="courier new,courier"&gt;t&lt;/FONT&gt;, i.e., to a sum of &lt;STRONG&gt;&lt;EM&gt;&lt;FONT face="courier new,courier"&gt;Use&lt;/FONT&gt;&lt;/EM&gt;&lt;/STRONG&gt; values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for providing more background information about your question. I guess that among a million customers some didn't use their credit card at all, so &lt;FONT face="courier new,courier"&gt;Use&lt;/FONT&gt; would be zero for them, right? As mentioned previously, my current code cannot handle this case appropriately because it computes ratios with &lt;FONT face="courier new,courier"&gt;Use&lt;/FONT&gt; in the denominator. As a quick fix I suggest the following changes to the SELECT statement in the PROC SQL step creating view &lt;FONT face="courier new,courier"&gt;vtemp&lt;/FONT&gt;:&lt;/P&gt;
&lt;PRE&gt;select mis, &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;max(use,1e-50) as&lt;/STRONG&gt;&lt;/FONT&gt; use, round(mis/&lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;calculated&lt;/FONT&gt;&lt;/STRONG&gt; use,1e-12) as r&lt;/PRE&gt;
&lt;P&gt;That is, &lt;FONT face="courier new,courier"&gt;use=0&lt;/FONT&gt; would effectively be replaced by a minuscule &lt;FONT face="courier new,courier"&gt;use=10**-50&lt;/FONT&gt; in the calculation, so that the division doesn't fail. It is still assumed that &lt;EM&gt;negative&lt;/EM&gt; values of &lt;FONT face="courier new,courier"&gt;use&lt;/FONT&gt; do not occur.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2019 10:35:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-factor-that-reduce-amount-by-defined-amount/m-p/600318#M173489</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-10-30T10:35:06Z</dc:date>
    </item>
  </channel>
</rss>

