Hi, I would like to use SGPLOT to create multi graphs using By-variable statement
- My code is like that:
Proc sgplot data=database;
by Symptom;
scatter x=VisitNum y=Mean / groupdisplay=cluster group= Treatment;
run;
- This is some of the graphs created:
My question is how could I change the label in the y axis to the name of variable (in this case: Symp1S Symp2S instead of Mean).
Thanks in advance
As far as I am aware there is not way to do what you're asking directly. I would suggest instead writing a short macro loop to do what you're trying to do:
%macro loop;
proc sql noprint;
select distinct symptom,label into :vlist separated by '|',:llist separated by '|' from database;
quit;
%do i = 1 %to %sysfunc(countw(&vlist,|));
proc sgplot data=database;
where symptom="%Qscan(%superq(vlist),&i,|)";
scatter x=VisitNum y=Mean / groupdisplay=cluster group=Treatment;
yaxis label="%qscan(%superq(llist),&i,|,m)";
run;
%end;
%mend;
%loop;
For the above code to work you would need to have another variable in your dataset, which I named label above, to contain a string you want the y-axis label to be. If you are using output statements to make your long version of your dataset you can use the VLABEL function to grab the label from each variable automatically.
If you instead of a dataset where you have the variables in separate columns instead of rows, you can instead make your loop with a %let statement:
%macro loop;
%let vlist=var1 var2 var3 var4;
%do i = 1 %to %sysfunc(countw(&vlist,|));
proc sgplot data=database;
scatter x=VisitNum y=%scan(&vlist,&i,|) / groupdisplay=cluster group=Treatment;
run;
%end;
%mend;
%loop;
This method would then use the labels from the variables themselves to be the y-axis label.
Tjek the YAXIS statemant with the label option
As far as I am aware there is not way to do what you're asking directly. I would suggest instead writing a short macro loop to do what you're trying to do:
%macro loop;
proc sql noprint;
select distinct symptom,label into :vlist separated by '|',:llist separated by '|' from database;
quit;
%do i = 1 %to %sysfunc(countw(&vlist,|));
proc sgplot data=database;
where symptom="%Qscan(%superq(vlist),&i,|)";
scatter x=VisitNum y=Mean / groupdisplay=cluster group=Treatment;
yaxis label="%qscan(%superq(llist),&i,|,m)";
run;
%end;
%mend;
%loop;
For the above code to work you would need to have another variable in your dataset, which I named label above, to contain a string you want the y-axis label to be. If you are using output statements to make your long version of your dataset you can use the VLABEL function to grab the label from each variable automatically.
If you instead of a dataset where you have the variables in separate columns instead of rows, you can instead make your loop with a %let statement:
%macro loop;
%let vlist=var1 var2 var3 var4;
%do i = 1 %to %sysfunc(countw(&vlist,|));
proc sgplot data=database;
scatter x=VisitNum y=%scan(&vlist,&i,|) / groupdisplay=cluster group=Treatment;
run;
%end;
%mend;
%loop;
This method would then use the labels from the variables themselves to be the y-axis label.
Your solution is very helpful sir. It works without any trouble
May I ask for another favor.
Because I'm pretty new to SAS so macro loop does not seem quite familiar with me. So could you please provide more explanation to these line of code ?
As far as I understand, this line of code create 2 macro variablesproc sql noprint; select distinct symptom,label into :vlist separated by '|',:llist separated by '|' from database; quit;
vlist
andllist
So what do "%sysfunc(countw(&vlist,|))", "%Qscan(%superq(vlist),&i,|)" and "%qscan(%superq(llist),&i,|,m)" do in these line of codes?
%do i = 1 %to %sysfunc(countw(&vlist,|));
where symptom="%Qscan(%superq(vlist),&i,|)";
label="%qscan(%superq(llist),&i,|,m)";
Thanks in advance
Thanks a lot for your clear explanation.
May I ask a bit more details about your solution sir.
In my situation, I would like to add one more string to the label and place it in the title of the graph
So far, I just added a title statement into the code like that:
%macro loop;
proc sql noprint;
select distinct symptom,label into :vlist separated by '|',:llist separated by '|' from database;
quit;
%do i = 1 %to %sysfunc(countw(&vlist,|));
proc sgplot data=database;
where symptom="%Qscan(%superq(vlist),&i,|)";
scatter x=VisitNum y=Mean / groupdisplay=cluster group=Treatment;
yaxis label="%qscan(%superq(llist),&i,|,m)";
TITLE "%qscan(%superq(llist),&i,|,m)";
run;
%end;
%mend;
%loop;
And it shows the label on the title.
So how could I join another string to the "%qscan(%superq(llist),&i,|,m)" function to achieve a title like "Symptom: ....(the label)..."
Thanks in advance
How about using a left-justified title2, that "looks like" a yaxis label at the top of the yaxis?
options nobyline;
title1 "Stock prices";
title2 justify=left "#byval(stock)";
proc sgplot data=sashelp.stocks;
by stock;
series x=date y=close;
yaxis display=(nolabel) /*label="#byval(stock)"*/;
xaxis display=(nolabel);
run;
If you want the axis label in the middle of the axis, you could use SAS/Graph gplot.
options nobyline;
title1 "Stock prices";
symbol1 value=none interpol=join;
axis1 label=(angle=90 "#byval(stock)");
axis2 label=none;
proc gplot data=sashelp.stocks;
by stock;
plot close*date / vaxis=axis1 haxis=axis2;
run;
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.