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
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).
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).
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.
Are VehicleType and Problem two separate variables?
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;
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;
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
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.
Ready to level-up your skills? Choose your own adventure.