DATA Step, Macro, Functions and more

Macro to decrease balances by a set monthly amount

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

Macro to decrease balances by a set monthly amount

Hi

I have a dataset with the following basic structure :

AccountBalanceM1M2M3M4M5M6M7M8M9M10
1100604000000000
220060606020000000
3300606060606000000
440060606060606040000
55006060606060606060200

The average that an account pays is R60 per month.  Therefore, in row1, this customer will pay R60 in month1 (M1), and the balance of R40 in M2.  In Row2, the customer pays R60 in M1, R60 in M2, R60 in M3, and the balance of R20 in M4, and so on.  I need a macro to run all these balances out by R60 per month, for 100 months.  Any ideas would be greatly appreciated


Accepted Solutions
Solution
‎10-21-2014 07:32 AM
Super Contributor
Posts: 543

Re: Macro to decrease balances by a set monthly amount

Hi,

How about this?

(NOTE: it is not a macro, but seems to take care of what you need, unless I completely misunderstood)

*start with account # and balance associated;

data have;

input account balance;

datalines;

1 1000

2 2000

3 3000

4 4000

5 6000

;

run;

*create 100 months;

data tmp;

    set have;

    do i = 1 to 100;

    month = i;output;

    end;drop i;

run;

%let monthlyPay = 60;

data want;

    set tmp;

    by account;

    retain result 0;

    if first.account then result = balance - &monthlyPay.;

        else result = result - &monthlyPay.;

        if result < 0 then result = 0;

run;

*make it wide ;

proc transpose data = want out = really_want (drop = _name_) prefix = mth;

by account;

id month;

var result;

run;

Best of luck,

Anca.

View solution in original post


All Replies
Solution
‎10-21-2014 07:32 AM
Super Contributor
Posts: 543

Re: Macro to decrease balances by a set monthly amount

Hi,

How about this?

(NOTE: it is not a macro, but seems to take care of what you need, unless I completely misunderstood)

*start with account # and balance associated;

data have;

input account balance;

datalines;

1 1000

2 2000

3 3000

4 4000

5 6000

;

run;

*create 100 months;

data tmp;

    set have;

    do i = 1 to 100;

    month = i;output;

    end;drop i;

run;

%let monthlyPay = 60;

data want;

    set tmp;

    by account;

    retain result 0;

    if first.account then result = balance - &monthlyPay.;

        else result = result - &monthlyPay.;

        if result < 0 then result = 0;

run;

*make it wide ;

proc transpose data = want out = really_want (drop = _name_) prefix = mth;

by account;

id month;

var result;

run;

Best of luck,

Anca.

Contributor
Posts: 40

Re: Macro to decrease balances by a set monthly amount

Posted in reply to AncaTilea

Thanks Anca - much appreciated !

Super Contributor
Posts: 490

Re: Macro to decrease balances by a set monthly amount

%macro count(AccMax);

Data new (drop=x i);

     Account=1;

     Balance=100;

     Array M[100];

       DO while (Account<=&AccMax);

            x=Balance;

            DO i=1 to 100 while (x>=0);

                 IF x >= 60 then do;

                      M=60;

                      x = x - 60;

                 END;

                 Else If x < 60 then do;

                      M=x;

                      x=0;

                 END;

            END;

            output;

            Account + 1;

            Balance + 100;

     END;

RUN;

%mend count;

%count(100);

Contributor
Posts: 40

Re: Macro to decrease balances by a set monthly amount

Posted in reply to mohamed_zaki

Thanks Mohamed - good one - but I think I'll stay away from arrays for the time being :-)

Super User
Super User
Posts: 7,977

Re: Macro to decrease balances by a set monthly amount

Hi,

Simple array processing should achieve the result:

data have;
  do account=1 to 6;
    balance=account*100;
    output;
  end;
run;

data want (drop=tmpbal i);
  set have;
  array m{100} 8.;
  tmpbal=balance;
  i=1;
  do until (tmpbal < 60);
    tmpbal=tmpbal - 60;
    m{i}=60;
    i=i+1;
  end;
  m{i}=tmpbal;
run;

Contributor
Posts: 40

Re: Macro to decrease balances by a set monthly amount

Thanks RW9, another good solution

Super User
Posts: 10,041

Re: Macro to decrease balances by a set monthly amount

data have;
input account balance;
datalines;
1 100
2 200
3 3000
4 4000
5 6000
;
run;
data want;
 set have ;
 array m{*} month1-month100;
 n=int(divide(balance,60));
 mod=mod(balance,60);
 do i=1 to n;
  m{i}=60;
 end;
 if mod ne 0 then m{n+1}=mod;
run;

Xia Keshan

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 283 views
  • 6 likes
  • 5 in conversation