DATA Step, Macro, Functions and more

Do Loop?

Accepted Solution Solved
Reply
New User
Posts: 1
Accepted Solution

Do Loop?

I'm wondering how I would write a do loop to take care of each observation:

Call this dataset "above":

N     Date     Amount     Term

1     1/10/15     120          12

2     2/1/15     60          12

I originally had:

data new;

set above;

do until(months=term);

months+1;

deferred_rev = amount/term;

deferred_date=intnx('month',date,months-1,'same');

output;

run;

But, I want it to do this for each transaction to defer the revenue... in this event the months kept counting and it didn't treat each row as the subset.  I have a feeling this needs to be a nested iterative do loop...

The term represents how many months to the next billing event, however that means the total amount needs to be split over 12 months in this case.  The output ideally would look like:

N          Date          Amount          Term          Deferred_Date     Months         Deferred_Rev        

1          1/10/15          120               12               1/10                    1               10

2          1/10/15          120               12               2/10                    2              10

3

4

5

...

13     2/1/15          60                    12               2/1/15               1               5

14     2/1/15          60                    12               3/1/15               2               5

...


Accepted Solutions
Solution
‎02-20-2015 08:45 AM
Super User
Super User
Posts: 6,500

Re: Do Loop?

Just use a normal DO loop .

data above ;

  informat date mmddyy10. ;

  format date yymmdd10. ;

  input id Date Amount Term;

cards;

1     1/10/15     120          12

2     2/1/15     60          12

;;;;

data new;

  set above;

  do month = 1 to term ;

    deferred_rev = amount/term;

    deferred_date=intnx('month',date,month-1,'same');

    output;

  end;

run;

proc print; run;

Note: I removed the S from the loop counter MONTH to avoid confusion on meaning since it is the month number and not the number of months.

View solution in original post


All Replies
Solution
‎02-20-2015 08:45 AM
Super User
Super User
Posts: 6,500

Re: Do Loop?

Just use a normal DO loop .

data above ;

  informat date mmddyy10. ;

  format date yymmdd10. ;

  input id Date Amount Term;

cards;

1     1/10/15     120          12

2     2/1/15     60          12

;;;;

data new;

  set above;

  do month = 1 to term ;

    deferred_rev = amount/term;

    deferred_date=intnx('month',date,month-1,'same');

    output;

  end;

run;

proc print; run;

Note: I removed the S from the loop counter MONTH to avoid confusion on meaning since it is the month number and not the number of months.

☑ This topic is SOLVED.

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

Discussion stats
  • 1 reply
  • 208 views
  • 1 like
  • 2 in conversation