0
votes

This is related to my previous question : Escape characters interpreted as array in SAS

I have managed to create the variable with special character (despite not a good practice, this is required).

Now I am trying to plot the respective variable using sgplot, but again having a difficulty to fetch the Age(years) variable , as it contains the special character of 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;

In the above code, sgplot does not work because of the following line

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

The aim is to display 'Age(years)' in the graph.

How can I allow the yaxistable of sgplot to read 'Age(years)' as a variable and display this correctly on the graph?

1
Do you "need" that variable to be called Age(years) for display purposes? Couldn't you just keep the variable as age_years and have it displayed as Age(years) by using a label? - user2877959
@user2877959 yes I have thought the same. it is not "needed" for the name of the variable , but only for the label. let's say I rename the var to age_years but how can I change the "yaxistable" to use var "age_years" and display this as label "age(years)" ? - sateayam
if you give the variable a label in your table, sgplot will use it by default in the output report (see my answer below). Another option is to explicit the label with the LABEL= option of the yaxistable statement. - user2877959
@user2877959 thanks for pointing out about the LABEL! - sateayam

1 Answers

1
votes

How about this?

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;

Edit: this can then of course be simplified as

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;