sas macro - SAS: Most frequent value (Like a MODE) ties solved by recency? -


i have data this:

data mydata; input id $ val $ date; datalines; 1   2010-12-01  1  b  2010-12-03  1   2010-12-04  1  b  2010-12-08 2  x  2009-10-01  2  x  2009-10-02  2  z  2009-10-03  ; run; 

i mode returned exists. id 1, however, doesn't have true mode. in case of ties modes not exist recent val break tie (as in id 1).

desired output:

id mode 1  b   2  x   

i tried proc univariate (which handles numeric modes, problem) gives dataset mode null; sas has correct not desired output. in datastep.

code:

proc univariate data=mydata noprint; class id; var val; output out=modetable mode=mode; run; 

output:

id mode 1   2  x 

use idgroup proc means

an example of statement can fount in identifying top 3 extreme values output statistics

let extend example data little bit;

data myinput;     infile datalines dsd delimiter='09'x;     input          @1 id 1.          @4 val $1.          @7 date yymmdd10.;     format date yymmdd10.;     datalines; 2  x  2009-10-01 2  x  2009-10-02 2  z  2009-10-03 3  c  2010-10-01 3  b  2010-10-03 3   2010-10-04 3   2010-12-01 3  b  2010-12-03 3  c  2010-12-04 ; run; 

now let count frequency , last occurence of each ´val´ each ´id´;

proc sql;     create view myview      select id, val, max(date) date format=yymmdd10., count(*) freq     myinput     group id, val; run; 

and finally, retain 1 val each id, prefering more frequent 1 , within equally frequent ones recent one;

proc means data=myview nway noprint;    class id;    output out=mymodes(keep= id mode)           idgroup( max(freq date) out[1] (val)=mode); run;  proc print data=mymodes; run; 

the result is;

id  mode 2   x 3   c 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -