SAS sgplot使用带有数组符号的变量作为特殊字符

时间:2017-08-14 10:58:17

标签: sas

这与我之前的问题有关:Escape characters interpreted as array in SAS

我已设法创建具有特殊字符的变量(尽管不是很好的做法,但这是必需的。)

现在我试图使用sgplot绘制相应的变量,但又难以获取Age(years)变量,因为它包含array()的特殊字符

data have;
   input Name $ Gender $ Age_years Health $;
   datalines;
     Dino male 6 sick
     Rita female 5 healthy
     Aurora female 4 healthy
     Joko male 3 sick
   ;
run;


options validvarname=any;
data want;
  set have;
  'Age(years)'n=Age_years; 
run;

options validvarname=any;
ods graphics / reset width=4in height=3in;
proc sgplot data=want noautolegend;

   *Error occurs here*;  
   yaxistable Gender 'Age(years)'n / 
         labelattrs=GraphUnicodeText(weight=bold size=9pt) 
         labelhalign=center position=left location=outside; 

   *Error occurs here*;
   scatter y='Age(years)'n x=Health;

   xaxis offsetmin=0.1 offsetmax=1 label="Health Status" 
   labelattrs=GraphUnicodeText(weight=bold size=9pt) ;
   yaxis offsetmax=0.1 display=none;

run;

在上面的代码中,由于以下行

,sgplot不起作用
   yaxistable Gender 'Age(years)'n / 
         labelattrs=GraphUnicodeText(weight=bold size=9pt) 
         labelhalign=center position=left location=outside;

目的是展示年龄(岁月)'在图中。

如何让sgplot的yaxistable读取'年龄(岁月)'作为变量并在图表上正确显示?

1 个答案:

答案 0 :(得分:1)

这个怎么样?

data have;
   input Name $ Gender $ Age_years Health $;
   datalines;
     Dino male 6 sick
     Rita female 5 healthy
     Aurora female 4 healthy
     Joko male 3 sick
   ;
run;


options validvarname=any;
data want;
  set have;
  label age_years='Age(years)'; /*only line changed from your code*/
run;

options validvarname=any;
ods graphics / reset width=4in height=3in;
proc sgplot data=want noautolegend;

   yaxistable Gender age_years / 
         labelattrs=GraphUnicodeText(weight=bold size=9pt) 
         labelhalign=center position=left location=outside; 

   scatter y=age_years x=Health;

   xaxis offsetmin=0.1 offsetmax=1 label="Health Status" 
   labelattrs=GraphUnicodeText(weight=bold size=9pt) ;
   yaxis offsetmax=0.1 display=none;

run;

编辑:这当然可以简化为

data want;
   input Name $ Gender $ Age_years Health $;
   label age_years='Age(years)';
   datalines;
     Dino male 6 sick
     Rita female 5 healthy
     Aurora female 4 healthy
     Joko male 3 sick
   ;
run;

ods graphics / reset width=4in height=3in;
proc sgplot data=want noautolegend;

   yaxistable Gender age_years / 
         labelattrs=GraphUnicodeText(weight=bold size=9pt) 
         labelhalign=center position=left location=outside; 

   scatter y=age_years x=Health;

   xaxis offsetmin=0.1 offsetmax=1 label="Health Status" 
        labelattrs=GraphUnicodeText(weight=bold size=9pt) ;
        yaxis offsetmax=0.1 display=none;

run;