DATA Step, Macro, Functions and more

Produce and count modes of combinations of variables

Reply
Occasional Contributor
Posts: 15

Produce and count modes of combinations of variables

[ Edited ]

Hello everyone !

 

My data looks like this :

 

occupationemployerqualification
dentistPrivate9
construction workerPrivate2
construction workerPublic2
dentistPrivate9
dentistPublic8
zoologistPublic8
dentistPublic6
dentistPublic6
police officerPublic4
police officerPublic4
police officerPublic4
police officerPublic4

 

I want to have the modes of the qualification variable for each occupation/employer couple. I'd also like to have counts for the modes. If there is an equality, the lowest qualification would be selected (lowest is 1 and highest is 9).


Result would be something like this :

occupationemployerqualificationcount
dentistPrivate92
dentist Public81
dentist Public62
construction workerPrivate21
construction workerPublic21
zoologistPublic81
police officerPublic44

 

My data has 1 million observations, so an sql approach is probably best.

Super User
Posts: 2,061

Re: Produce and count modes of combinations of variables

Posted in reply to alex_philby
data have;
input occupation & $25.	employer :$20.	qualification ;	
cards;
dentist		Private	9
construction worker		Private	2
construction worker		Public	2
dentist		Private	9
dentist		Public	8
zoologist	Public	8
dentist		Public	6
dentist		Public	6
police officer		Public	4
police officer		Public	4
police officer		Public	4
police officer		Public	4
;
proc sql;
create table want as
select   occupation,employer,qualification,count( qualification ) as count
from have
group by occupation,employer,qualification;
quit;
Super User
Posts: 6,933

Re: Produce and count modes of combinations of variables

Posted in reply to alex_philby

To get counts (before limiting results to modes):

 

proc freq data=have;

tables occupation * employer * qualification / out=want (drop=percent);

run;

Occasional Contributor
Posts: 15

Re: Produce and count modes of combinations of variables

Posted in reply to Astounding

I already tried this but it takes hours before SAS finally crash down.

Super User
Posts: 6,933

Re: Produce and count modes of combinations of variables

Posted in reply to alex_philby

It's difficult to believe that would happen with only 1M observations, but there is a workaround:

 

proc sort data=have;

by occupation;

run;

proc freq data=have;

by occupation;

tables employer * qualification / out=want (drop=percent);

run;

Occasional Contributor
Posts: 15

Re: Produce and count modes of combinations of variables

Posted in reply to Astounding

I think it's because at work we have to run SAS on some crappy virtual machine. I always have this problem when I run proc freq on 1M+ tables.

Ok so now I've got the counts but no way to find how to get the modes on SAS. Proc univariate only allows you to have the mode of one variable, not the modes of occupation*employer*qualification.

Any idea ?

Super User
Posts: 6,933

Re: Produce and count modes of combinations of variables

Posted in reply to alex_philby

If your data set holding the counts is named COUNTS, you would just need to sort and subset:

 

proc sort data=counts;

   by occupation employer descending count qualification;

run;

 

data want;

set counts;

by occupation employer descending count qualification;

if first.count;

run;

Ask a Question
Discussion stats
  • 6 replies
  • 75 views
  • 0 likes
  • 3 in conversation