BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
2 REPLIES 2
LinusH
Tourmaline | Level 20
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
deleted_user
Not applicable
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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1034 views
  • 0 likes
  • 2 in conversation