BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Bright
Obsidian | Level 7

Hi, 

I have a dataset as below:

ID Condition A Condition B Condition C Condition D
250 0 0 0 0
250 0 1 0 0
250 1 0 0 0
260 0 0 0 0
260 0 0 0 0
270 1 0 0 0
270 0 1 0 0
270 0 0 0 1

I want an output like the table below:

ID Condition A Condition B Condition C Condition D
250 1 1 0 0
260 0 0 0 0
270 1 1 0 1

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Can we assume that variable are boolean 1/0 numeric values ?

Then just use PROC SUMMARY.

If the goal is to get an aggregate boolean values to indicate if the condition ever occurred then use the MAX statistic.

If the goal is to get a count of how many times the condition occurred then use the SUM statistic.

proc summary data=have nway;
  class id ;
  var condition:;
  output out=want max= ;
run;

If the data is already sorted by ID then use BY ID instead of CLASS ID.

 

View solution in original post

3 REPLIES 3
japelin
Rhodochrosite | Level 12

If values are only 0 or 1, how about this.

 

data have;
  input id a b c d;
datalines;
250	0	0	0	0
250	0	1	0	0
250	1	0	0	0
260	0	0	0	0
260	0	0	0	0
270	1	0	0	0
270	0	1	0	0
270	0	0	0	1
;
run;

proc sql;
  create table want as
  select id,
         max(a) as a,
         max(b) as b,
         max(c) as c,
         max(d) as d
         from have
  group by id
  ;
quit;
Kurt_Bremser
Super User
proc summary data=have nway;
class id;
var cond:;
output
  out=want (drop=_type_ _freq_)
  sum()=
;
run;

Untested; for tested code, provide data in a working data step with datalines.

(working = no WARNINGs, no ERRORs)

Tom
Super User Tom
Super User

Can we assume that variable are boolean 1/0 numeric values ?

Then just use PROC SUMMARY.

If the goal is to get an aggregate boolean values to indicate if the condition ever occurred then use the MAX statistic.

If the goal is to get a count of how many times the condition occurred then use the SUM statistic.

proc summary data=have nway;
  class id ;
  var condition:;
  output out=want max= ;
run;

If the data is already sorted by ID then use BY ID instead of CLASS ID.

 

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
  • 3 replies
  • 4794 views
  • 0 likes
  • 4 in conversation