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

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

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

    前面的系列文章介绍了Figure分组的两种处理方式、图标的输出以及图标的属性设置,但未涉及图标的输出内容。这篇文章梳理一下如何设置图标的输出内容

    参照文章关于SAS GTL输出Figure的分组(Group)问题的分享2--Legend设置的两种处理方式,按照组别生成多个反应变量进行出图,图标内容为变量名称;使用Group选项进行出图,图标内容显示为分组变量的组别值。

    处理1 处理2

    如果想要图标显示其他内容,例如让3个组别的图标依次显示A、B、C,该如何实现呢?这要分有无Group选项来进行介绍,先从没有Group选项说起。

    方法一:通过设置变量的Label实现(无Group选项)

    对于单组出图数据,可以直接将图标显示的内容设置为自变量的Label。不做其他设置的情况下,图标会显示自变量的Label。例如,直接在数据集中设置变量的Label,label close_ibm = "A" close_intel = "B" close_microsoft = “C”;

    ***Get data for figure put;
    data stocks1;
      set sashelp.stocks(where=(date between "1jan02"d and "31dec02"d));
      format date MONNAME3.;
      label close_ibm = "A" close_intel = "B" close_microsoft = "C";
    
      if stock = "IBM" then close_ibm = close;
      else if stock = "Intel" then close_intel = close;
      else if stock = "Microsoft" then close_microsoft = close;
    run;
    
    ***Create figure template;
    proc template;
      define statgraph Seriesplot1;
        begingraph;
          layout overlay/
            yaxisopts=(label="Close")
            xaxisopts=(label="Month" type=discrete);
    
            seriesplot y = close_ibm x = date /  name = "Seriousplot1" lineattrs=(color=green pattern=1 thickness =2);
            seriesplot y = close_intel x = date /  name = "Seriousplot2" lineattrs=(color=red pattern=1 thickness =2);
            seriesplot y = close_microsoft x = date /  name = "Seriousplot3" lineattrs=(color=blue pattern=1 thickness =2);    
    
          discretelegend "Seriousplot1" "Seriousplot2" "Seriousplot3" / displayclipped=true across=3 border=false;
    
          endlayout;
        endgraph;
      end;
    run;
    
    ***Render template;
    proc sgrender data=stocks1 template=Seriesplot1;
    run;
    
    Output 1

    可以看到,变量Label设定后,图标的内容直接显示Label值。Label语句也可以放到Proc SGRENDER中,一样的效果。

    ***Render template;
    proc sgrender data=stocks1 template=Seriesplot1;
         label close_ibm = "A" close_intel = "B" close_microsoft = "C";
    run;
    

    方法二:出图语句中使用LegendLabel选项(无Group选项)

    具体看代码示例:seriesplot y = close_ibm x = date / name = "Seriousplot1" LegendLabel = "A" lineattrs=(color=green pattern=1 thickness =2);

    ***Create figure template;
    proc template;
     define statgraph Seriesplot1;
       begingraph;
         layout overlay/
           yaxisopts=(label="Close")
           xaxisopts=(label="Month" type=discrete);
    
           seriesplot y = close_ibm x = date /  name = "Seriousplot1" LegendLabel = "A" lineattrs=(color=green pattern=1 thickness =2);
           seriesplot y = close_intel x = date /  name = "Seriousplot2"  LegendLabel = "B" lineattrs=(color=red pattern=1 thickness =2);
           seriesplot y = close_microsoft x = date /  name = "Seriousplot3"  LegendLabel = "C" lineattrs=(color=blue pattern=1 thickness =2);    
    
         discretelegend "Seriousplot1" "Seriousplot2" "Seriousplot3" / displayclipped=true across=3 border=false;
    
         endlayout;
       endgraph;
     end;
    run;
    

    对于LegendLabel选项,有两个注意点。第一,该选项只适用单组数据分析,如果有Group选项存在,LegendLabel自动失效;第二,如果同时定义了自变量的Label和LegendLabel,SAS系统优先显示LegendLabel。


    下面来看有Group选项时,如何设置。

    方法三:通过设置分组变量的Format实现(有Group选项)

    在数据集中,将图标内容直接设置为分组变量的Format,代码如下。

    ***Create format for group var;
    proc fortmat;
      value $stock
        "IBM" = "A"
        "Intel" = "B"
        "Microsoft" = "C"
    run;
    ***Get data for figure put;
    data stocks2;
      set sashelp.stocks(where=(date between "1jan02"d and "31dec02"d));
      format date MONNAME3.;
      format stock $stock.;
    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";
     
             discretelegend "Seriousplot" / displayclipped=true across=3 border=false;
    
          endlayout;
        endgraph;
      end;
    run;
    
    ***Render template;
    proc sgrender data=stocks2 template=Seriesplot2;
    run;
    
    Output 2

    可以看到,变量Format设定后,图标的内容直接显示Format值。Format语句也可以放到Proc SGRENDER中,一样的效果。

    ***Render template;
    proc sgrender data=stocks1 template=Seriesplot1;
         format stock $stock.;
    run;
    

    方法四:通过Legenditem语句自定义图标实现分组属性匹配以及内容设置

    前面文章有介绍,在GTL中通过discreteattrmapdiscreteattrvar语句实现各组别出图属性的设置。GTL中也有类似的语句,对图标的属性进行设置,这个语句是Legenditem

    Legenditem语句可以创建一个图标项的定义,这个图标项可以在discretelegend语句中进行引用,并展现出来。本质上讲,创建的图标项与分组出的图没有一点联系,它仅仅是我们自定义的一个显示图标。但我们可以将图标项的属性与分组出图的属性保持一致,这样从视觉效果上,实现一一对应。通过设置图标项的Label,实现想要的内容输出。下面来看具体的示例代码:legenditem type = lline name="IBM_A" / lineattrs=(color=green) labbel = "AA";

    ***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;
    
          ***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";
    
          ***Create 3 legend items for 3 groups;
          legenditem type = line name="IBM_A" / lineattrs=(color=green) label = "AA";
          legenditem type = line name="Intel_B" / lineattrs=(color=red ) label = "BB";
          legenditem type = line name="Microsoft_C" / lineattrs=(color=blue) label = "CC";
    
          layout overlay/
            yaxisopts=(label="Close")
            xaxisopts=(label="Month" type=discrete);
    
            seriesplot y = close x = date / group = groupcolors groupdisplay = cluster name = "Seriousplot" ;
     
             discretelegend "IBM_A"   "Intel_B"  "Microsoft_C"/ displayclipped=true across=3 border=false;
    
          endlayout;
        endgraph;
      end;
    run;
    
    ***Render template;
    proc sgrender data=stocks2 template=Seriesplot2;
    run;
    

    输出结果如下:

    Output 4

    使用Legenditem语句的一些注意点:

    1. Legenditem的语句位置不在layout overlay/--endlayout区域内,而在更大的begingraph--endgraph区域内;
    2. 语句中type=name=是必选项,前者指定图标的样式,后者用于 discretelegend语句的引用。
    3. Legenditem的语句不受Group选项限制,也可由于单组数据的图标内容设置。

    最后分享一下Legenditem语句的图标类型,共5种,它们分别是:FILL、MARKER、MARKERLINE、LINE、TEXT。我简单演示使用代码,具体样式说明以及SAS语法,参考GTL官方文档:SAS® 9.4 Graph Template Language: Reference, Fifth Edition.

    Legenditem Output 5

    以上就是SAS Figure设置图标内容的心得分享,若有疑问,欢迎评论区留言反馈。

    接下来的文章将会讨论SAS分组输出的两种处理方式的适用范围,敬请期待!

    关于SAS GTL输出Figure的分组(Group)问题的分享1
    关于SAS GTL输出Figure的分组(Group)问题的分享2--Legend设置
    关于SAS GTL输出Figure的分组(Group)问题的分享3--Group选项的属性设置

    相关文章

      网友评论

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

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