BookmarkSubscribeRSS Feed
AlexeyS
Pyrite | Level 9

I am looking for SAS code to calculate the medcouple, has anyone such knowledge?

 

https://en.wikipedia.org/wiki/Medcouple

5 REPLIES 5
Ksharp
Super User

It is my first time to hear this term. I don't know if @Rick_SAS  could write some IML code to make it happen.

AlexeyS
Pyrite | Level 9

Unfortunately, I need it in SAS BASE, and not in SAS IML.

 

Rick_SAS
SAS Super FREQ

I am familiar with this concept, but I do not have any non-IML code to share.

mkeintz
PROC Star

Here's the brute force way.  I.e. it doesn't take advantage of the "fast" algorithm in the Wikipedia reference. so it won't scale well against larger datasets.

 

The example finds the medcouple stat for variable CLOSE in sashelp.stocks for IBM stocks (N=233).  If you have a test data set to confirm correctness of the algorithm, I'd suggest running it.

 

data have (keep=date close);
  set sashelp.stocks;
  where stock='IBM';
run;


proc means data=have noprint;
  output out=median  median=median;
  var close;
run;


data vneed (keep=h) / view=vneed;
  set have end=end_of_have;
  if _n_=1 then set median;

  array upper{2000} _temporary_; /* distance of upper obs to median*/
  array lower{2000} _temporary_; /* distance of lower obs to median*/

  if close>=median then do;
    P+1;
    upper{p}=close-median;
  end;
  if close<=median then do;
    Q+1;
    lower{q}=median-close;
  end;

  if end_of_have;
  do i=1 to P;
    do j=1 to Q;
      if upper{i}=0 and lower{j}=0 then h=sign(P-1-i-j);
      else h= (upper{i}-lower{j})/(upper{i}+lower{j});
      output;
    end;
  end;
run;
proc means data=vneed median; var h;
run;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
peiting
Calcite | Level 5

Hi, I'm newbie here. may I clarify array upper {2000} and lower {2000}? is the 2000 is the max & min value of the dataset?

 

Thank you!

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 2076 views
  • 0 likes
  • 5 in conversation