BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
fbl204653
Obsidian | Level 7

Hi All,

 

I used the following code to generate two vertical plots that have uniform x axises. In ordet to hide X axis in the first plot and X2axis in the second plot, I used "display=none", then the second plot has longer axis than that in the first plot. If I mask "display=none" in both sgplot procedures, the allignment problem is fixed. Is there other way that can fix this and let me remove those two axises. Thanks a lot for your answer in advance.

 

 

%let &mindate10=20089;
%let &mindate=20481;
%let &minday10=1;
%let &minday= 393;
data subject_lb;
input lbdy lbstresn lbdtc;
cards;
1 10 20453
;
run;

 

options nocenter nonumber nodate formchar= "___" missing=" "
ls=256 ps=49 orientation=landscape sysprintfont="Courier New" 7;

ODS LISTING CLOSE;
Ods Rtf File="C:\plottest1.rtf" style=adv startpage=no;

Ods Graphics On/Width=11in Height=3.5in maxlegendarea=80;
ods escapechar='^';


proc sgplot data=subject_lb noautolegend nocycleattrs;
title;
footnote1;
footnote2;
refline 1 / axis=x lineattrs=(thickness=1 color=black);
series x=lbdy y=lbstresn / lineattrs=(pattern=solid thickness=3) nomissinggroup name='bp' markerattrs=(symbol=circlefilled size=11);
scatter x=lbdy y=lbstresn / markerattrs=(symbol=circlefilled size=11) datalabel=lbstresn;
scatter x=lbdtc y=lbstresn / markerattrs=(size=0) x2axis;
yaxis display=(nolabel noticks novalues) grid;
y2axis display=none ;
xaxis grid label='Study Days' offsetmin=0.02 offsetmax=0.03
values=(&minday10 to &maxday by 10 ) FITPOLICY=rotate display=none;;
x2axis notimesplit display=(nolabel) tickvalueformat=yymmdd10.
offsetmin=0.02 offsetmax=0.03 values=(&mindate10 to &maxdate by 10) FITPOLICY=rotate;
run;
Ods Graphics Off;


Ods Graphics On/Width=11in Height=3.5in maxlegendarea=80;
ods escapechar='^';


proc sgplot data=subject_lb noautolegend nocycleattrs;
title;
footnote1;
footnote2;
refline 1 / axis=x lineattrs=(thickness=1 color=black);
series x=lbdy y=lbstresn / lineattrs=(pattern=solid thickness=3) nomissinggroup name='bp' markerattrs=(symbol=circlefilled size=11);
scatter x=lbdy y=lbstresn / markerattrs=(symbol=circlefilled size=11) datalabel=lbstresn;
scatter x=lbdtc y=lbstresn / markerattrs=(size=0) x2axis;
/*inset ("R:"="0-0")/ border position=topright;*/
yaxis display=(nolabel noticks novalues) grid;
y2axis display=none ;
xaxis grid label='Study Days' offsetmin=0.02 offsetmax=0.03
values=(&minday10 to &maxday by 10 ) ;
x2axis notimesplit tickvalueformat=yymmdd10.
offsetmin=0.02 offsetmax=0.03 values=(&mindate10 to &maxdate by 10) FITPOLICY=rotate display=none;
run;
Ods Graphics Off;
Ods Rtf Close;
Ods Listing;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Pamela_JSRCC
Quartz | Level 8

I used proc template to display your graphs in a lattice with 1 column.  I ran your proc sgplot statements to create code for the layouts for proc template.  Then combined them in a proc template statement below (just copied the layout statement from each of the template files plot_top.tmp and plot_bttm.tmp into the proc template statement than creates the sgplot_top_bttm template.  Then proc sgRender is called to render the graphs using the data provided.

 

proc sgplot data=graph_data noautolegend nocycleattrs TMPLOUT= "&xpath.\plot_top.tmp"

;

refline 1 / axis=x lineattrs=(thickness=1 color=black);

series x=lbdy y=lbstresn / lineattrs =(pattern=solid thickness=3) nomissinggroup name='bp'

markerattrs=(symbol=circlefilled size=11);

scatter x=lbdy y=lbstresn / markerattrs=(symbol=circlefilled size=11) datalabel=lbstresn;

scatter x=lbdtc y=lbstresn / markerattrs=(size=0) x2axis;

yaxis display=(nolabel noticks novalues) grid;

y2axis display=none ;

x2Axis notimesplit tickvalueformat=yymmdd10. label="Study Date"

offsetmin=&offsetMin offsetmax=&offsetMax

values =(&mindate10 to &maxdate by 10) FITPOLICY=rotate;

xAxis grid label='Study Days'

offsetmin=&offsetMin offsetmax=&offsetMax

values =(&minday10 to &maxday by 10 ) /*FITPOLICY=rotate*/ display=none;

run;

proc sgplot data=graph_data noautolegend nocycleattrs TMPLOUT= "&xpath.\plot_bttm.tmp"

;

refline 1 / axis=x lineattrs=(thickness=1 color=black);

series x=lbdy y=lbstresn / lineattrs =(pattern=solid thickness=3) nomissinggroup name='bp'

markerattrs=(symbol=circlefilled size=11);

scatter x=lbdy y=lbstresn / markerattrs=(symbol=circlefilled size=11) datalabel=lbstresn;

scatter x=lbdtc y=lbstresn / markerattrs=(size=0) x2axis;

/*inset ("R:"="0-0")/ border position=topright;*/

yaxis display=(NoLabel NoTicks NoValues) grid;

y2axis display=none ;

x2Axis notimesplit tickvalueformat=yymmdd10.

offsetmin=&offsetMin offsetmax=&offsetMax

values =(&mindate10 to &maxdate by 10) FITPOLICY=rotate display=none;

xAxis grid label='Relative Study Days'

offsetmin=&offsetMin offsetmax=&offsetMax

values =(&minday10 to &maxday by 10);

run;

Ods Graphics Off;

*Ods Rtf Close;

Ods Listing;

/* Get Layout statements from the template files generated above */

proc template;

define statgraph sgplot_top_bttm;

begingraph / collation=binary;

layout lattice / columns=1 rowDataRange=union;

/* layout statement copied from plot_top.tmp */

layout overlay / xaxisopts=( display=none Label="Study Days" labelFitPolicy=Split offsetmin=0.02 offsetmax=0.03 type=linear linearopts=( tickvaluelist=( 1 11 21 31 41 51 61 71 81 91 101 111 121 131 141 151 161 171 181 191 201 211 221 231 241 251 261 271 281 291 301 311 321 331 341 351 361 371 381 391 ) viewmin=1 viewmax=391 ) griddisplay=on ) yaxisopts=( display=( line ) type=linear griddisplay=on ) x2axisopts=( Label="Study Date" labelFitPolicy=Split offsetmin=0.02 offsetmax=0.03 type=linear linearopts=( tickvaluelist=( 20089 20099 20109 20119 20129 20139 20149 20159 20169 20179 20189 20199 20209 20219 20229 20239 20249 20259 20269 20279 20289 20299 20309 20319 20329 20339 20349 20359 20369 20379 20389 20399 20409 20419 20429 20439 20449 20459 20469 20479 ) viewmin=20089 viewmax=20479 TickValueFitPolicy=Rotate TickValueFormat=YYMMDD10. ) ) y2axisopts=( display=none type=auto );

ReferenceLine x=1 / clip=true Lineattrs=( Color=CX000000 Thickness=1);

SeriesPlot X='lbdy'n Y='lbstresn'n / includemissinggroup=false Markerattrs=( Symbol=CIRCLEFILLED Size=11) Lineattrs=( Pattern=1 Thickness=3) LegendLabel="lbstresn" NAME="bp";

ScatterPlot X='lbdy'n Y='lbstresn'n / subpixel=off primary=true Markerattrs=( Symbol=CIRCLEFILLED Size=11) LegendLabel="lbstresn" DataLabel='lbstresn'n NAME="SCATTER";

ScatterPlot X='lbdtc'n Y='lbstresn'n / subpixel=off xaxis=x2 Markerattrs=( Size=0) LegendLabel="lbstresn" NAME="SCATTER1";

endlayout;

/* layout statement copied from plot_bttm.tmp */

layout overlay / xaxisopts=( Label="Relative Study Days" labelFitPolicy=Split offsetmin=0.02 offsetmax=0.03 type=linear linearopts=( tickvaluelist=( 1 11 21 31 41 51 61 71 81 91 101 111 121 131 141 151 161 171 181 191 201 211 221 231 241 251 261 271 281 291 301 311 321 331 341 351 361 371 381 391 ) viewmin=1 viewmax=391 ) griddisplay=on ) yaxisopts=( display=( line ) type=linear griddisplay=on ) x2axisopts=( display=none labelFitPolicy=Split offsetmin=0.02 offsetmax=0.03 type=linear linearopts=( tickvaluelist=( 20089 20099 20109 20119 20129 20139 20149 20159 20169 20179 20189 20199 20209 20219 20229 20239 20249 20259 20269 20279 20289 20299 20309 20319 20329 20339 20349 20359 20369 20379 20389 20399 20409 20419 20429 20439 20449 20459 20469 20479 ) viewmin=20089 viewmax=20479 TickValueFitPolicy=Rotate TickValueFormat=YYMMDD10. ) ) y2axisopts=( display=none type=auto );

ReferenceLine x=1 / clip=true Lineattrs=( Color=CX000000 Thickness=1);

SeriesPlot X='lbdy'n Y='lbstresn'n / includemissinggroup=false Markerattrs=( Symbol=CIRCLEFILLED Size=11) Lineattrs=( Pattern=1 Thickness=3) LegendLabel="lbstresn" NAME="bp";

ScatterPlot X='lbdy'n Y='lbstresn'n / subpixel=off primary=true Markerattrs=( Symbol=CIRCLEFILLED Size=11) LegendLabel="lbstresn" DataLabel='lbstresn'n NAME="SCATTER";

ScatterPlot X='lbdtc'n Y='lbstresn'n / subpixel=off xaxis=x2 Markerattrs=( Size=0) LegendLabel="lbstresn" NAME="SCATTER1";

endlayout;

/* end copied lines */

endlayout; /* lattice */

endgraph;

end;

run;

 

Ods Graphics On / Width=11in Height=7in maxlegendarea=80;

Ods Listing close;

 

proc sgrender data = graph_data

template = sgplot_top_bttm;

run;

 

Ods Graphics Off;

Ods Listing;

 

View solution in original post

14 REPLIES 14
Jay54
Meteorite | Level 14

Please attach some sample data so we can run the code to see what is going on.

fbl204653
Obsidian | Level 7

Hi Sanjay,

 

Thanks for your reply. Sample data is as follows, not much, but it can show the probleme of axis allignment.

 

%let mindate10=20089;
%let maxdate=20481;
%let minday10=1;
%let maxday= 393;
data subject_lb;
input lbdy lbstresn lbdtc;
cards;
1 10 20453
;
run;

 

Jay54
Meteorite | Level 14

In your code, the %LET should likely not have "&" before the variable names.  Also &maxdate does not resolve.

Please ensure your code runs in a new SAS session.  

fbl204653
Obsidian | Level 7

Sorry Sanjay,

 

I modifed the data and it looks like 

 

%let mindate10=20089;
%let maxdate=20481;
%let minday10=1;
%let maxday= 393;
data subject_lb;
input lbdy lbstresn lbdtc;
cards;
1 10 20453
;
run;

Pamela_JSRCC
Quartz | Level 8

I was able to run your code and see the problem.  To have SAS line up the two graphs, I think you will need to use something that associates the graphs together, like proc sgPanel or proc sgScatter.  I don't know if they will give you the flexibility that comes from creating two different x-axes.  Hopefully someone with more experience with graphics can suggest a more complete solution.

fbl204653
Obsidian | Level 7

Thanks for your answer, Pamela, I used sgPanel, but this procedure seems sas doesn't support x2axis which I want to keep.

 

 

Jay54
Meteorite | Level 14

I ran your new code at SAS 9.40M3, and I seem to get the graphs with the same "length" of axis.  What release of SAS are you using.  Please attach your result, so we can compare.  RTF output is attached (as txt file).  Download and change the extension to "rtf" to view.

fbl204653
Obsidian | Level 7

Hi SanJay,

 

I've got the same result as yours. But 391 on the xaxis is supposed to share the same grid x line as 2016-01-26 on x2axis. The right margin of top graph is wider than that in the below graph. Once you remove "display=none", you will find that they share the same grid line.

 

Thank you very much!

Pamela_JSRCC
Quartz | Level 8

I used proc template to display your graphs in a lattice with 1 column.  I ran your proc sgplot statements to create code for the layouts for proc template.  Then combined them in a proc template statement below (just copied the layout statement from each of the template files plot_top.tmp and plot_bttm.tmp into the proc template statement than creates the sgplot_top_bttm template.  Then proc sgRender is called to render the graphs using the data provided.

 

proc sgplot data=graph_data noautolegend nocycleattrs TMPLOUT= "&xpath.\plot_top.tmp"

;

refline 1 / axis=x lineattrs=(thickness=1 color=black);

series x=lbdy y=lbstresn / lineattrs =(pattern=solid thickness=3) nomissinggroup name='bp'

markerattrs=(symbol=circlefilled size=11);

scatter x=lbdy y=lbstresn / markerattrs=(symbol=circlefilled size=11) datalabel=lbstresn;

scatter x=lbdtc y=lbstresn / markerattrs=(size=0) x2axis;

yaxis display=(nolabel noticks novalues) grid;

y2axis display=none ;

x2Axis notimesplit tickvalueformat=yymmdd10. label="Study Date"

offsetmin=&offsetMin offsetmax=&offsetMax

values =(&mindate10 to &maxdate by 10) FITPOLICY=rotate;

xAxis grid label='Study Days'

offsetmin=&offsetMin offsetmax=&offsetMax

values =(&minday10 to &maxday by 10 ) /*FITPOLICY=rotate*/ display=none;

run;

proc sgplot data=graph_data noautolegend nocycleattrs TMPLOUT= "&xpath.\plot_bttm.tmp"

;

refline 1 / axis=x lineattrs=(thickness=1 color=black);

series x=lbdy y=lbstresn / lineattrs =(pattern=solid thickness=3) nomissinggroup name='bp'

markerattrs=(symbol=circlefilled size=11);

scatter x=lbdy y=lbstresn / markerattrs=(symbol=circlefilled size=11) datalabel=lbstresn;

scatter x=lbdtc y=lbstresn / markerattrs=(size=0) x2axis;

/*inset ("R:"="0-0")/ border position=topright;*/

yaxis display=(NoLabel NoTicks NoValues) grid;

y2axis display=none ;

x2Axis notimesplit tickvalueformat=yymmdd10.

offsetmin=&offsetMin offsetmax=&offsetMax

values =(&mindate10 to &maxdate by 10) FITPOLICY=rotate display=none;

xAxis grid label='Relative Study Days'

offsetmin=&offsetMin offsetmax=&offsetMax

values =(&minday10 to &maxday by 10);

run;

Ods Graphics Off;

*Ods Rtf Close;

Ods Listing;

/* Get Layout statements from the template files generated above */

proc template;

define statgraph sgplot_top_bttm;

begingraph / collation=binary;

layout lattice / columns=1 rowDataRange=union;

/* layout statement copied from plot_top.tmp */

layout overlay / xaxisopts=( display=none Label="Study Days" labelFitPolicy=Split offsetmin=0.02 offsetmax=0.03 type=linear linearopts=( tickvaluelist=( 1 11 21 31 41 51 61 71 81 91 101 111 121 131 141 151 161 171 181 191 201 211 221 231 241 251 261 271 281 291 301 311 321 331 341 351 361 371 381 391 ) viewmin=1 viewmax=391 ) griddisplay=on ) yaxisopts=( display=( line ) type=linear griddisplay=on ) x2axisopts=( Label="Study Date" labelFitPolicy=Split offsetmin=0.02 offsetmax=0.03 type=linear linearopts=( tickvaluelist=( 20089 20099 20109 20119 20129 20139 20149 20159 20169 20179 20189 20199 20209 20219 20229 20239 20249 20259 20269 20279 20289 20299 20309 20319 20329 20339 20349 20359 20369 20379 20389 20399 20409 20419 20429 20439 20449 20459 20469 20479 ) viewmin=20089 viewmax=20479 TickValueFitPolicy=Rotate TickValueFormat=YYMMDD10. ) ) y2axisopts=( display=none type=auto );

ReferenceLine x=1 / clip=true Lineattrs=( Color=CX000000 Thickness=1);

SeriesPlot X='lbdy'n Y='lbstresn'n / includemissinggroup=false Markerattrs=( Symbol=CIRCLEFILLED Size=11) Lineattrs=( Pattern=1 Thickness=3) LegendLabel="lbstresn" NAME="bp";

ScatterPlot X='lbdy'n Y='lbstresn'n / subpixel=off primary=true Markerattrs=( Symbol=CIRCLEFILLED Size=11) LegendLabel="lbstresn" DataLabel='lbstresn'n NAME="SCATTER";

ScatterPlot X='lbdtc'n Y='lbstresn'n / subpixel=off xaxis=x2 Markerattrs=( Size=0) LegendLabel="lbstresn" NAME="SCATTER1";

endlayout;

/* layout statement copied from plot_bttm.tmp */

layout overlay / xaxisopts=( Label="Relative Study Days" labelFitPolicy=Split offsetmin=0.02 offsetmax=0.03 type=linear linearopts=( tickvaluelist=( 1 11 21 31 41 51 61 71 81 91 101 111 121 131 141 151 161 171 181 191 201 211 221 231 241 251 261 271 281 291 301 311 321 331 341 351 361 371 381 391 ) viewmin=1 viewmax=391 ) griddisplay=on ) yaxisopts=( display=( line ) type=linear griddisplay=on ) x2axisopts=( display=none labelFitPolicy=Split offsetmin=0.02 offsetmax=0.03 type=linear linearopts=( tickvaluelist=( 20089 20099 20109 20119 20129 20139 20149 20159 20169 20179 20189 20199 20209 20219 20229 20239 20249 20259 20269 20279 20289 20299 20309 20319 20329 20339 20349 20359 20369 20379 20389 20399 20409 20419 20429 20439 20449 20459 20469 20479 ) viewmin=20089 viewmax=20479 TickValueFitPolicy=Rotate TickValueFormat=YYMMDD10. ) ) y2axisopts=( display=none type=auto );

ReferenceLine x=1 / clip=true Lineattrs=( Color=CX000000 Thickness=1);

SeriesPlot X='lbdy'n Y='lbstresn'n / includemissinggroup=false Markerattrs=( Symbol=CIRCLEFILLED Size=11) Lineattrs=( Pattern=1 Thickness=3) LegendLabel="lbstresn" NAME="bp";

ScatterPlot X='lbdy'n Y='lbstresn'n / subpixel=off primary=true Markerattrs=( Symbol=CIRCLEFILLED Size=11) LegendLabel="lbstresn" DataLabel='lbstresn'n NAME="SCATTER";

ScatterPlot X='lbdtc'n Y='lbstresn'n / subpixel=off xaxis=x2 Markerattrs=( Size=0) LegendLabel="lbstresn" NAME="SCATTER1";

endlayout;

/* end copied lines */

endlayout; /* lattice */

endgraph;

end;

run;

 

Ods Graphics On / Width=11in Height=7in maxlegendarea=80;

Ods Listing close;

 

proc sgrender data = graph_data

template = sgplot_top_bttm;

run;

 

Ods Graphics Off;

Ods Listing;

 

fbl204653
Obsidian | Level 7

I am using SAS 9.3

Pamela_JSRCC
Quartz | Level 8

That should not be a problem.  Proc template and proc render are both available in SAS 9.3.  You may have to recreate the template files to get the template code generated from your SAS version.  That's what I did when running it in SAS 9.3.  I've attached a PNG file showing what the graph looks like.


plot_top_bttm.png
fbl204653
Obsidian | Level 7

Wow, it looks terrific. Thanks you so much. I am trying to understand your code 

Pamela_JSRCC
Quartz | Level 8

Most of the code is yours; thats why I made it so small except for where I didn't.  The main idea is that you can create your graphs separately with proc sgPlot and fiddle around 'til you get what you want.  Then use the TMPLOUT= option to put the template code into text files.  You can then copy the layout statements from the template files into the proc template code that controls the layout.  I've attached a PNG of the template files in Notepad showing the part I copied.  A picture is worth a 1,000 words.

 

This is my first time using proc template for graphics, so there may be a better way, but this is simpler than I thought it would be.

 


plot_top_bttm_cut_paste.png
fbl204653
Obsidian | Level 7

its smart way, thank you

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 14 replies
  • 4138 views
  • 1 like
  • 3 in conversation