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.  

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 911 views
  • 2 likes
  • 3 in conversation