BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
alan0101
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
AncaTilea
Pyrite | Level 9

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

7 REPLIES 7
AncaTilea
Pyrite | Level 9

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.

alan0101
Obsidian | Level 7

Thanks Anca - much appreciated !

mohamed_zaki
Barite | Level 11

%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);

alan0101
Obsidian | Level 7

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

alan0101
Obsidian | Level 7

Thanks RW9, another good solution

Ksharp
Super User
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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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