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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 1 reply
  • 693 views
  • 1 like
  • 2 in conversation