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?
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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.