<?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: I have a data sorted by two variables (comp and ind), how can I sum sales by ind? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/I-have-a-data-sorted-by-two-variables-comp-and-ind-how-can-I-sum/m-p/644863#M192696</link>
    <description>Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;, very elegant!</description>
    <pubDate>Sun, 03 May 2020 18:27:02 GMT</pubDate>
    <dc:creator>xmg2088</dc:creator>
    <dc:date>2020-05-03T18:27:02Z</dc:date>
    <item>
      <title>I have a data sorted by two variables (comp and ind), how can I sum sales by ind?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-have-a-data-sorted-by-two-variables-comp-and-ind-how-can-I-sum/m-p/644846#M192690</link>
      <description>&lt;P&gt;A&amp;nbsp;&amp;nbsp; X&amp;nbsp; 8&lt;/P&gt;&lt;P&gt;A&amp;nbsp;&amp;nbsp; X&amp;nbsp;&amp;nbsp;6&lt;/P&gt;&lt;P&gt;A&amp;nbsp;&amp;nbsp; Y&amp;nbsp; 9&lt;/P&gt;&lt;P&gt;A&amp;nbsp;&amp;nbsp; Y&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;B&amp;nbsp;&amp;nbsp; X&amp;nbsp; 7&lt;/P&gt;&lt;P&gt;B&amp;nbsp;&amp;nbsp; X&amp;nbsp; 5&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I calculate a new variable that sum the same A, first X + first Y (8+9), second X + second Y (6+2). results look like this:&lt;/P&gt;&lt;P&gt;A&amp;nbsp;&amp;nbsp; (8+9=)17&lt;/P&gt;&lt;P&gt;A&amp;nbsp; (6+2=)8&lt;/P&gt;&lt;P&gt;B&amp;nbsp; ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for anyone's help.&lt;/P&gt;</description>
      <pubDate>Sun, 03 May 2020 15:28:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-have-a-data-sorted-by-two-variables-comp-and-ind-how-can-I-sum/m-p/644846#M192690</guid>
      <dc:creator>xmg2088</dc:creator>
      <dc:date>2020-05-03T15:28:17Z</dc:date>
    </item>
    <item>
      <title>Re: I have a data sorted by two variables (comp and ind), how can I sum sales by ind?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-have-a-data-sorted-by-two-variables-comp-and-ind-how-can-I-sum/m-p/644848#M192691</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/326823"&gt;@xmg2088&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am thinking about something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input comp $ ind $ value;
	datalines;
A   X  8
A   X  6
A   Y  9
A   Y  2
B   X  7
B   X  5
;
run;

data have_fl;
	set have;
	by comp ind;
	if first.ind then num_obs = 0;
	num_obs + 1;
run;

proc sort data=have_fl;
	by comp num_obs;
run;

proc means data=have_fl sum noprint;
	var value;
	by comp num_obs;
	output out=want (drop=_: num_obs) sum=sum;
run;

proc print;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="Capture d’écran 2020-05-03 à 17.45.08.png" style="width: 122px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/39010i31025D9E72A5808C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture d’écran 2020-05-03 à 17.45.08.png" alt="Capture d’écran 2020-05-03 à 17.45.08.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 03 May 2020 15:45:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-have-a-data-sorted-by-two-variables-comp-and-ind-how-can-I-sum/m-p/644848#M192691</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-05-03T15:45:25Z</dc:date>
    </item>
    <item>
      <title>Re: I have a data sorted by two variables (comp and ind), how can I sum sales by ind?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-have-a-data-sorted-by-two-variables-comp-and-ind-how-can-I-sum/m-p/644849#M192692</link>
      <description>&lt;P&gt;This works, brilliant! Thanks so much!&lt;/P&gt;</description>
      <pubDate>Sun, 03 May 2020 15:50:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-have-a-data-sorted-by-two-variables-comp-and-ind-how-can-I-sum/m-p/644849#M192692</guid>
      <dc:creator>xmg2088</dc:creator>
      <dc:date>2020-05-03T15:50:44Z</dc:date>
    </item>
    <item>
      <title>Re: I have a data sorted by two variables (comp and ind), how can I sum sales by ind?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-have-a-data-sorted-by-two-variables-comp-and-ind-how-can-I-sum/m-p/644854#M192693</link>
      <description>Awesome ! You're welcome &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/326823"&gt;@xmg2088&lt;/a&gt;</description>
      <pubDate>Sun, 03 May 2020 16:04:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-have-a-data-sorted-by-two-variables-comp-and-ind-how-can-I-sum/m-p/644854#M192693</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-05-03T16:04:08Z</dc:date>
    </item>
    <item>
      <title>Re: I have a data sorted by two variables (comp and ind), how can I sum sales by ind?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-have-a-data-sorted-by-two-variables-comp-and-ind-how-can-I-sum/m-p/644855#M192694</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have;
input comp $ ind $ sales ;
cards;
A   X  8

A   X  6

A   Y  9

A   Y  2

B   X  7

B   X  5
;


data Want ;
 if _n_=1 then do;
  dcl hash H (ordered:'a') ;
  h.definekey  ("_n_") ;
  h.definedata ("sum") ;
  h.definedone () ;
  dcl hiter hi('h');
 end;
 do until(last.comp);
  do _n_=1 by 1 until(last.ind);
   set have;
   by comp ind;
   if h.find() ne 0 then sum=sales;
   else sum=sum(sales,sum);
   h.replace();
  end;
 end;
 do While(hi.next()=0);
   output;
 end;
 h.clear();
 keep comp sum;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 03 May 2020 16:07:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-have-a-data-sorted-by-two-variables-comp-and-ind-how-can-I-sum/m-p/644855#M192694</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-03T16:07:04Z</dc:date>
    </item>
    <item>
      <title>Re: I have a data sorted by two variables (comp and ind), how can I sum sales by ind?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-have-a-data-sorted-by-two-variables-comp-and-ind-how-can-I-sum/m-p/644863#M192696</link>
      <description>Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;, very elegant!</description>
      <pubDate>Sun, 03 May 2020 18:27:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-have-a-data-sorted-by-two-variables-comp-and-ind-how-can-I-sum/m-p/644863#M192696</guid>
      <dc:creator>xmg2088</dc:creator>
      <dc:date>2020-05-03T18:27:02Z</dc:date>
    </item>
  </channel>
</rss>

