【问题标题】:SAS sgplot using variable with array symbol as special characterSAS sgplot使用带有数组符号作为特殊字符的变量
【发布时间】:2017-08-14 10:58:16
【问题描述】:

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

我已经设法创建了带有特殊字符的变量(尽管这不是一个好习惯,但这是必需的)。

现在我正在尝试使用 sgplot 绘制相应的变量,但再次难以获取 Age(years) 变量,因为它包含数组 () 的特殊字符

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 读取“年龄(年)”作为变量并在图表上正确显示?

【问题讨论】:

  • 您是否“需要”将该变量称为 Age(years) 以用于显示目的?您不能将变量保留为age_years 并使用标签将其显示Age(years)
  • @user2877959 是的,我也这么认为。变量的名称不是“需要”的,而只是标签。假设我将 var 重命名为 age_years 但如何将“yaxistable”更改为使用 var“age_years”并将其显示为标签“age(years)”?
  • 如果你在表格中给变量一个标签,sgplot 将在输出报告中默认使用它(见下面我的回答)。另一种选择是使用yaxistable 语句的LABEL= 选项显式标签。
  • @user2877959 感谢您指出标签!

标签: sas


【解决方案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;

【讨论】:

    猜你喜欢
    • 2016-06-09
    • 2021-03-29
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多