Solved
Contributor
Posts: 40

# Macro to decrease balances by a set monthly amount

Hi

I have a dataset with the following basic structure :

 Account Balance M1 M2 M3 M4 M5 M6 M7 M8 M9 M10 1 100 60 40 0 0 0 0 0 0 0 0 2 200 60 60 60 20 0 0 0 0 0 0 3 300 60 60 60 60 60 0 0 0 0 0 4 400 60 60 60 60 60 60 40 0 0 0 5 500 60 60 60 60 60 60 60 60 20 0

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,

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

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.

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

## Re: Macro to decrease balances by a set monthly amount

Hi,

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

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

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

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

Super User
Posts: 9,599

## 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,787

## 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.