美文网首页
关于SAS GTL输出Figure的分组(Group)问题的分享

关于SAS GTL输出Figure的分组(Group)问题的分享

作者: 野藤_ | 来源:发表于2021-01-11 17:24 被阅读0次

前面文章中,分享了关于分组数据两种方式的出图以及图标设置的处理,其中留了一个问题,使用Group选项下,图形的各分组属性如何设置。下面介绍如何进行编程处理。

通常,对图形的属性设置,我们可以直接在出图语句中的选项中进行设置,例如,前面文章中使用3个出图语句生成3组不同的图形。但是,如果使用Group选项后,直接在出图语句中设置属性,会覆盖SAS自动为每个分组设置的属性,导致3个图形的属性相同,没有区分。代码和例如如下:

***Get data for figure put;
data stocks2;
  set sashelp.stocks(where=(date between "1jan02"d and "31dec02"d));
  format date MONNAME3.;
run;

***Create figure template;
proc template;
  define statgraph Seriesplot2;
    begingraph;
      layout overlay/
        yaxisopts=(label="Close")
        xaxisopts=(label="Month" type=discrete);

        seriesplot y = close x = date / group = stock groupdisplay = cluster name = "Seriousplot" lineattrs=(color=green pattern=1 thickness =2);
 
         discretelegend "Seriousplot" / displayclipped=true across=3 border=false;

      endlayout;
    endgraph;
  end;
run;

***Render template;
proc sgrender data=stocks2 template=Seriesplot2;
run;
Output

这时候,就需要对每一个组别进行单独的属性设置。提前设置好分组变量每一个值对应的图形属性,出图时直接调用,这需要discreteattrmapdiscreteattrvar语句来实现。

discreteattrmap语句是用于创建一个属性映射,该属性映射将图形属性离散值匹配。属性映射可以与图中的分类变量相关联。设定各分组属性时,选项ignorecase = true不区分分组变量的大小写;当然,最好设置离散值与分组变量值一致。例如:

***define the attribute map and assign the name  "Linecolors";
discreteattrmap name = "Linecolors" / ignorecase = true;
  value "IBM" / lineattrs=(color=green );
  value "Intel" /  lineattrs=(color=red );
  value "Microsoft" /  lineattrs=(color=blue);
enddiscreteattrmap;

discreteattrvar语句是用来在已定义的属性映射包含对应分类值的分类变量之间创建一个关联。attrvar=选项指定属性映射的关联名称,var=选项指定输入列,attrmap=指定已经定义好的属性映射名称。例如:

***associate the attribute map with input data column Sex and assign the name "groupmarkers" "groupcolors" to the named association;
discreteattrvar attrvar = groupcolors var = stock attrmap = "Linecolors";

具体如何应用?在Template绘图语句中,group=选项使用已经关联组别属性映射的名称,如以下示例代码中的 group = groupcolors。完整的代码如下:

***Create figure template;
proc template;
  define statgraph Seriesplot2;
    begingraph;

      ***define the attribute map and assign the name  "Linecolors";
      discreteattrmap name = "Linecolors" / ignorecase = true;
          value "IBM" / lineattrs=(color=green );
          value "Intel" /  lineattrs=(color=red );
          value "Microsoft" /  lineattrs=(color=blue);
      enddiscreteattrmap;

      ***associate the attribute map with input data column Sex and assign the name "groupmarkers" "groupcolors" to the named association;
      discreteattrvar attrvar = groupcolors var = stock attrmap = "Linecolors";

      layout overlay/
        yaxisopts=(label="Close")
        xaxisopts=(label="Month" type=discrete);

        seriesplot y = close x = date / group = groupcolors groupdisplay = cluster name = "Seriousplot" ;
 
         discretelegend "Seriousplot" / displayclipped=true across=3 border=false;

      endlayout;
    endgraph;
  end;
run;

***Render template;
proc sgrender data=stocks2 template=Seriesplot2;
run;

输出结果如下:


Output2

可以看到,不同组别折线图的属性已经设置好了。这里有一些注意点:

  1. discreteattrmap以及discreteattrvar的语句位置不在layout overlay/--endlayout区域内,而在更大的begingraph--endgraph区域内;
  2. discreteattrmap语句定义好离散值属性后,需要用discreteattrvar语句将离散值属性与分组变量相连接,并定义一个类似于分组变量角色新名称
  3. 定义模板时,Group选项值不再使用原先数据中的分组变量,而是用“新名称”;
  4. 即便定义好各分组的属性,如果出图语句中依旧使用属性定义选项,也会覆盖定义好的各分组属性。

以上就是Group选项的属性设置,要分享的内容。若有疑问欢迎评论区留言反馈。

接下来的文章,将会分享Figure分组输出后,图标(Legend)的进一步设置的相关问题。

相关文章:
关于SAS GTL输出Figure的分组(Group)问题的分享1
关于SAS GTL输出Figure的分组(Group)问题的分享2--Legend设置
关于SAS GTL输出Figure的分组(Group)问题的分享4--图标(Legend)内容的设置

相关文章

网友评论

      本文标题:关于SAS GTL输出Figure的分组(Group)问题的分享

      本文链接:https://www.haomeiwen.com/subject/vvydaktx.html