<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Spline with superimposed histogram in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Spline-with-superimposed-histogram/m-p/871313#M344170</link>
    <description>&lt;P&gt;You could save this histogram firstly and after combine it with this spline series point and plot them all together.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ods output sgplot=sgplot(keep=BIN_WEIGHT____X BIN_WEIGHT____Y where=(BIN_WEIGHT____Y is not missing));
proc sgplot data=sashelp.heart;
histogram weight;
run;

proc sort data=sashelp.class out=class;by weight;run;
data have;
 set sgplot class(keep=weight in=inb);
 zero=0;
 call streaminit(123);
if inb then percent=rand('integer',1,15);
run;

proc sgplot data=have;
highlow x=BIN_WEIGHT____X low=zero high=BIN_WEIGHT____Y/type=bar barwidth=1;
series x=weight y=percent;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1682165216330.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83039i448DEE17A89589FA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1682165216330.png" alt="Ksharp_0-1682165216330.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 22 Apr 2023 12:07:05 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2023-04-22T12:07:05Z</dc:date>
    <item>
      <title>Spline with superimposed histogram</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Spline-with-superimposed-histogram/m-p/871283#M344153</link>
      <description>&lt;P&gt;Is there a way to create a plot with histogram and natural cubic spline in one plot?&amp;nbsp; An example downloaded from the net is given below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The spline can be&amp;nbsp; created using the ESTIMATE statement.&amp;nbsp; &amp;nbsp;I cannot share the data.&amp;nbsp; I have a spline curve and would like to also show the histogram on the same plot.&amp;nbsp; A sample downloaded is given below.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc phreg data = want;&lt;BR /&gt;title "Colorectal-cancer incidence attempt";&lt;/P&gt;&lt;P&gt;effect resid_total_polys = spline(resid_total_poly / basis=tpf(noint) NATURALCUBIC details knotmethod=percentiles(4));&lt;/P&gt;&lt;P&gt;class categorical_covariate (ref="0");&lt;/P&gt;&lt;P&gt;model eof_age_CRC*Inc_CRC(0) = resid_total_polys&lt;BR /&gt;categorical_covariate continuous_covariate&lt;BR /&gt;/ entry=enrollment_agemonths rl=wald;&lt;/P&gt;&lt;P&gt;store work.coxr;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%macro est(ref=212, start=0, end=3000, by=1);&lt;/P&gt;&lt;P&gt;%Do i = 1 %To %eval(%SysFunc( Ceil( %SysEvalF( ( &amp;amp;End - &amp;amp;Start ) / &amp;amp;By ) ) ) +1) ;&lt;/P&gt;&lt;P&gt;%Let value=%SysEvalF( ( &amp;amp;Start - &amp;amp;By ) + ( &amp;amp;By * &amp;amp;I ) ) ;&lt;/P&gt;&lt;P&gt;estimate "&amp;amp;value." resid_total_polys [-1, &amp;amp;ref] [1, &amp;amp;value] / exp cl;&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;%mend est;&lt;/P&gt;&lt;P&gt;ods exclude all;&lt;/P&gt;&lt;P&gt;ods dataset Estimates=Estimates;&lt;/P&gt;&lt;P&gt;proc plm restore=work.coxr;&lt;/P&gt;&lt;P&gt;%est(ref=212, start=0, end=3000, by=1);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;ods exclude none;&lt;/P&gt;&lt;P&gt;data estimates;&lt;/P&gt;&lt;P&gt;set estimates;&lt;/P&gt;&lt;P&gt;resid_total_poly=label*1;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;proc sgplot data=estimates NOAUTOLEGEND Noborder;&lt;/P&gt;&lt;P&gt;Series y=ExpEstimate x=resid_total_poly / LINEATTRS=(color=black thickness=3px);&lt;/P&gt;&lt;P&gt;Series y=LowerExp x=resid_total_poly / LINEATTRS=(pattern=ShortDash color=Black THICKNESS=1);&lt;/P&gt;&lt;P&gt;Series y=UpperExp x=resid_total_poly / LINEATTRS=(pattern=ShortDash color=Black THICKNESS=1);&lt;BR /&gt;&lt;BR /&gt;band x=resid_total_poly upper=UpperExp lower=LowerExp / fillattrs=(color=graydd) transparency=.50;&lt;/P&gt;&lt;P&gt;REFLINE 1 / axis=y;&lt;/P&gt;&lt;P&gt;REFLINE 3000 / Axis=X LINEATTRS=(pattern=ThinDot color=Black THICKNESS=1);;&lt;/P&gt;&lt;P&gt;yaxis Values=(0.5 0.8 1 1.2 1.5) Label="Hazard ratio" Type=LOG LABELATTRS=(weight=BOLD);&lt;/P&gt;&lt;P&gt;xaxis min=0 VALUES=(0 to 3000 by 100) LABELATTRS=(weight=BOLD);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PamG_0-1682130949268.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83034iE55DABD8357B4B47/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PamG_0-1682130949268.png" alt="PamG_0-1682130949268.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Apr 2023 02:46:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Spline-with-superimposed-histogram/m-p/871283#M344153</guid>
      <dc:creator>PamG</dc:creator>
      <dc:date>2023-04-22T02:46:09Z</dc:date>
    </item>
    <item>
      <title>Re: Spline with superimposed histogram</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Spline-with-superimposed-histogram/m-p/871307#M344168</link>
      <description>&lt;P&gt;As far as I know, you cannot overlay a histogram and curve in PROC SGPLOT because they are&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/grstatproc/p0yud64khw8fuin1xgr85dgxbb7t.htm" target="_self"&gt; different graph types&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;However, you can perform the overlay in GTL, and the basic ideas are presented in the article, &lt;A href="https://blogs.sas.com/content/iml/2013/04/24/overlay-density-curve-on-a-histogram.html" target="_self"&gt;"How to overlay a custom density curve on a histogram in SAS."&lt;/A&gt;&amp;nbsp;&amp;nbsp;For your use case, the curve is not a density curve, which means that you want the curve to be associated with the Y2 axis.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can take the example from the "How to overlay..." article and apply it to your example. Since you did not supply data, I created some example data to illustrate the method:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SGRender11.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83037i92B21CFEE30E75C8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SGRender11.png" alt="SGRender11.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create example data to use for the histogram and the curve/band */
data&lt;BR /&gt; Have;
  set sashelp.class;
  keep Height;
run;
data Curve;
  do Height = 51 to 72;
     ratio = 1 + 0.01*(Height - 60)**2;
     lowerCL = ratio - ratio/10;
     upperCL = ratio + ratio/10;
     output;
  end;
run;

/* merge the data into one data set */
data All;
set Have
    Curve(rename=(Height=h));
run;

/* Use GTL to overlay a curve on a histogram.
   See https://blogs.sas.com/content/iml/2013/04/24/overlay-density-curve-on-a-histogram.html
*/
proc template;
define statgraph HistCurve;
dynamic _X _T _Y _YLower _YUpper _Title _HAlign _binstart _binstop _binwidth;
begingraph;
   entrytitle halign=center _Title;
   layout overlay /xaxisopts=(linearopts=(viewmax=_binstop));
      histogram _X / name='hist' SCALE=PERCENT binaxis=true YAXIS=Y2
         FillAttrs=GraphConfidence(Color=CXD3D3D3) /* gray bars */
         endlabels=true xvalues=leftpoints binstart=_binstart binwidth=_binwidth;
      BandPlot X=_T LimitUpper=_YUpper LimitLower=_YLower / DataTransparency=0.5 yaxis=Y;
      SeriesPlot X=_T Y=_Y / yaxis=Y;
      ReferenceLine y=1 / clip=true;
   endlayout;
endgraph;
end;
run;

proc sgrender data=All template=HistCurve;
label Ratio = "Hazard Ratio (95% CI)";
dynamic _X="Height" _T="H" _Y="Ratio" _YLower="lowerCL" _YUpper="upperCL" _HAlign="right"
        _binstart=50 _binstop=75 _binwidth=5
        _Title="Overlay Histogram and Curve";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Apr 2023 11:02:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Spline-with-superimposed-histogram/m-p/871307#M344168</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2023-04-22T11:02:24Z</dc:date>
    </item>
    <item>
      <title>Re: Spline with superimposed histogram</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Spline-with-superimposed-histogram/m-p/871313#M344170</link>
      <description>&lt;P&gt;You could save this histogram firstly and after combine it with this spline series point and plot them all together.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ods output sgplot=sgplot(keep=BIN_WEIGHT____X BIN_WEIGHT____Y where=(BIN_WEIGHT____Y is not missing));
proc sgplot data=sashelp.heart;
histogram weight;
run;

proc sort data=sashelp.class out=class;by weight;run;
data have;
 set sgplot class(keep=weight in=inb);
 zero=0;
 call streaminit(123);
if inb then percent=rand('integer',1,15);
run;

proc sgplot data=have;
highlow x=BIN_WEIGHT____X low=zero high=BIN_WEIGHT____Y/type=bar barwidth=1;
series x=weight y=percent;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1682165216330.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83039i448DEE17A89589FA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1682165216330.png" alt="Ksharp_0-1682165216330.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Apr 2023 12:07:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Spline-with-superimposed-histogram/m-p/871313#M344170</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-04-22T12:07:05Z</dc:date>
    </item>
    <item>
      <title>Re: Spline with superimposed histogram</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Spline-with-superimposed-histogram/m-p/871353#M344199</link>
      <description>&lt;P&gt;Thank you so much Rick!&amp;nbsp; This works perfectly.&amp;nbsp; And so does the response from KSharp below.&lt;/P&gt;</description>
      <pubDate>Sat, 22 Apr 2023 21:48:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Spline-with-superimposed-histogram/m-p/871353#M344199</guid>
      <dc:creator>PamG</dc:creator>
      <dc:date>2023-04-22T21:48:23Z</dc:date>
    </item>
    <item>
      <title>Re: Spline with superimposed histogram</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Spline-with-superimposed-histogram/m-p/871354#M344200</link>
      <description>&lt;P&gt;This works perfectly and so does the code given by Rick above.&amp;nbsp; Thank you so much Ksharp!&lt;/P&gt;</description>
      <pubDate>Sat, 22 Apr 2023 21:49:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Spline-with-superimposed-histogram/m-p/871354#M344200</guid>
      <dc:creator>PamG</dc:creator>
      <dc:date>2023-04-22T21:49:16Z</dc:date>
    </item>
    <item>
      <title>Re: Spline with superimposed histogram</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Spline-with-superimposed-histogram/m-p/871355#M344201</link>
      <description>&lt;P&gt;Looks like I can't pick two responses as 'Solutions'.&amp;nbsp; I am a new user.&amp;nbsp; Thanks.&lt;/P&gt;</description>
      <pubDate>Sat, 22 Apr 2023 21:51:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Spline-with-superimposed-histogram/m-p/871355#M344201</guid>
      <dc:creator>PamG</dc:creator>
      <dc:date>2023-04-22T21:51:10Z</dc:date>
    </item>
  </channel>
</rss>

