SAS Programming

DATA Step, Macro, Functions and more
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-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1092 views
  • 0 likes
  • 4 in conversation