【问题标题】:SAS---how to create histograms plot with specific bin in certain rangeSAS---如何在特定范围内创建具有特定 bin 的直方图
【发布时间】:2026-02-13 05:30:01
【问题描述】:

我尝试通过proc univariate 创建直方图。目标是使用从 0 到 1.5 的 0.1 宽度的 bin 来创建分布,然后将所有剩余的 bin 放在一个 bin 中。

我应用了以下代码来识别从 0 到 1.5 的范围,而它无法管理其余的范围。如何更正代码?

proc univariate data=HAVE;
where pred between 0 and 1.5;
var pred;
histogram pred/ vscale=percent midpoints=0 to 2 by 0.1 normal (noprint);
run;

【问题讨论】:

    标签: sas histogram


    【解决方案1】:

    您可以尝试以下代码,通过从一个变量创建两个变量来组合两个直方图:

    /*Temporary DS with values ranging from 01. to 2.0*/
    data have;
    do i=0.1 to 2.0 by 0.1;
        output;
    end;
    rename i=pred;
    run;
    
    /*Creating two variables x(0.1-1.5) and y(1.6-2.0)*/
    data have;
     set have;
     if pred<1.6 then x=pred;
     else y=pred;
     drop pred;
    run;
    
    /*Combine two Histograms*/
    proc sgplot data=have;
       histogram x / nbins=15 binwidth=0.1;
       density x / type=normal;
       histogram y / nbins=5 binwidth=1.0;
       density y / type=normal;
       keylegend / location=inside position=topright noborder across=2;
       xaxis display=(nolabel) values=(0.1 to 2.5 by 0.1);
    run;
    

    【讨论】:

      【解决方案2】:
      1. 创建自己的群组
      2. 创建格式以显示您想要的方式
      3. 用 SGPLOT 绘制它

        *create your own groups for data, especially the last group;
        data mileage;
        set sashelp.cars;
        mpg_group=floor(mpg_highway / 10);
        
        if mpg_group in (5, 6, 7) then
            mpg_group=5;
        keep mpg_highway mpg_group;
        run;
        
        *format to control display;
        
        proc format;
        value mpg_fmt 1='0 to 10' 2='11 to 20' 3='21 to 30' 4='31 to 40' 5='40+';
        run;
        
        *plot the data;
        proc sgplot data=mileage;
        vbar mpg_group /stat=freq barwidth=1;
        format mpg_group mpg_fmt.;
        run;
        

      【讨论】:

        最近更新 更多