BookmarkSubscribeRSS Feed
hunluuu947
Calcite | Level 5

I have an input dataset like this:

data input_data;
input Customer_num Total_bads;
cards;
19 60
10 60
25 60
20 60
30 60
15 60
;

I want to get the following result:

 

 

data output;
  input Customer_num Total_bads Current_bads;
cards;
19 41 19 
10 31 10 
25  6 25  
20  0  6  
30  0  0
15  0  0
;

 

 

 For every observation I want current_bads =customer_num and total_bads is deducted by the number of current_bads until total_bads =0.

 

Should I use loop? Can anyone help me with this?

 

 

1 REPLY 1
Tom
Super User Tom
Super User

You need to make a new variable to keep the running total so you can RETAIN it.

data input_data;
input Customer_num Total_beds;
cards;
19 60
10 60
25 60
20 60
30 60
15 60
;

data want;
  set input_data;
  if _n_=1 then new_total=total_beds;
  retain new_total;
  new_beds=min(customer_num,new_total);
  new_total=max(0,new_total-customer_num);
run;

proc print;
run;
       Customer_    Total_     new_
Obs       num        beds     total    new_beds

 1         19         60        41        19
 2         10         60        31        10
 3         25         60         6        25
 4         20         60         0         6
 5         30         60         0         0
 6         15         60         0         0
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
  • 575 views
  • 0 likes
  • 2 in conversation