BookmarkSubscribeRSS Feed
Meet26
Calcite | Level 5

I want to find the mode of columns value1,value2 for every group of name,surname columns. as mentioned in below example.

Meet26_2-1670436275121.png

 

 

I have various approaches but couldn't able to generate any significant output so have not attached my approch. but here is sample data on which it can be tried.

 

data table;
infile datalines delimiter=',';
input name $ surname $ value1 value2;
datalines;
N1,S1,90,A
N1,S1,90,B
N1,S1,100,B
N1,S2,70,C
N1,S2,80,C
N1,S2,70,C
;
run;

 

 

 

2 REPLIES 2
ballardw
Super User

First thing is make sure the data step generates the data you expect. The Value2 is missing as shown because it has not been read as character.

 

I don't have CAS so can't show code using it, much less with groupby. However the logic below may help. Caution: This approach requires a separate summary data set for each character variable.

 

If the values were all numeric proc summary would do this easily. But you have included a character value so you need something a bit more.

One way:

proc sort data=table;
   by name surname;
run;

proc summary data=table;
  by name surname;
  var value1;
  output out=value1 (drop= _:) mode=;
run;

proc freq data=table noprint order=freq;
by name surname;
tables value2/ out=value2 ;
run;

data want;
   merge value1 
         value2 (drop=count percent)
   ;
   by name surname;
   if first.surname;
run;

The order=freq in proc freq places the highest count at the first of each by group. Otherwise you would sort the data by descending count to accomplish the same thing. So when combined with the proc summary output the first match of name and surname is the one you want. Summary will only have one value for each name surname combination.

 

I

s_lassen
Meteorite | Level 14

Here is another way to generate the data you want, just using PROC SUMMARY:

proc summary data=table order=freq;
  class value1 value2;
  by name surname;
  output out=counts;
run;


data want;
  merge 
    counts(where=(_type_=2) drop=value2)
    counts(where=(_type_=1) drop=value1)
    ;
  by name surname;
  if first.surname;
  drop _:;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 2 replies
  • 374 views
  • 1 like
  • 3 in conversation