Graphics Programming

Data visualization using SAS programming, including ODS Graphics and SAS/GRAPH. Charts, plots, maps, and more!
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
zimcom
Pyrite | Level 9

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

1 ACCEPTED SOLUTION

Accepted Solutions
GraphGuy
Meteorite | Level 14

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;

 

 

overlay_plot.png

View solution in original post

9 REPLIES 9
Reeza
Super User
Switch to SGPLOT.

proc sgplot data=want;
series x=day y=test_a;
series x=day y=test_b;
run;
DanH_sas
SAS Super FREQ

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.

zimcom
Pyrite | Level 9

Thank you both, the codes works.

 

How can I define the line label, markers size and color and line color and style etc.?

 

 

DanH_sas
SAS Super FREQ

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;
zimcom
Pyrite | Level 9
where I can see all the other options?
zimcom
Pyrite | Level 9

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!!

DanH_sas
SAS Super FREQ

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...

 

GraphGuy
Meteorite | Level 14

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;

 

 

overlay_plot.png

zimcom
Pyrite | Level 9

This is indeed what I am trying to plot, this is great!

 

Thank you so much!

zimcom

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 7264 views
  • 2 likes
  • 4 in conversation