楼主: caibirdcnb
19771 24

[问答] 请问高手用SAS如何做出以下图表,非常感谢! [推广有奖]

  • 0关注
  • 36粉丝

讲师

47%

还不是VIP/贵宾

-

威望
0
论坛币
1367 个
通用积分
16.5538
学术水平
67 点
热心指数
70 点
信用等级
64 点
经验
6762 点
帖子
206
精华
2
在线时间
433 小时
注册时间
2011-8-31
最后登录
2023-9-23

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
有个说法是如果SAS是AK47,那么EXCEL就是玩具枪。实际应用中我也能感觉到SAS的强大和高效。但不知道为什么,在作图这一块,总感觉SAS太僵化了一些。
例如下面2种图表,我能在EXCEL实现,但不知道如何在SAS中实现,请高手指导,非常感谢!

柱状趋势线图:注意每周里,柱状图是左右放的,而趋势线是上下放的。(似乎SAS无法做到这一点,最多柱状一个胖一个瘦叠在一起)

趋势图:注意同一周不同的点是横着往右走的。 (在SAS里,同一周不同的点是竖着上下放的)

谢谢!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:非常感谢 EXCEL exce xcel 实际应用 如何

overlay.jpg (131.22 KB)

overlay.jpg

barline.jpg (131.81 KB)

barline.jpg

本帖被以下文库推荐

沙发
新人2012 发表于 2012-12-30 18:24:39 |只看作者 |坛友微信交流群
对你的问题不感兴趣,推荐一本书:statistical graphics in sas
只有你想不到的,没有SAS做不到的。
已有 1 人评分经验 论坛币 学术水平 收起 理由
Imasasor + 12 + 12 + 1 热心帮助其他会员

总评分: 经验 + 12  论坛币 + 12  学术水平 + 1   查看全部评分

使用道具

藤椅
bobguy 发表于 2012-12-31 00:10:10 |只看作者 |坛友微信交流群
The gbarline can plot bars and line together. Here is an example for your problem.

data trend;
input week        yield        input;
n+1;
if n<=6 then flag='up  ';
else flag='down';
wkflag=catt(week,'_',flag);
if flag='up  ' then yield_up=yield;
else yield_down=yield;
cards;
1        92                360       
2        95                460       
3        81                560       
4        91                360       
5        85                460       
6        90                560       
1        90                644       
2        91                244       
3        78                344       
4        87                544       
5        92                244       
6        88                544       
;
proc print;run;

proc sort data=trend; by week flag; run;

goptions reset=all border;
axis1 label=(j=c  j=c ) minor=none; /* left */
axis2 label=(j=c  j=c  j=c ) minor=none; /* right */
axis3 label=none; /* bottom */
/* Bar legend */
legend1 position=(middle right outside)  across=1
        label=(position=(top )  j=l );
/* Line plot legend */
legend2 position=(bottom right outside) across=1 repeat=1
        label=(position=(top) j=l  "Lines:") ;
symbol1 c=black value=circle;
symbol2 value=dot;
proc gbarline data=trend;
   bar wkflag/ discrete  sumvar=input subgroup=flag
              raxis=axis1 maxis=axis3 legend=legend1
              ;

   plot / sumvar=yield_up    legend=legend2 axis=axis2;
   plot / sumvar=yield_down  legend=legend2 axis=axis2;
run;
quit;

使用道具

板凳
ziyenano 发表于 2012-12-31 00:33:37 |只看作者 |坛友微信交流群
SAS的作图一向不以简洁著称,而是以其全面性,基本可以画任何需要的图形;
而想真正发挥graph的实力,还需要需要学习annote的语法;
倒是SAS不能像R或者matlab那样,能够在画好的图形上进行任何操作,
虽然9.2推出了graphics editor,但是所添的功能也着实有限
已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
Imasasor + 12 + 60 + 1 + 1 + 1 牛人,懂sas graph的人很少

总评分: 经验 + 12  论坛币 + 60  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

使用道具

报纸
bobguy 发表于 2012-12-31 00:38:46 |只看作者 |坛友微信交流群
If you have sgplot licensed, it will be much easier.

data trend;
input week        yield        input;
n+1;
if n<=6 then flag='up  ';
else flag='down';

cards;
1        92                360       
2        95                660       
3        81                560       
4        91                360       
5        85                460       
6        90                560       
1        90                644       
2        91                444       
3        78                344       
4        87                544       
5        92                444       
6        88                544       
;
proc print;run;

proc sgplot data=trend;
  vbar week / response=yield group=flag BARWIDTH=0.6
   GROUPDISPLAY= CLUSTER CLUSTERWIDTH=0.8;
  vline week / response=input group=flag  GROUPDISPLAY=OVERLAY  y2axis ;
run;

使用道具

地板
ziyenano 发表于 2012-12-31 00:42:23 |只看作者 |坛友微信交流群
现有bar-line模块好像不能将柱状图左右放,
用gplot变通一下,可以达到以上的效果:
data ex;
input
week $ yield_up:percent5.1 input_up yield_down:percent5.1 input_down;
format yield_up percent5.1 yield_down percent5.1;
cards;
wk01 92.0% 160 90.0% 644
wk02 93.0% 346 92.0% 740
wk03 94.0% 632 93.0% 230
wk04 95.0% 780 94.0% 173
wk05 95.0% 699 94.0% 173
wk06 96.0% 46  95.0% 151
;
run;
data ex1;
set ex;
n=_n_;
n1=n-0.1;
n2=n+0.1;
run;
proc sql noprint;
select cat('t=',n+1,' ',quote(compress(week))) into:value separated by ' ' from ex1;
quit;

goptions reset=all;
symbol1 i=join v=square c=bule ;
symbol2 i=join v=triangle c=red;
symbol3 i=needle v=none c=black w=30;
symbol4 i=needle v=none c=vibg  w=30;
axis1 offset=(0,0) value=(t=1 "" &value t=8 "") order=(0 to 7 by 1) minor=none label=("Week");
axis2 order=(0.87 to 0.97 by 0.01) minor=none label=("Yield");
axis3 order=(0 to 900 by 100) minor=none label=("Input");
legend1 across=2 down=1 position=(bottom center outside) noframe label=("");
legend2 across=2 down=1 position=(bottom center outside) noframe label=("");
proc gplot data=ex1;
plot yield_up*n1=1 yield_down*n2=2/overlay haxis=axis1 vaxis=axis2 autovref legend=legend1 ;
plot2 input_up*n1=3 input_down*n2=4/overlay legend=legend2 vaxis=axis3;
run;
quit;

使用道具

7
caibirdcnb 发表于 2012-12-31 02:33:13 |只看作者 |坛友微信交流群
  1. 非常感谢bobguy和ziyenano两位高手的指导。特别bobguy一直以来给了我很大的帮助。
复制代码
在这个bar-line例子中,我认为两位高手的代码还存在一点点瑕疵。其中bobguy 的图形能实现柱状图左右分开,趋势线上下叠加,但是无法实现良率用线图,数量用柱图。(反过来实现的话,良率就必须放在右轴,而不是左轴)
ziyenano 的图形是柱图和线图的点都是左右分开,而不是线图上下叠加。

上面代码是我认真学习帮助文档后的成绩,能完美实现要求的图形。不仅如此,也是定制作图的一般步骤。
第一步是恢复SAS默认模板。
第二步是定义一个公共模板,主要控制图形输出外观相关,例如用group=选项的时候,控制线条颜色。
第三步是定义一个画图模板,主要是定义生成什么图形,需要什么要素以及如何摆放。
第四步就是用上面定义好的模板画图。

但是,关于如何画趋势散点图的另外一个例子,目前我还没想到好办法,请各位继续指导,谢谢!



/*******************************************************************************************/
data test;
input Week $ UP_DOWN $ Yield Input;
datalines;
wk01        U        0.75        5452
wk01        D        0.67958        4519
wk02        U        0.74241        4313
wk02        D        0.83931        3765
wk03        D        0.84109        22843
wk05        D        0.79949        2339
wk06        U        0.62222        225
wk09        U        0.85874        2230
wk09        D        0.82832        6215
wk10        U        0.68742        7387
wk10        D        0.80633        8184
wk11        U        0.77253        12182
wk11        D        0.79991        10615
wk12        U        0.75884        13157
wk12        D        0.80153        4842
wk13        U        0.75761        3911
wk13        D        0.84413        1219
wk14        U        0.70016        2538
;
run;

/*******************************************************************************************/
%let _color=blue red green orange purple darkgrey
        lightvividblue lightvividred lightvividgreen darkyellow violet brown;
%let _marker=circle circle circle circle circle circle
        circle circle circle circle circle circle;
%let _line=1 1 1 1 1 1 1 1 1 1 1 1;

%macro colorList(_var=);
%do i=1 %to 12;
        "&_var&i"=%scan(&_color,&i)
%end;
%mend colorList;

%macro markerList(_var=);
%do i=1 %to 12;
        class &_var&i / markersymbol="%scan(&_marker,&i)" linestyle=%scan(&_line,&i);
%end;
%mend markerList;


/*******************************************************************************************/
options nolabel;
ods path sashelp.tmplmst(read) work.templat(update);

/*******************************************************************************************/
proc template;
        define style trend;
           parent=Styles.Default;
        class GraphFonts
                "Fonts used in graph styles" /
                'GraphDataFont'=("Arial",8pt)
                'GraphUnicodeFont'=("Arial",10pt)
                'GraphValueFont'=("Arial",12pt)
                'GraphLabelFont'=("Arial",12pt)
                'GraphAnnoFont'=("Arial",10pt)
                'GraphFootnoteFont'=("Arial",12pt)
                'GraphTitleFont'=("Arial",14pt,bold);
        class color_list
                "Colors used in the default style" /
                'bgA'=lightgray;
        class GraphBorderLines /
                linethickness=2px;
        class GraphColors /
                'ggrid'=lightgray
                'glabel'=black
                'gcdata'=blue
                'gwalls'=lightyellow
                %colorList(_var=gdata)
                %colorList(_var=gcdata);
        %markerList(_var=GraphData);
        end;
run;

/*******************************************************************************************/
proc template;
        define statgraph temp;
    begingraph;

    entrytitle 'Yield & Input By UP_DOWN';
    entryfootnote 'MIB Confidential, by Smoon team';

    layout overlay / yaxisopts=(griddisplay=on);
                barchart x=Week y=Input / group=UP_DOWN groupdisplay=cluster name="I"
                        yaxis=y2 fillattrs=(transparency=0.7) barwidth=1 clusterwidth=0.5;
        seriesplot x=Week y=Yield / group=UP_DOWN groupdisplay=overlay name="Y"
                        display=all lineattrs=(thickness=2) markerattrs=(size=12) break=true;
       
            layout gridded / columns=1 autoalign=(topleft topright);
                     entry halign=left textattrs=(color=blue) "UP weighted average = 75%";
                        entry halign=left textattrs=(color=red) "DN weighted average = 80%";
            endlayout;
    endlayout;

    layout globallegend / type=column;
        discretelegend "Y" / title="Yield(%)";
        discretelegend "I" / title="Input(Kpcs)";
    endLayout;

        endgraph;                                                         
        end;
run;

ods listing close;
ods html style=trend; *file='Yield.html' path='G:\SAS';
ods graphics on / reset=all width=9in height=4.5in;

proc sgrender data=test template=temp;
run;

ods html close;
ods listing;

















使用道具

8
ziyenano 发表于 2012-12-31 09:38:17 |只看作者 |坛友微信交流群
bobguy 发表于 2012-12-31 00:38
If you have sgplot licensed, it will be much easier.

data trend;
貌似9.3才支持groupdisplay

使用道具

9
ziyenano 发表于 2012-12-31 09:39:37 |只看作者 |坛友微信交流群
caibirdcnb 发表于 2012-12-31 02:33
在这个bar-line例子中,我认为两位高手的代码还存在一点点瑕疵。其中bobguy 的图形能实现柱状图 ...
template 是个好方法,
只是觉得语法烦了点,时间久了,就容易忘记,无奈啊

使用道具

10
冰雨狼 发表于 2012-12-31 10:45:37 |只看作者 |坛友微信交流群
了解一下。

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-24 21:51