<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: help with loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/help-with-loop/m-p/844038#M333694</link>
    <description>&lt;P&gt;You need to make a new variable to keep the running total so you can RETAIN it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;       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
&lt;/PRE&gt;</description>
    <pubDate>Mon, 14 Nov 2022 01:00:57 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-11-14T01:00:57Z</dc:date>
    <item>
      <title>help with loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-with-loop/m-p/844034#M333692</link>
      <description>&lt;P&gt;I have an input dataset like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data input_data;
input Customer_num Total_bads;
cards;
19 60
10 60
25 60&lt;BR /&gt;20 60&lt;BR /&gt;30 60&lt;BR /&gt;15&amp;nbsp;60&lt;BR /&gt;;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to get the following result:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;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
;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;SPAN&gt;For every observation I want current_bads =customer_num and total_bads is deducted by the number of current_bads until total_bads =0.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Should I use loop? Can anyone help me with this?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Nov 2022 22:48:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-with-loop/m-p/844034#M333692</guid>
      <dc:creator>hunluuu947</dc:creator>
      <dc:date>2022-11-13T22:48:38Z</dc:date>
    </item>
    <item>
      <title>Re: help with loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/help-with-loop/m-p/844038#M333694</link>
      <description>&lt;P&gt;You need to make a new variable to keep the running total so you can RETAIN it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;       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
&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Nov 2022 01:00:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/help-with-loop/m-p/844038#M333694</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-11-14T01:00:57Z</dc:date>
    </item>
  </channel>
</rss>

