Mathias Nissen
Mathias Nissen

Reputation: 27

Creating a histogram and afterwards adding points to it using SGPLOT

I have some data for six different columns that I want to create a histogram for each column. This is pretty straight forward where I can use SGPLOT, for example

PROC SGPLOT DATA=test;
   HISTOGRAM leverage;
RUN;

However, now I want to calculate the 1%, 99% quantiles from the column called "leverage" and then insert these two values as points, also with their names. This is pretty easy to do in R, but I need to be able to do it in SAS aswell. Here is how the plot should look like, which has been done with ggplot in R. plot in R

Notice that I have to add another point which comes from a CSV file. I think I can manage to plot this aswell if someone can help me plot the quantiles. Thanks

Upvotes: 1

Views: 301

Answers (2)

Dirk Horsten
Dirk Horsten

Reputation: 3845

Try this

proc means p1 p99 data = sashelp.Heart noprint;
    var Cholesterol;
    output out=Heart_p 
        p1(Cholesterol)=Cholesterol_1 
        p99(Cholesterol)=Cholesterol_99;
run;
proc sql noprint;
    select Cholesterol_1,  Cholesterol_99
    into  :Cholesterol_1, :Cholesterol_99
    from Heart_p;
quit;
 
proc sgplot data=sashelp.Heart;
   histogram Cholesterol;
   xaxis max=500;
   refline &Cholesterol_1 280 &Cholesterol_99 
    /   axis=x lineattrs=(thickness=3 color=darkred pattern=dash)
        label=("P1" "acceptable" "P99");
   /* Note: Order matters. Put REFLINE stmt first if you want it behind the bars */
run;

result

Upvotes: 1

Richard
Richard

Reputation: 27508

For some graphs you can overlay another graph, say a SERIES to display additional information. Unfortunately, a SERIES can not be overlay a HISTOGRAM.

The alternative is to use the option SGANNO=<annotation-dataset> to draw on a plot.

Example:

Proc SUMMARY is used to compute P1 and P99. The annotation data is prepared as needed.

data have;
  call streaminit(20230323);
  do index = 1 to 1000;
    do until (x>0);
      x = rand('normal',5, 2.5);
    end;
    output;
  end;
run;

proc summary data=have;
   var x;
   output out=quantiles p1=x_p1 p99=x_p99;
run;

data external;
  x = 3.14159;
run;

data anno(keep=x1 y1 label function drawspace);
  function = 'text';
  drawspace= 'datavalue';
  y1=0;
  set quantiles;
  length label $20;
  x1 = x_p1;  label = cats('P1=' ,put(x1,7.4))  ; output;
  x1 = x_p99; label = cats('P99=',put(x1,7.4)); output;
  set external;
  x1 = x; label = cats('Pi=',put(x1,7.4)); output;
run;

ods html;
title;
proc sgplot data=have sganno=anno;
  histogram x;
run;
ods html close;

enter image description here

Upvotes: 1

Related Questions