BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AK100
Pyrite | Level 9

I want to know for every vehicle how much a problem occurs. So this means a problem can occurs more than once for every vehicle.

 

INPUT
VehicleType - Problem
AA-Z - Motor

AA-Z - Motor

AA-Z - Pedal

BB-C - Screen

BB-C - Screen

DD-F - Motor

GG-H - Motor

GG-H- Motor

GG-H - Motor

 

Expected Output
VehicleType - Problem - CountProblem
AA-Z - Motor - 2

BB-C -Screen - 2

DD-F - Motor - 1
GG-H - Motor- 3

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
proc sql;
create table want as
  select *
  from (
    select vehicletype, problem, count(*) as countproblem
    from have
    group by vehicletype, problem
  )
  group by vehicletype
  having countproblem = max(countproblem)
;
quit;

Untested, for lack of usable example data (working data step with datalines).

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User
proc sql;
create table want as
  select *
  from (
    select vehicletype, problem, count(*) as countproblem
    from have
    group by vehicletype, problem
  )
  group by vehicletype
  having countproblem = max(countproblem)
;
quit;

Untested, for lack of usable example data (working data step with datalines).

AK100
Pyrite | Level 9
Thanks Kurt, this one worked good. However, what If my collumn problem is actually named Problem-Iso-2434?
Kurt_Bremser
Super User

That's not a valid SAS name, so the question is moot. Even with the option of validvarname=any, YOU DO NOT USE SUCH NAMES.

 

Fix such issues when importing data into SAS, change the hyphens to underlines.

PeterClemmensen
Tourmaline | Level 20

Are VehicleType and Problem two separate variables?

Ksharp
Super User
data have;
input Vehicle $ Problem $;
cards;
AA-Z  Motor
AA-Z  Motor
AA-Z  Pedal
BB-C  Screen
BB-C  Screen
DD-F  Motor
GG-H  Motor
GG-H  Motor
GG-H  Motor
;
proc sql;
create table temp as
select vehicle,problem,count(*) as n
 from have
  group by vehicle,problem
   order by vehicle,n;
quit;
data want;
 set temp;
 by vehicle;
 if last.vehicle;
run;
PeterClemmensen
Tourmaline | Level 20
data have;
infile datalines dlmstr=' - ';
input VehicleType $ Problem $;
datalines;
AA-Z - Motor 
AA-Z - Motor 
AA-Z - Pedal 
BB-C - Screen
BB-C - Screen
DD-F - Motor 
GG-H - Motor 
GG-H - Motor 
GG-H - Motor 
;

proc freq data=have noprint order=freq;
   tables VehicleType*Problem / out=temp(drop=percent);
run;

data want;
   set temp;
   by VehicleType notsorted;
   if first.VehicleType;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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