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
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;
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;
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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.