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
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.
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.
Thanks Anca - much appreciated !
%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);
Thanks Mohamed - good one - but I think I'll stay away from arrays for the time being 🙂
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;
Thanks RW9, another good solution
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.