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

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

...

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

1 REPLY 1
Tom
Super User Tom
Super User

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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