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. 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                40

3            1                40

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=1) as indicate, sum(var) as sumvar
from have
group by id
having indicate=1;
quit;


View solution in original post

5 REPLIES 5
Reeza
Super User

 

SUM + WHERE works as expected. 

 

https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas

 

Proc sql;

Select sex, sum(weight) as tot_weight
From sashelp.class
where sex='F';

Quit;

 

PaigeMiller
Diamond | Level 26
 proc summary data=have nway;
    Class id;
    var indicate var;
    output out=want(where=(max_indicate=1)) max(indicate)=max_indicate
        sum(var)=sum_var;
run;
--
Paige Miller
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=1) as indicate, sum(var) as sumvar
from have
group by id
having indicate=1;
quit;


Jagadishkatam
Amethyst | Level 16

You can also try below sql approach

 

proc sql;
create table want as select a.indicate,b.* from have as a left join (select id, sum(var) as sum from have group by id) as b on a.id=b.id having a.indicate=1;
quit;
Thanks,
Jag
Jagadishkatam
Amethyst | Level 16

Though the datastep approach is not asked, I am writing this if it helps

 

proc sort data=have;
by id indicate ;
run;

data want(where=(id=1));
set have;
by id indicate;
retain sum;
if first.id then sum=var;
else sum+var;
if last.id;
run;
Thanks,
Jag
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
  • 5 replies
  • 1642 views
  • 1 like
  • 5 in conversation