BookmarkSubscribeRSS Feed
radhikaa4
Calcite | Level 5

Hello. I would like to use PROC SQL command to collapse by group.

 

For example here is the dataset:

subjectidpmh_1pmh_2pmh_3pmh_4
11000
10100
10001
10010
20001
20010
21000

 

I want this output (one subject Id per row but summed )

 

subjectidpmh_1pmh_2pmh_3pmh_4
11111
2101

1

I tried having and group by clause but it wont do much

 


Thanks so much for your help!

3 REPLIES 3
Reeza
Super User

@radhikaa4 wrote:

Hello. I would like to use PROC SQL command to collapse by group.

 

For example here is the dataset:

subjectid pmh_1 pmh_2 pmh_3 pmh_4
1 1 0 0 0
1 0 1 0 0
1 0 0 0 1
1 0 0 1 0
2 0 0 0 1
2 0 0 1 0
2 1 0 0 0

 

I want this output (one subject Id per row but summed )

 

subjectid pmh_1 pmh_2 pmh_3 pmh_4
1 1 1 1 1
2 1 0 1

1

I tried having and group by clause but it wont do much

 


Thanks so much for your help!


Please post what you've tried so we can help you out.

SQL should work fine and this is fairly simple calculation using either MAX or SUM depending on your logic and requirements.

ballardw
Super User

@radhikaa4 wrote:

Hello. I would like to use PROC SQL command to collapse by group.

 

For example here is the dataset:

subjectid pmh_1 pmh_2 pmh_3 pmh_4
1 1 0 0 0
1 0 1 0 0
1 0 0 0 1
1 0 0 1 0
2 0 0 0 1
2 0 0 1 0
2 1 0 0 0

 

I want this output (one subject Id per row but summed )

 

subjectid pmh_1 pmh_2 pmh_3 pmh_4
1 1 1 1 1
2 1 0 1

1

I tried having and group by clause but it wont do much

 


Thanks so much for your help!


Show the code and explain why you think it "wont do much".

novinosrin
Tourmaline | Level 20

I concur with others in the thread. I am jumping in the thread to have my argument against sql for this exercise for the reason sql doesn't support variable lists making it inconvenient to type, that is more applicable to especially lazy folks like me. When you have an optimized pre written procs assuming you are using SAS, the convention is to take advantage of it.

 

 

data have;
input subjectid	pmh_1	pmh_2	pmh_3	pmh_4;
cards;
1	1	0	0	0
1	0	1	0	0
1	0	0	0	1
1	0	0	1	0
2	0	0	0	1
2	0	0	1	0
2	1	0	0	0
;

proc means data=have noprint;
by subjectid;
var pmh_:;
output out=want(drop=_:) sum=;
run;

 

 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1115 views
  • 6 likes
  • 4 in conversation