Help using Base SAS procedures

Counting obs Horizontally

Reply
N/A
Posts: 0

Counting obs Horizontally

Hi i have 5 variable i want the count of most observation occured

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
Super Contributor
Super Contributor
Posts: 3,174

Re: Counting obs Horizontally

Posted in reply to deleted_user
Using a DATA step and two arrays (one to track the values and another to track the counts), you can loop through each observation's variable "values" for A through E identifying each unique instance and incrementing a counter.

Scott Barry
SBBWorks, Inc.
Valued Guide
Posts: 2,177

Re: Counting obs Horizontally

might also find CALL SORTc/n() useful, to put the array values into order. Then as you go through the array of values, you only need to keep the latest value, it's counter and the highest value/count so far. It scales up better, and you won't need to search for the counter to update as you look at each (disordered) array value.

Peter
Super Contributor
Super Contributor
Posts: 3,174

Re: Counting obs Horizontally

A SAS pre-processing SORT step gets the data into order, followed by the DATA step suggested. The use of two ARRAYs (in parallel, one for value and another for count) addresses the objective, using the DO I=1 TO DIM(); / END; with the "I" variable paralleling the two arrays.

Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 3 replies
  • 113 views
  • 0 likes
  • 3 in conversation