BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi i have 5 variable i want the count of most observation occured

a b c d e
1 1 1 2 3
5 5 6 5 2
9 9 2 0 8


output

a b c d e most_obs count
1 1 1 2 3 1 3
5 5 6 5 2 5 2
9 9 2 0 8 2 9

in this first observation the mostly repedatd observation was 1 so the variable as most_obs as 1 and the count was 3 .like this i have 20,000 obs
4 REPLIES 4
LinusH
Tourmaline | Level 20
See if I get you right, but I think that you want the values (not observations) that has most occurrences on each row/observation, and the number of occurrences.
If this is the case, it seems that your sample output is a bit incorrect. Wouldn't rather be like:

a b c d e most_obs count
1 1 1 2 3 1 3
5 5 6 5 2 5 3
9 9 2 0 8 9 2

Maybe you can solve this via some data step programming. But a more generic way is to transpose the data, then use SQL with COUNT and HAVING to find out your new columns, and then join back to the original table. This means you will need an id column. If you don't have one, you can easily create it using _n_ in a data step.

/Linus
Data never sleeps
ChrisNZ
Tourmaline | Level 20
I agree with Linus, transposing would help and be valid with any number of values you could have. I reckon you need 3 steps:

1)transpose to:
ID VAL
1 1
1 1
1 1
1 2
1 3
2 5
..

then 2)count observations by ID and VAL (proc sql or means, output sorted by frequency),
and 3)keep the maximum count for each id(data step using the LAST. variable)
deleted_user
Not applicable
As Linus and Chris mentioned this is the solution for your problem:

data test;
set test;
seq+1;
if seq ne 1;
run;

proc transpose data=test out=test1;
by seq;
var a b c d e;
run;

proc freq data=test1 noprint;
tables col1/out=test2;
by seq;
run;

proc sort data=test2;
by seq descending count;
run;

data test3;
set test2;
by seq;
if first.seq;
run;

data test4(drop=seq rename=(col1=most_obs));
merge test test3(keep=seq col1 count);
by seq;
run;

~ Sukanya E
deleted_user
Not applicable
Thqs it worked .

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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