zoukankan      html  css  js  c++  java
  • SAS代写高级ODS图形:PROC SGPLOT,BY组和SG注释

    原文链接:http://tecdat.cn/?p=5179

     

    在我们开始之前,让我们更好地了解当您运行PROC SGPLOT以及BY语句和SG注释数据集时会发生什么。这个例子为每个通过Sex运行PROC SGPLOT生成的图形添加了一个注释,单词“Students”。

    proc sort data=sashelp.class out=c;
    
      by sex;
    
    run;
    
     
    
    data anno1;
    
      retain x1 20 y1 85 function 'Text' dataspace 'GraphPercent' width 100;
    
      label = 'Students'; output;
    
    run;
    
     
    
    proc sgplot data=c sganno=anno1 tmplout='tmp1.tmp';
    
      scatter y=weight x=height;
    
      by sex;
    
    run;

     

     

    不需要TMPLOUT =选项。但是,它显示了PROC SGPLOT编写的用于制作图形的模板。如果您真的想了解PROC SGPLOT的作用,您需要了解模板。它存储在文件tmp1.tmp中,如下所示(添加缩进后)。

    proc template;
    
      define statgraph sgplot;
    
          dynamic __BYLINE__;
    
          begingraph / collation=binary;
    
            EntryTitle __BYLINE__ / textattrs=(size=GraphLabelText:fontsize);
    
            layout overlay / yaxisopts=(labelFitPolicy=Split)
    
                              y2axisopts=(labelFitPolicy=Split);
    
                ScatterPlot X=Height Y=Weight / subpixel=off primary=true
    
                          LegendLabel="Weight" NAME="SCATTER";
    
                DrawText  "Students" / X=20 Y=85 WIDTH=100;
    
            endlayout;
    
          endgraph;
    
      end;
    
    run;

    出于我们的目的,我想指出的是DRAWTEXT语句。它提供了注释。虽然PROC SGRENDER接受SGANNO =数据集,但不是这个图形的创建方式。相反,PROC SGPLOT读取SG注释数据集并将每一行转换为GTL DRAW语句。

    现在考虑SG注释数据集,其具有与DATA =数据集中的BY变量匹配的BY变量。

    data anno2;
    
      x1 = 20; y1 = 85; function = 'Text'; dataspace = 'GraphPercent'; width = 100;
    
      label = 'Female Students'; Sex = 'F'; output;
    
      label = 'Male Students';  Sex = 'M'; output;
    
    run;

    如果您使用BY语句和SGANNO =选项运行PROC SGPLOT,则在两个图中都会同时获得两个注释,这几乎肯定不是您想要的。

    proc sgplot data=c sganno=anno2 tmplout='tmp2.tmp';
    
      scatter y=weight x=height;
    
      by sex;
    
    run;

     

     

    这是文件tmp2.tmp,其中包含生成的GTL:

    proc template;
    
      define statgraph sgplot;
    
          dynamic __BYLINE__;
    
          begingraph / collation=binary;
    
            EntryTitle __BYLINE__ / textattrs=(size=GraphLabelText:fontsize);
    
            layout overlay / yaxisopts=(labelFitPolicy=Split)
    
                              y2axisopts=(labelFitPolicy=Split);
    
                ScatterPlot X=Height Y=Weight / subpixel=off primary=true
    
                            LegendLabel="Weight" NAME="SCATTER";
    
                DrawText  "Female Students" / X=20 Y=85 WIDTH=100;
    
                DrawText  "Male Students" / X=20 Y=85 WIDTH=100;
    
            endlayout;
    
          endgraph;
    
      end;
    
    run;

    现在有两个DRAWTEXT语句。两者都是无条件使用的。因此,如果我们想在每个图中使用不同的注释,我们必须以其他方式处理这个问题。如果要为每个图形添加不同的文本,则不需要SG注释。您可以修改输入数据集并在PROC SGPLOT中使用TEXT语句。

    data c2;
    
      set c;
    
      by sex;
    
      if first.sex and sex eq 'F' then do;
    
          x1 = 51;  y1 = 104; Label = 'Female';
    
          end;
    
      else if first.sex and sex eq 'M' then do;
    
          x1 = 56;  y1 = 140; Label = 'Male';
    
          end;
    
      else call missing(label,x1,y1);
    
    run;
    
     
    
    proc sgplot data=c2;
    
      scatter y=weight x=height;
    
      text y=y1 x=x1 text=label;
    
      by sex;
    
    run;

     

     

    使用这种方法以及TEXT和POLYGON语句可以做很多事情,而不需要SG注释。尽管如此,SG注释非常有用,与TEXT和POLYGON语句不同,它为您提供了各种坐标系。

    接下来,我们将创建一个SG注释数据集以及一个ID变量(名为ID),其值与BY变量Sex相匹配。

    data anno3;
    
      x1 = 20; y1 = 85; function = 'Text'; dataspace = 'GraphPercent'; width = 100;
    
      label = 'Female Students'; id = 'F'; output;
    
      label = 'Male Students';  id = 'M'; output;
    
    run;
    
    现在,PROC SGPLOT仅用于将模板写入文件tmp3.tmp。
    
    proc sgplot data=c tmplout='tmp3.tmp';
    
      ods exclude sgplot;
    
      scatter y=weight x=height;
    
      by sex;
    
    run;

    这是文件(不添加任何缩进)。

    proc template;
    
    define statgraph sgplot;
    
    dynamic __BYLINE__;
    
    begingraph / collation=binary;
    
    EntryTitle __BYLINE__ / textattrs=(size=GraphLabelText:fontsize);
    
    layout overlay / yaxisopts=(labelFitPolicy=Split) y2axisopts=(labelFitPolicy=Split);
    
      ScatterPlot X=Height Y=Weight / subpixel=off primary=true LegendLabel="Weight" NAME="SCATTER";
    
    endlayout;
    
    endgraph;
    
    end;
    
    run;

    您可以使用DATA步骤编辑此模板并将其提交给SAS。下面的语句添加了一个PROC TEMPLATE语句,将模板名称从sgplot更改为by,并添加动态变量和ANNOTATE语句。

    data _null_;
    
      infile 'tmp3.tmp';
    
      input;
    
      if _n_ eq 1 then call execute('proc template;');
    
      _infile_ = tranwrd(_infile_, 'sgplot;', 'by;');
    
      call execute(_infile_);
    
      if find(_infile_, 'layout overlay') then
    
          call execute('dynamic _byval_; annotate / id=_byval_;');
    
    run;

    您可以提交以下语句以查看已编辑的模板。

    proc template; source by; quit;

    接下来显示编译的模板。

    define statgraph By;
    
      dynamic __BYLINE__ _byval_;
    
      begingraph / collation=binary;
    
          EntryTitle __BYLINE__ / textattrs=(size=GraphLabelText:fontsize);
    
          layout overlay / yaxisopts=(labelFitPolicy=Split) y2axisopts=(
    
            labelFitPolicy=Split);
    
            annotate / id=_BYVAL_;
    
            ScatterPlot X=HEIGHT Y=WEIGHT / subpixel=off primary=true LegendLabel=
    
                "Weight" NAME="SCATTER";
    
          endlayout;
    
      endgraph;
    
    end;

    请注意,编译的模板与原始模板不完全匹配。特别是,DYNAMIC语句被组合在一起。使用PROC TEMPLATE查看模板的一个好处是模板很好地缩进。

    请注意,动态变量_byval_与ID =值匹配。

    现在,您可以使用PROC SGRENDER以及SGANNO =选项和BY语句来为每个图形获取单独的注释。

    title;
    
    options nobyline;
    
    proc sgrender data=c template=by sganno=anno3; by sex; run;
    
    options byline;

     

     

    替代方法要求您使用GTL编写图形模板,然后使用PROC SGRENDER。虽然这种PROC SGPLOT方法有更多步骤,但它更容易。在PROC SGPLOT中指定如何构建图形比从头开始编写模板更容易。您将需要一个DATA步骤来修改该模板,但这并不难,因为您可以简单地复制我的代码。修改模板的DATA步骤中没有特定于此示例的内容。

    如果您有任何疑问,请在下面发表评论。

     
  • 相关阅读:
    合并本地多次commit为一个commit
    git 取消文件跟踪
    遍历进程 遍历窗口
    linux查看程序运行参数
    ubuntu下载地址
    将博客搬至CSDN
    extern"C"的使用
    ESP32-NVS存储(非易失性存储库)
    ESP32-EEPROM存储
    c语言简单数据类型
  • 原文地址:https://www.cnblogs.com/tecdat/p/9317968.html
Copyright © 2011-2022 走看看