Greetings!
proc sgplot data=example noautolegend;
styleattrs datasymbols=(circlefilled );
scatter x=b y=a ;
keylegend / location=outside position=NW across=1;
run;
For the above code, is it possible to label the y axis using variable "CAT" to make the y-axis more meaningful?
CAT | a | b |
CAT1 | 1 | 2 |
CAT2 | 2 | 3 |
CAT3 | 3 | 5 |
CAT4 | 4 | 1 |
CAT5 | 5 | 6 |
CAT6 | 6 | 4 |
I might suggest simply trying y=cat as a first simple to test code.
When a character variable is used for a yaxis like that the first alphabetic value will be closest to the x axis and be in ascending alphabetical value from bottom to top.
If that isn't sufficient then I would use your A and Cat values to make a format then apply the format to the A variable in the plot.
proc format; value AtoCat 1= 'Cat 1' 2= 'Cat 2' <repeat as needed> ;
proc sgplot data=example noautolegend;
styleattrs datasymbols=(circlefilled );
scatter x=b y=a ;
keylegend / location=outside position=NW across=1;
format a AtoCat. ;
run;
If you have LOTS of A values you may need to use a YAXIS statement to make the axis TYPE=Discrete and set a FITPOLICY=none to force all the values to have a tick mark to label.
I might suggest simply trying y=cat as a first simple to test code.
When a character variable is used for a yaxis like that the first alphabetic value will be closest to the x axis and be in ascending alphabetical value from bottom to top.
If that isn't sufficient then I would use your A and Cat values to make a format then apply the format to the A variable in the plot.
proc format; value AtoCat 1= 'Cat 1' 2= 'Cat 2' <repeat as needed> ;
proc sgplot data=example noautolegend;
styleattrs datasymbols=(circlefilled );
scatter x=b y=a ;
keylegend / location=outside position=NW across=1;
format a AtoCat. ;
run;
If you have LOTS of A values you may need to use a YAXIS statement to make the axis TYPE=Discrete and set a FITPOLICY=none to force all the values to have a tick mark to label.
Does a scatter plot make sense for your data? Also
Either way, another option is to list the values on the XAXIS statement but the FORMAT is definitely the better answer.
proc sql noprint;
select quote(cat) into :labels_list separated by " " from example order by a;
select quote(a) into :values_list separated by " " from example order by a;
quit;
Then add this to your PROC SGPLOT.
XAXIS VALUES = (&values_list.) VALUESDISPLAY = (&labels_list.);
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.