<?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 Using first.variable and last.variable to get sum of 2 observations in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Using-first-variable-and-last-variable-to-get-sum-of-2/m-p/572556#M12349</link>
    <description>&lt;P&gt;SAS Version 9.4&lt;/P&gt;&lt;P&gt;Good day and thank you for looking at my question.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.have;
	infile datalines dlm=' ';
	input CN $1. @5 SEN $1. @9 RT $1. @12 Value;
	datalines;
x	p	d	5
x	p	b	7
x	u	d	6
x 	u	b	8
y	t	d	2
y	t	b	8
z	t	d	3
z	t	b	9
q	p	d	4
q	p	b	6
;
run;

proc sort data=work.have; by cn sen; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The sorted dataset, work.have, is identical to the original.&lt;/P&gt;&lt;P&gt;For each unique CN, I need to check that the first observation of SEN is the same as the second observation of SEN.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, I need the first RT to be D or B; if D then the second RT should be B; if B, then the second RT should be D.&lt;/P&gt;&lt;P&gt;Finally, I need to calculate the sum of the 2 VALUES that meet these conditions.&lt;/P&gt;&lt;P&gt;The resulting dataset should look like this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="4.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/30927iB034F3F4B5A94FA4/image-size/large?v=v2&amp;amp;px=999" role="button" title="4.JPG" alt="4.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;My attempts have failed, but I have this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.want;
	set work.have;
	by CN SEN;
	sumit=0;
	if first.CN and first.sen then do;
		count=0;
		save_value1=value;
	end;
	count+1;
	sumit=save_value1;
	if last.cn and last.sen then save_value2=value;
	sumit+save_value2;
run;
proc print data=work.want; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you for assistance.&lt;/P&gt;&lt;P&gt;Jane&lt;/P&gt;</description>
    <pubDate>Wed, 10 Jul 2019 22:31:36 GMT</pubDate>
    <dc:creator>jawhitmire</dc:creator>
    <dc:date>2019-07-10T22:31:36Z</dc:date>
    <item>
      <title>Using first.variable and last.variable to get sum of 2 observations</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-first-variable-and-last-variable-to-get-sum-of-2/m-p/572556#M12349</link>
      <description>&lt;P&gt;SAS Version 9.4&lt;/P&gt;&lt;P&gt;Good day and thank you for looking at my question.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.have;
	infile datalines dlm=' ';
	input CN $1. @5 SEN $1. @9 RT $1. @12 Value;
	datalines;
x	p	d	5
x	p	b	7
x	u	d	6
x 	u	b	8
y	t	d	2
y	t	b	8
z	t	d	3
z	t	b	9
q	p	d	4
q	p	b	6
;
run;

proc sort data=work.have; by cn sen; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The sorted dataset, work.have, is identical to the original.&lt;/P&gt;&lt;P&gt;For each unique CN, I need to check that the first observation of SEN is the same as the second observation of SEN.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, I need the first RT to be D or B; if D then the second RT should be B; if B, then the second RT should be D.&lt;/P&gt;&lt;P&gt;Finally, I need to calculate the sum of the 2 VALUES that meet these conditions.&lt;/P&gt;&lt;P&gt;The resulting dataset should look like this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="4.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/30927iB034F3F4B5A94FA4/image-size/large?v=v2&amp;amp;px=999" role="button" title="4.JPG" alt="4.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;My attempts have failed, but I have this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.want;
	set work.have;
	by CN SEN;
	sumit=0;
	if first.CN and first.sen then do;
		count=0;
		save_value1=value;
	end;
	count+1;
	sumit=save_value1;
	if last.cn and last.sen then save_value2=value;
	sumit+save_value2;
run;
proc print data=work.want; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you for assistance.&lt;/P&gt;&lt;P&gt;Jane&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2019 22:31:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-first-variable-and-last-variable-to-get-sum-of-2/m-p/572556#M12349</guid>
      <dc:creator>jawhitmire</dc:creator>
      <dc:date>2019-07-10T22:31:36Z</dc:date>
    </item>
    <item>
      <title>Re: Using first.variable and last.variable to get sum of 2 observations</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-first-variable-and-last-variable-to-get-sum-of-2/m-p/572576#M12352</link>
      <description>&lt;P&gt;I tried, but the thing I don't get is why the first sumit should be 0 and the rest of them should be the sum of all the value in the group. But, I got the rest. If you really need a 0 there, you would have to use a temporary variable that you retain and then make sure to only use that one if you are on row two in each group.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.want;
	set work.have;
	by CN SEN;
	if first.sen then do;
		sumit=0; 
		count=0;
		save_value1=value;
	end;
	count+1;
	sumit+value;
	if last.sen then save_value2=value;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I think you got a bit confused with the first.CN. You basically don't need that one.&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you have first.CN then by definition first.SEN will be true. As an example.&lt;/P&gt;&lt;P&gt;CN SEN&lt;/P&gt;&lt;P&gt;A A&lt;/P&gt;&lt;P&gt;A B&amp;nbsp;&lt;/P&gt;&lt;P&gt;B B&lt;/P&gt;&lt;P&gt;The third row will have have both first.CN and first.SEN true.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2019 01:02:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-first-variable-and-last-variable-to-get-sum-of-2/m-p/572576#M12352</guid>
      <dc:creator>heffo</dc:creator>
      <dc:date>2019-07-11T01:02:04Z</dc:date>
    </item>
    <item>
      <title>Re: Using first.variable and last.variable to get sum of 2 observations</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-first-variable-and-last-variable-to-get-sum-of-2/m-p/572792#M12379</link>
      <description>&lt;P&gt;The problem is that CN may have 4 identical characters.&amp;nbsp; The following is my current version and is summing as desired, but fails to check one RT value is "d" and the other is "b":&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.have;
	infile datalines dlm=' ';
	input CN $1. @5 SEN $1. @9 RT $1. @12 Value;
	datalines;
x	p	d	5
x	p	b	7
x	u	d	6
x 	u	b	8
y	t	d	2
y	t	b	8
z	t	d	3
z	t	b	9
q	p	d	4
q	p	b	6
;
run;

proc sort data=work.have; by CN SEN; run;
data work.want2 ;
	set work.have;
	by CN SEN;
	if first.CN or first.SEN then cumscore=value;
	else cumscore+value;
run;
proc print data=work.want2; title 'want2'; run;

proc print data=work.want; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;output:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2.JPG" style="width: 329px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/30978i0828A49228B97624/image-size/large?v=v2&amp;amp;px=999" role="button" title="2.JPG" alt="2.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2019 15:36:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-first-variable-and-last-variable-to-get-sum-of-2/m-p/572792#M12379</guid>
      <dc:creator>jawhitmire</dc:creator>
      <dc:date>2019-07-11T15:36:56Z</dc:date>
    </item>
    <item>
      <title>Re: Using first.variable and last.variable to get sum of 2 observations</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-first-variable-and-last-variable-to-get-sum-of-2/m-p/572809#M12380</link>
      <description>&lt;P&gt;Note that if your BY statement has two variables like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;by CN SEN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then whenever you are at the first observation for a given value of CN you are also by definition at the first observation for any variable after it in the BY statement.&amp;nbsp; So your IF condition can be simplified by eliminating the FIRST.CN.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.SEN &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2019 15:58:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-first-variable-and-last-variable-to-get-sum-of-2/m-p/572809#M12380</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-11T15:58:43Z</dc:date>
    </item>
  </channel>
</rss>

