Help using Base SAS procedures

Summinng on first and last.

Reply
N/A
Posts: 0

Summinng on first and last.

Hi

i have a data i want the income sum betweeen where first=1 and last=1;

id inco first last
1 2 1 0
1 2 0 0
2 2 0 1
3 4 1 0
4 6 0 0
5 7 0 1


the output is like this

id inc first last final_tot
1 2 1 0
1 2 0 0
2 2 0 1 4
3 4 1 0
4 6 0 0
5 7 0 1 12
Super User
Posts: 5,429

Re: Summinng on first and last.

Posted in reply to deleted_user
You should explore the use of BY group processing techniques in the data step such as FIRST. and LAST., together with RETAIN.

/Linus
Data never sleeps
N/A
Posts: 0

Re: Summinng on first and last.

Posted in reply to deleted_user
in the o/p you have shown ,
you are adding id and inco where last=1.

but you are saying "income sum betweeen where first=1 and last=1"
here is solution.

data i;
input id inco first last;
cards;
1 2 1 0
1 2 0 0
2 2 0 1
3 4 1 0
4 6 0 0
5 7 0 1
;

data i1;
set i;
retain tot 0;
if first=1 then tot = inco;
else if last = 1 then final_tot = tot + inco;
else tot = tot + inco;
drop tot;
run;



o/p - i1

id inc first last final_tot
1 2 1 0
1 2 0 0
2 2 0 1 6
3 4 1 0
4 6 0 0
5 7 0 1 17
Ask a Question
Discussion stats
  • 2 replies
  • 238 views
  • 0 likes
  • 2 in conversation