<?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: more than 1 grouping variable-Accumulate by groups in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/more-than-1-grouping-variable-Accumulate-by-groups/m-p/610446#M177767</link>
    <description>&lt;P&gt;What wrong results is it giving?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes you can have two different FIRST. in the same data step&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data = raw_tbl;
    by group group2;
run;

data want;
    set raw_tbl;
    by group group2;
    if first.group2 and first.group; 
    /* Although I don't think the above is what you want, I think you want */
    /* if first.group2; */
    /* but that's up to you */
...
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 09 Dec 2019 12:49:45 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2019-12-09T12:49:45Z</dc:date>
    <item>
      <title>more than 1 grouping variable-Accumulate by groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/more-than-1-grouping-variable-Accumulate-by-groups/m-p/610440#M177766</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to accumulate by group .&lt;/P&gt;
&lt;P&gt;The group is&amp;nbsp; more than 1 grouping variable.&lt;/P&gt;
&lt;P&gt;I want to ask 2 questions please:&lt;/P&gt;
&lt;P&gt;1-Why my code is giving wrong results?&lt;/P&gt;
&lt;P&gt;2-How can create the calculation by using&amp;nbsp; multiple FIRST. statements&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data raw_tbl;
input ID group $  group2 $ Y;
cards;
1 a One 5
2 a One 10
3 a Two 15
4 a Two 20
5 b One 25
6 b Two 30
7 b Two 35
8 c Two 40
9 c Two 45
10 c One 50
;
run;


data raw_tbl2;
set raw_tbl;
group_CAT=CATX(',',group,group2);
Run;

proc sort data=raw_tbl2;by group_CAT;Run;
data required;
set raw_tbl2;
by group_CAT;
retain Accum_Y;
if first.group_CAT  then  Accum_Y=Y;
Accum_Y+y;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Dec 2019 11:55:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/more-than-1-grouping-variable-Accumulate-by-groups/m-p/610440#M177766</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-12-09T11:55:23Z</dc:date>
    </item>
    <item>
      <title>Re: more than 1 grouping variable-Accumulate by groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/more-than-1-grouping-variable-Accumulate-by-groups/m-p/610446#M177767</link>
      <description>&lt;P&gt;What wrong results is it giving?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes you can have two different FIRST. in the same data step&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data = raw_tbl;
    by group group2;
run;

data want;
    set raw_tbl;
    by group group2;
    if first.group2 and first.group; 
    /* Although I don't think the above is what you want, I think you want */
    /* if first.group2; */
    /* but that's up to you */
...
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Dec 2019 12:49:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/more-than-1-grouping-variable-Accumulate-by-groups/m-p/610446#M177767</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-09T12:49:45Z</dc:date>
    </item>
    <item>
      <title>Re: more than 1 grouping variable-Accumulate by groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/more-than-1-grouping-variable-Accumulate-by-groups/m-p/610447#M177768</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you use the variable group_cat, you just need to reinitialize the counter to 0 each time there is a new Group_cat value:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data required;
set raw_tbl2;
by group_CAT;
if first.group_CAT  then  Accum_Y=0;
Accum_Y+y;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can simplify your code as follow:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data raw_tbl;
	input ID group $  group2 $ Y;
	cards;
1 a One 5
2 a One 10
3 a Two 15
4 a Two 20
5 b One 25
6 b Two 30
7 b Two 35
8 c Two 40
9 c Two 45
10 c One 50
;
run;

proc sort data=raw_tbl;
	by group group2;
Run;

data required;
	set raw_tbl;
	by group group2;
	if first.group2 then Accum_Y=0;
	Accum_Y+y;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The rational is that when you specify "BY" variables, SAS creates the following internal corresponding variables : Last. and First.&lt;/P&gt;
&lt;P&gt;First.&amp;lt;var&amp;gt; = 1 for the first record and 0 otherwise.&lt;/P&gt;
&lt;P&gt;In the above example, the first and last values would be as follow. So you just need to reinitialize the counter when First.group2 = 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ID	group   group2  Y	First.group First.group2
1	a		One 	 5		1		1
2	a		One 	10		0		0
3	a 		Two 	15		0		1
4	a		Two 	20		0		0
5	b		One 	25		1		1
6	b		Two		30		0		1
7	b		Two 	35		0		0
8 	c		Two 	40		1		1
9	c		Two		45		0		0
10	c		One 	50		0		1
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2019 12:39:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/more-than-1-grouping-variable-Accumulate-by-groups/m-p/610447#M177768</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-09T12:39:09Z</dc:date>
    </item>
    <item>
      <title>Re: more than 1 grouping variable-Accumulate by groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/more-than-1-grouping-variable-Accumulate-by-groups/m-p/610449#M177769</link>
      <description>&lt;P&gt;As others have already shown, you can have more than one variable in a BY statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And I think most of the suggestions you got solved the calculation problem you had. But to answer you question:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;1-Why my code is giving wrong results?&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I think you are missing an ELSE. Your summation code is&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.group_CAT  then  Accum_Y=Y;
Accum_Y+y;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Which means that in the first OBS in the group, the accumulated value will be Y+Y, and that error will persist throughout the result data.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Just change it to &lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.group_CAT  then  Accum_Y=Y;
else Accum_Y+y;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;and I think you will get the right result.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2019 12:51:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/more-than-1-grouping-variable-Accumulate-by-groups/m-p/610449#M177769</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-12-09T12:51:37Z</dc:date>
    </item>
  </channel>
</rss>

