I need to add a second x-axis on a plot using PROC SGPLOT in Sas. It is a time series with vline (because it is mean values with error bars). My dataset has the following columns:
ID (subjects)
treatment
time_elapsed (num)
clocktime (num)
Response.
Here is my code:
proc sgplot data=mydata ;
vline time_elapsed / response=Response stat=mean limitstat=stderr numstd=2
/ group=condition ;
xaxis label="Time elapsed [hours]" values=(0 to 9 by 1);
This gives my elapsed time on the x-axis. I would like to have another x-axis, maybe on top of the plot, with "clock time" - that is on the same scale (hours). I can not do so:
proc sgplot data=mydata ;
vline time_elapsed / response=Response stat=mean limitstat=stderr numstd=2
/ group=condition ;
vline clocktime / response=Response stat=mean limitstat=stderr numstd=2
/ group=condition x2axis;
Because the category variable should be the same. And I must use "vline" to be able to represent mean values and error bars. Any other idea?
Thank you!
Your best option would be to use PROC MEANS or PROC SUMMARY to calculate the numbers for each overlay, merge the results into one data set, and use SERIES and SCATTER plots (with error bars) to display the result. This following code is uses this technique with some sample data. Just substitute your data and variables. Let me know if you have any questions about the details.
Thanks!
Dan
proc summary data=sashelp.heart nway;
class weight_status;
var cholesterol;
output out=weight mean=chol_wgt stderr=se_wgt;
run;
proc summary data=sashelp.heart nway;
class chol_status;
var cholesterol;
output out=chol mean=chol_chol stderr=se_chol;
run;
data merged;
set weight chol;
wgt_upper = chol_wgt + se_wgt;
wgt_lower = chol_wgt - se_wgt;
chol_upper = chol_chol + se_chol;
chol_lower = chol_chol - se_chol;
run;
proc sgplot data=merged cycleattrs;
series x=weight_status y=chol_wgt / name="weight" lineattrs=graphdata1;
scatter x=weight_status y=chol_wgt / markerattrs=graphdata1(size=0)
yerrorupper=wgt_upper yerrorlower=wgt_lower;
series x=chol_status y=chol_chol / x2axis name="chol" lineattrs=graphdata2;
scatter x=chol_status y=chol_chol/ markerattrs=graphdata2(size=0) x2axis
yerrorupper=chol_upper yerrorlower=chol_lower;
keylegend "weight" "chol";
run;
Your best option would be to use PROC MEANS or PROC SUMMARY to calculate the numbers for each overlay, merge the results into one data set, and use SERIES and SCATTER plots (with error bars) to display the result. This following code is uses this technique with some sample data. Just substitute your data and variables. Let me know if you have any questions about the details.
Thanks!
Dan
proc summary data=sashelp.heart nway;
class weight_status;
var cholesterol;
output out=weight mean=chol_wgt stderr=se_wgt;
run;
proc summary data=sashelp.heart nway;
class chol_status;
var cholesterol;
output out=chol mean=chol_chol stderr=se_chol;
run;
data merged;
set weight chol;
wgt_upper = chol_wgt + se_wgt;
wgt_lower = chol_wgt - se_wgt;
chol_upper = chol_chol + se_chol;
chol_lower = chol_chol - se_chol;
run;
proc sgplot data=merged cycleattrs;
series x=weight_status y=chol_wgt / name="weight" lineattrs=graphdata1;
scatter x=weight_status y=chol_wgt / markerattrs=graphdata1(size=0)
yerrorupper=wgt_upper yerrorlower=wgt_lower;
series x=chol_status y=chol_chol / x2axis name="chol" lineattrs=graphdata2;
scatter x=chol_status y=chol_chol/ markerattrs=graphdata2(size=0) x2axis
yerrorupper=chol_upper yerrorlower=chol_lower;
keylegend "weight" "chol";
run;
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.