Dear community,
I have a data set partially like below
pid | visit | test_a | test_b |
Patient_1 | 24hr | 5 | 20 |
Patient_1 | Day5 | 4 | 68 |
Patient_1 | day30 | 0 | 46 |
Patient_1 | day90 | 3 | 24 |
Patient_2 | 24hr | 4 | 5 |
Patient_2 | Day5 | 1 | 65 |
Patient_2 | day30 | 2 | 72 |
Patient_2 | day90 | 0 | 26 |
Which has well over a few hundreds of patients’ test_a and test_b results (if it is possible, I also want to plot test_c on the graph as well). I can only plot test _a with the following code and I don’t know if it is possible to add test_b line on the same plot using the secondary axis, any help would be greatly appreciatd.
/*
data want; set test_data;
if _name_ = '24hr' then day = 1;
else if _name_ = 'Day5' then day = 2;
else if _name_ = 'Day30' then day = 3;
else if _name_ = 'Day90' then day = 4;
run;
proc format;
value dayfor
1 = '24hr'
2 = 'Day5'
3 = 'Day30'
4 = 'Day90';
run;
data want; set want;
format day dayfor.;
run;
proc sort data = want;
by pid day; run;
axis2 order = (0 to 7);
symbol1 interpol=join width=2 color=vligb value=dot height=6;
symbol2 interpol=join width=2 color=salmon value=dot height=6;
proc gplot data=want;
plot test_a * day;
run;
*/
Thanks
zimcom
Here's some Proc Gplot code, which I believe covers all the things you mentioned, as well as some other things you might want to add:
data test_data;
length pid $20 visit $10;
input pid visit test_a test_b test_c;
datalines;
Patient_1 24hr 5 20 18
Patient_1 Day5 4 68 45
Patient_1 Day30 0 46 32
Patient_1 Day90 3 24 15
Patient_2 24hr 4 5 9
Patient_2 Day5 1 65 52
Patient_2 Day30 2 72 68
Patient_2 Day90 0 26 22
;
run;
proc format;
value dayfor
1 = '24hr'
2 = 'Day5'
3 = 'Day30'
4 = 'Day90';
run;
data want; set test_data;
format day dayfor.;
if visit = '24hr' then day = 1;
else if visit = 'Day5' then day = 2;
else if visit = 'Day30' then day = 3;
else if visit = 'Day90' then day = 4;
run;
proc sort data=want out=want;
by pid day;
run;
/* insert a missing value between each patient for the 'skipmiss' */
data want; set want;
by pid;
output;
if last.pid then do;
day=.;
output;
end;
run;
goptions gunit=pct ftitle='albany amt/bold' ftext='albany amt' htitle=3.5 htext=3.0;
goptions ctext=gray33;
symbol1 interpol=join width=2 color=vligb value=dot height=3;
symbol2 interpol=join width=2 color=salmon value=circle height=3;
symbol3 interpol=join width=2 color=lig value=trianglefilled height=3;
legend1 position=(top left inside) mode=share across=1 label=none repeat=2;
legend2 position=(top right inside) mode=share across=1 label=none repeat=2;
axis1 label=none minor=none offset=(5,5);
axis2 label=(f='albany amt/bold' c=vligb 'Test A') order=(0 to 7 by 1) minor=none;
axis3 label=(f='albany amt/bold' j=l c=salmon 'Test B' j=l c=lig 'Test C')
order=(0 to 100 by 20) minor=none;
title1 ls=1.5 "Test Results";
proc gplot data=want;
plot test_a*day=1 / haxis=axis1 vaxis=axis2 skipmiss legend=legend1;
plot2 test_b*day=2 test_c*day=3 / haxis=axis1 vaxis=axis3 overlay skipmiss legend=legend2;
run;
You have two options:
1) Use the PLOT2 statement in GPLOT:
proc gplot data=want;
plot test_a * day;
plot2 test_b * day
run;
2) Use SGPLOT (as @Reeza said)
proc sgplot data=want;
series x=day y=test_a;
series x=day y=test_b / y2axis;
run;
Option #2 will require you to incorporate your AXIS and SYMBOL statement options into SGPLOT syntax, as these global statements are not used by that procedure.
Thank you both, the codes works.
How can I define the line label, markers size and color and line color and style etc.?
Based on the SYMBOL options from your previous program, you should try something like this:
proc sgplot data=want;
series x=day y=test_a / markerattrs=(color=vligb symbol=circlefilled size=9)
lineattrs=(color=vligb thickness=2);
series x=day y=test_b / y2axis markerattrs=(color=salmon symbol=circlefilled size=9)
lineattrs=(color=salmon thickness=2);
run;
also it would be more consistent to fix the scale of both axises, like set up the maximum of 7 for the primary axis with major tick as of 1 and the maximum of 100 for the secondary axis and major tick being 10.
THNAK YOU!!
For your scaling, you'll probably want to use the VALUES option on the YAXIS and Y2AXIS statements. As for all of the other options, there are a lot of them :-). Here is the link to the online doc: http://support.sas.com/documentation/cdl//en/grstatproc/69716/HTML/default/viewer.htm#n0yjdd910dh59z...
Here's some Proc Gplot code, which I believe covers all the things you mentioned, as well as some other things you might want to add:
data test_data;
length pid $20 visit $10;
input pid visit test_a test_b test_c;
datalines;
Patient_1 24hr 5 20 18
Patient_1 Day5 4 68 45
Patient_1 Day30 0 46 32
Patient_1 Day90 3 24 15
Patient_2 24hr 4 5 9
Patient_2 Day5 1 65 52
Patient_2 Day30 2 72 68
Patient_2 Day90 0 26 22
;
run;
proc format;
value dayfor
1 = '24hr'
2 = 'Day5'
3 = 'Day30'
4 = 'Day90';
run;
data want; set test_data;
format day dayfor.;
if visit = '24hr' then day = 1;
else if visit = 'Day5' then day = 2;
else if visit = 'Day30' then day = 3;
else if visit = 'Day90' then day = 4;
run;
proc sort data=want out=want;
by pid day;
run;
/* insert a missing value between each patient for the 'skipmiss' */
data want; set want;
by pid;
output;
if last.pid then do;
day=.;
output;
end;
run;
goptions gunit=pct ftitle='albany amt/bold' ftext='albany amt' htitle=3.5 htext=3.0;
goptions ctext=gray33;
symbol1 interpol=join width=2 color=vligb value=dot height=3;
symbol2 interpol=join width=2 color=salmon value=circle height=3;
symbol3 interpol=join width=2 color=lig value=trianglefilled height=3;
legend1 position=(top left inside) mode=share across=1 label=none repeat=2;
legend2 position=(top right inside) mode=share across=1 label=none repeat=2;
axis1 label=none minor=none offset=(5,5);
axis2 label=(f='albany amt/bold' c=vligb 'Test A') order=(0 to 7 by 1) minor=none;
axis3 label=(f='albany amt/bold' j=l c=salmon 'Test B' j=l c=lig 'Test C')
order=(0 to 100 by 20) minor=none;
title1 ls=1.5 "Test Results";
proc gplot data=want;
plot test_a*day=1 / haxis=axis1 vaxis=axis2 skipmiss legend=legend1;
plot2 test_b*day=2 test_c*day=3 / haxis=axis1 vaxis=axis3 overlay skipmiss legend=legend2;
run;
This is indeed what I am trying to plot, this is great!
Thank you so much!
zimcom
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.