SAS可视化高级ODS图形:PROCSGPLOT,BY组和SG注释
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
SAS可视化高级ODS图形:PROCSGPLOT,BY组和SG注
释
原文链接:/?p=5179
在我们开始之前,让我们更好地了解当您运行PROC SGPLOT以及BY语句和SG注释数据集时会发生什么。
这个例子为每个通过Sex运行PROC SGPLOT生成的图形添加了一个注释。
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
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步骤中没有特定于此示例的内容。