<?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: Use previous value with a maximum cap of each ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-previous-value-with-a-maximum-cap-of-each-ID/m-p/527222#M143690</link>
    <description>Thanks a lot for your reply! The code works really well!</description>
    <pubDate>Tue, 15 Jan 2019 06:19:41 GMT</pubDate>
    <dc:creator>saf_nadia</dc:creator>
    <dc:date>2019-01-15T06:19:41Z</dc:date>
    <item>
      <title>Use previous value with a maximum cap of each ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-previous-value-with-a-maximum-cap-of-each-ID/m-p/527211#M143683</link>
      <description>I have data like this:&lt;BR /&gt;Id date cb&lt;BR /&gt;X 1jan 27&lt;BR /&gt;Y 1jan 47&lt;BR /&gt;X 2jan 10&lt;BR /&gt;Y 2jan 13&lt;BR /&gt;X 3jan 37&lt;BR /&gt;&lt;BR /&gt;Each Id have a cap of 50 for total cb (let cb2)&lt;BR /&gt;I want an output to be like this:&lt;BR /&gt;&lt;BR /&gt;Id date Cb Cb2&lt;BR /&gt;X 1jan 27 27&lt;BR /&gt;Y 1jan 47 47&lt;BR /&gt;X 2jan 10 10&lt;BR /&gt;Y 2jan 13 3&lt;BR /&gt;X 3jan 37 13&lt;BR /&gt;&lt;BR /&gt;Hope you guys can help&lt;BR /&gt;</description>
      <pubDate>Tue, 15 Jan 2019 04:50:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-previous-value-with-a-maximum-cap-of-each-ID/m-p/527211#M143683</guid>
      <dc:creator>saf_nadia</dc:creator>
      <dc:date>2019-01-15T04:50:34Z</dc:date>
    </item>
    <item>
      <title>Re: Use previous value with a maximum cap of each ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-previous-value-with-a-maximum-cap-of-each-ID/m-p/527212#M143684</link>
      <description>&lt;P&gt;I think I understand that new variable CB2 should never exceed 50, but I don't understand any other rule you have in mind for CB2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What are your rules of constructing CB2?&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jan 2019 05:00:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-previous-value-with-a-maximum-cap-of-each-ID/m-p/527212#M143684</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-01-15T05:00:36Z</dc:date>
    </item>
    <item>
      <title>Re: Use previous value with a maximum cap of each ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-previous-value-with-a-maximum-cap-of-each-ID/m-p/527213#M143685</link>
      <description>I need cb2 to calculate the total of the whole dataset so that the total does not exceed the limit (let say the total of all cb2 should less than 100).</description>
      <pubDate>Tue, 15 Jan 2019 05:04:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-previous-value-with-a-maximum-cap-of-each-ID/m-p/527213#M143685</guid>
      <dc:creator>saf_nadia</dc:creator>
      <dc:date>2019-01-15T05:04:40Z</dc:date>
    </item>
    <item>
      <title>Re: Use previous value with a maximum cap of each ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-previous-value-with-a-maximum-cap-of-each-ID/m-p/527215#M143686</link>
      <description>&lt;P&gt;You mean, something like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Id $ date :date9. cb;
format date date5.;
datalines;
X 1jan2019 27
Y 1jan2019 47
X 2jan2019 10
Y 2jan2019 13
X 3jan2019 37
;

proc sort data=have; by id date; run;

%let limit=50;

data want;
s = 0;
do until(last.id);
    set have; by id;
    cb2 = max(0, min(cb, &amp;amp;limit. - s)); 
    s = s + cb;
    output;
    end;
drop s;
run;

proc sort data=want; by date id; run;

proc print data=want noobs; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Jan 2019 05:40:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-previous-value-with-a-maximum-cap-of-each-ID/m-p/527215#M143686</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-01-15T05:40:14Z</dc:date>
    </item>
    <item>
      <title>Re: Use previous value with a maximum cap of each ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-previous-value-with-a-maximum-cap-of-each-ID/m-p/527217#M143688</link>
      <description>&lt;P&gt;OK, I get it now.&amp;nbsp; For each ID, you want to maintain a cumulative sum of CB.&amp;nbsp; As long as the sum is &amp;lt;= 50, CB2=CB. But if it exceeds 50, then CB2 is the value that brings the sum to exactly 50.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your data were sorted by ID/DATE, you could just keep a cumulative sum (CBSUM) to guide your assignment of CB2.Then resort to original order (by DATE/ID):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Id $ date date9. cb;
format date date9.;
datalines;
X 1jan2018 27
Y 1jan2018 47
X 2jan2018 10
Y 2jan2018 13
X 3jan2018 37
Y 3jan2018 11
run;


proc sort data=have; 
  by id date;
run;
data want (drop=cbsum);
  set have;
  by id;
  if first.id then cbsum=0;
  if cbsum&amp;gt;50 then cb2=0;
  else cb2=min(50-cbsum,cb);
  cbsum+cb;
run;
proc sort;
  by date id;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that CBSUM is retained across observations, due to the "summing statement" ( CBSUM+CB).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to avoid sorting&amp;nbsp;then you could keep a CBSUM value for each ID, in a hash object:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=cbsum);
  set have;
  if _n_=1 then do;
    declare hash h ();
      h.definekey('id');
      h.definedata('cbsum');
      h.definedone();
  end;
  if h.find()^=0 then cbsum=0;
  if cbsum&amp;gt;50 then cb2=0;
  else cb2=min(50-cbsum,cb);
  cbsum+cb;
  h.replace();
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Jan 2019 05:52:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-previous-value-with-a-maximum-cap-of-each-ID/m-p/527217#M143688</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-01-15T05:52:13Z</dc:date>
    </item>
    <item>
      <title>Re: Use previous value with a maximum cap of each ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-previous-value-with-a-maximum-cap-of-each-ID/m-p/527222#M143690</link>
      <description>Thanks a lot for your reply! The code works really well!</description>
      <pubDate>Tue, 15 Jan 2019 06:19:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-previous-value-with-a-maximum-cap-of-each-ID/m-p/527222#M143690</guid>
      <dc:creator>saf_nadia</dc:creator>
      <dc:date>2019-01-15T06:19:41Z</dc:date>
    </item>
  </channel>
</rss>

