BookmarkSubscribeRSS Feed
monday89
Fluorite | Level 6

I have the following table. What I would like to do is report the sum of all 1's overall by subjectID. Only 1's, no 2's, 0's, missing

 

have

SubjectID value1 value2 value3
1 1 0  
1 0 1 2
1 0 0 0
1 0 0 0
2 0 1 0
2 0 0 0
2 0 0 0
2 0 0 1
2 0 0 2
2 0 0 2
3 0 0 0
4 0 0 0
4 1 0 1
4 1 1 0
       

 

SubjectID total
1 2
2 2
3 0
4 4

 

i tried the following

proc sql;

select subjectid, count(subjectid) as total

from a.have

where value1 = 1 or value2 = 1 or value3= 1;quit;

2 REPLIES 2
Krueger
Pyrite | Level 9

Below will run according to what you requested; keep in mind if your expected output changes this likely won't cross over well.

 

Edit: Made correction to code.

 

proc sql;
create table want as
	select SubjectID, SUM(Value1=1)+SUM(Value2=1)+SUM(Value3=1) As Total
	from have
	group by SubjectID;
quit;
novinosrin
Tourmaline | Level 20

Hi @monday89  I'm afraid SQL is not best suited for wide datasets.

 

data have;
input SubjectID	value1	value2	value3;
cards;
1	1	0	. 
1	0	1	2
1	0	0	0
1	0	0	0
2	0	1	0
2	0	0	0
2	0	0	0
2	0	0	1
2	0	0	2
2	0	0	2
3	0	0	0
4	0	0	0
4	1	0	1
4	1	1	0
;
data want;
 do until(last.subjectid);
  set have;
  by subjectid;
  array t value1-value3;
  call missing(n);
  do over t;
   if t=1 then n=sum(t,n);
  end;
  sum=sum(sum,n,0);
 end;
 keep subjectid sum;
run;

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

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
  • 882 views
  • 1 like
  • 3 in conversation