<?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: SELECT first rows by SUM values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SELECT-first-rows-by-SUM-values/m-p/433360#M107424</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
if _N_ = 1 then do;
	if 0 then set have;
   declare hash h(dataset:'have(obs=0)',ordered:'y');
   h.defineKey('id');
   h.defineData('id','value');
   h.defineDone();
end;
set have;
_k=_value;_value+value;
if _value&amp;lt;=10 then h.add();
else if _value=10 then do;h.output(dataset:'want');stop;end;
else if _value&amp;gt;10 then do;value=value-_k-1;h.add();h.output(dataset:'want');stop;end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 02 Feb 2018 02:59:44 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-02-02T02:59:44Z</dc:date>
    <item>
      <title>SELECT first rows by SUM values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SELECT-first-rows-by-SUM-values/m-p/433336#M107414</link>
      <description>&lt;P&gt;Hi there!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Consider the following problem:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TableA:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="1.png" style="width: 153px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/18263iF88AE5B32A444111/image-size/large?v=v2&amp;amp;px=999" role="button" title="1.png" alt="1.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to select the first rows that the sum of values is equal 10. Therefore, the output is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="2.png" style="width: 157px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/18264i099C61822D796DCD/image-size/large?v=v2&amp;amp;px=999" role="button" title="2.png" alt="2.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Someone know how to do this in DATA SET or PROC SQL?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 00:47:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SELECT-first-rows-by-SUM-values/m-p/433336#M107414</guid>
      <dc:creator>RoberJunior</dc:creator>
      <dc:date>2018-02-02T00:47:18Z</dc:date>
    </item>
    <item>
      <title>Re: SELECT first rows by SUM values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SELECT-first-rows-by-SUM-values/m-p/433349#M107418</link>
      <description>&lt;P&gt;what if you have rows&lt;/P&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;P&gt;11&lt;/P&gt;
&lt;P&gt;?&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 02:06:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SELECT-first-rows-by-SUM-values/m-p/433349#M107418</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-02-02T02:06:50Z</dc:date>
    </item>
    <item>
      <title>Re: SELECT first rows by SUM values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SELECT-first-rows-by-SUM-values/m-p/433352#M107420</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id value;
datalines;
1 3
2 5
3 2
4 14
5 5
;

data _null_;
if _N_ = 1 then do;
	if 0 then set have;
   declare hash h(dataset:'have(obs=0)',ordered:'y');
   h.defineKey('id');
   h.defineData('id','value');
   h.defineDone();
end;
set have;
_value+value;
h.add();
if _value&amp;gt;=10 then do;h.output(dataset:'want');stop;end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Feb 2018 02:15:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SELECT-first-rows-by-SUM-values/m-p/433352#M107420</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-02-02T02:15:37Z</dc:date>
    </item>
    <item>
      <title>Re: SELECT first rows by SUM values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SELECT-first-rows-by-SUM-values/m-p/433354#M107421</link>
      <description>We change the last row value. The output will be:&lt;BR /&gt;1&lt;BR /&gt;1&lt;BR /&gt;8</description>
      <pubDate>Fri, 02 Feb 2018 02:26:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SELECT-first-rows-by-SUM-values/m-p/433354#M107421</guid>
      <dc:creator>RoberJunior</dc:creator>
      <dc:date>2018-02-02T02:26:36Z</dc:date>
    </item>
    <item>
      <title>Re: SELECT first rows by SUM values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SELECT-first-rows-by-SUM-values/m-p/433355#M107422</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
input ID VALUE;
datalines;
1 3
2 5
4 14
5 5
run;
data WANT;
  set HAVE;
  SUM+VALUE;
  if SUM&amp;gt;=10 then do;
    VALUE+-SUM+10;
    output; 
    stop;
  end;
  else output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;ID&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;VALUE&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;SUM&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;TD class="r data"&gt;8&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;22&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 02:35:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SELECT-first-rows-by-SUM-values/m-p/433355#M107422</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-02-02T02:35:17Z</dc:date>
    </item>
    <item>
      <title>Re: SELECT first rows by SUM values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SELECT-first-rows-by-SUM-values/m-p/433360#M107424</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
if _N_ = 1 then do;
	if 0 then set have;
   declare hash h(dataset:'have(obs=0)',ordered:'y');
   h.defineKey('id');
   h.defineData('id','value');
   h.defineDone();
end;
set have;
_k=_value;_value+value;
if _value&amp;lt;=10 then h.add();
else if _value=10 then do;h.output(dataset:'want');stop;end;
else if _value&amp;gt;10 then do;value=value-_k-1;h.add();h.output(dataset:'want');stop;end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Feb 2018 02:59:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SELECT-first-rows-by-SUM-values/m-p/433360#M107424</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-02-02T02:59:44Z</dc:date>
    </item>
    <item>
      <title>Re: SELECT first rows by SUM values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SELECT-first-rows-by-SUM-values/m-p/433731#M107559</link>
      <description>&lt;P&gt;Thanks for help me guys &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, I want to develop a new step. Consider this example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;TABLE 01:&lt;/STRONG&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="1.png" style="width: 235px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/18289i468883B3A8C32DFB/image-size/large?v=v2&amp;amp;px=999" role="button" title="1.png" alt="1.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the declare a &lt;STRONG&gt;variable X&lt;/STRONG&gt;&amp;nbsp;that is sum VALUES if CLASS = A. Therefore, &lt;STRONG&gt;X = 10&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then will run the code with variable X:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
if _N_ = 1 then do;
	if 0 then set have;
   declare hash h(dataset:'have(obs=0)',ordered:'y');
   h.defineKey('id');
   h.defineData('id','value');
   h.defineDone();
end;
set have;
_k=_value;_value+value;
if _value&amp;lt;=X then h.add();
else if _value=X then do;h.output(dataset:'want');stop;end;
else if _value&amp;gt;X then do;value=value-_k-1;h.add();h.output(dataset:'want');stop;end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you guys help to develop the first step, plz?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 03 Feb 2018 00:21:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SELECT-first-rows-by-SUM-values/m-p/433731#M107559</guid>
      <dc:creator>RoberJunior</dc:creator>
      <dc:date>2018-02-03T00:21:30Z</dc:date>
    </item>
  </channel>
</rss>

