BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Melk
Lapis Lazuli | Level 10

I have a longitudinal dataset and I want to sum var for IDs where indicate = 1 but not including the observation where indicate=1. How can I do this in sql?

 

Data format:

ID       Indicate         var

1         1                    10

1         0                    20

1         0                    10

2         0                    5

2         0                    15

2         0                    30

3         1                    30

3         0                    10

 

 

Desired data:

ID       indicate      sumvar

1            1                30

3            1                10

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;
input ID       Indicate         var;
cards;
1         1                    10
1         0                    20
1         0                    10
2         0                    5
2         0                    15
2         0                    30
3         1                    30
3         0                    10
;

proc sql;
create table want as
select id,max(indicate) as indicate, sum((indicate=0)*var) as sum
from have
group by id
having indicate=1;
quit;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20
data have;
input ID       Indicate         var;
cards;
1         1                    10
1         0                    20
1         0                    10
2         0                    5
2         0                    15
2         0                    30
3         1                    30
3         0                    10
;

proc sql;
create table want as
select id,max(indicate) as indicate, sum((indicate=0)*var) as sum
from have
group by id
having indicate=1;
quit;
ajkalale
Obsidian | Level 7
proc sql;
select sum(var) as nvars
from s
where indicate='1';
quit;

I have used indicate='1'with quotes because indicate is a "Character variable". You can even wish to make indicate as a numeric variable and use the code without quotes and the program will work.

 

Thank you! It was great SAS programming exercise.  

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1371 views
  • 2 likes
  • 3 in conversation