Published on :
Reporting INTERNAL_CREATION

Overlaying two 3D surfaces with PROC G3D and GREPLAY

This code is also available in: Deutsch Español Français
Awaiting validation
This script illustrates an advanced SAS©/Graph technique. It starts by creating a raw data set (`raw_data`) via a DATA step with embedded data (datalines). Then, it uses PROC G3GRID to smooth this data and create an interpolated grid. The smoothed data is then modified to simulate two distinct surfaces. The script configures the graphical environment for HTML output and generates two separate 3D plots with PROC G3D, saving them to a graphics catalog without displaying them immediately. The particularity is to play with the text color (white then black) to avoid a blur effect during superposition. Finally, PROC GREPLAY is used to combine these two graphs into a single image, which is then rendered in the output HTML file.
Data Analysis

Type : INTERNAL_CREATION


Initial data is created directly in the script via a DATA STEP block and the 'datalines' statement. All subsequent data is derived from this internal source.

1 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates the 'raw_data' table containing X, Y, Z coordinates. The data is embedded directly in the code using the 'datalines' statement.
Copied!
1DATA raw_data;
2INPUT X Y Z;
3DATALINES;
4-1.0 -1.0 15.5
5 -.5 -1.0 18.6
6 .0 -1.0 19.6
7 .5 -1.0 18.5
8 1.0 -1.0 15.8
9-1.0 -.5 10.9
10 -.5 -.5 14.8
11 .0 -.5 16.5
12 .5 -.5 14.9
13 1.0 -.5 10.9
14-1.0 .0 9.6
15 -.5 .0 14.0
16 .0 .0 15.7
17 .5 .0 13.9
18 1.0 .0 9.5
19-1.0 .5 11.2
20 -.5 .5 14.8
21 .0 .5 16.5
22 .5 .5 14.9
23 1.0 .5 11.1
24-1.0 1.0 15.8
25 -.5 1.0 18.6
26 .0 1.0 19.5
27 .5 1.0 18.5
28 1.0 1.0 15.8
29;
30RUN;
2 Code Block
PROC G3GRID Data
Explanation :
The G3GRID procedure is used for interpolation. It takes the raw data ('raw_data') and generates a new 'smoothed' table with a finer grid of points. The 'spline' option with 'smooth=.05' applies a cubic spline smoothing to obtain a regular surface.
Copied!
1PROC G3GRID DATA=raw_data out=smoothed;
2grid y*x=z / spline smooth=.05
3 axis1=-1 to 1 BY .1
4 axis2=-1 to 1 BY .1;
5RUN;
3 Code Block
DATA STEP Data
Explanation :
This DATA STEP modifies the 'smoothed' table to simulate two distinct surfaces. It creates two new variables 'z1' and 'z2' by offsetting the original 'z' value by -8 and +8 respectively.
Copied!
1DATA smoothed; SET smoothed;
2label z1='Z' z2='Z';
3z1=z-8;
4z2=z+8;
5RUN;
4 Code Block
ODS / GOPTIONS
Explanation :
This block configures the output environment. 'goptions' sets graph parameters (device, size). ODS (Output Delivery System) is configured to close standard listing output and redirect output to an HTML file. The 'nodisplay' option prevents immediate display of graphs.
Copied!
1%let name=example06;
2filename odsout '.';
3 
4goptions device=png;
5goptions xpixels=600 ypixels=600;
6goptions noborder;
7
8ODS LISTING CLOSE;
9ODS HTML path=odsout body="&name..htm" (title="SAS/Graph g3d plots") style=minimal;
10 
11goptions gunit=pct htitle=4.0 htext=2.5 ftitle="albany amt/bo" ftext="albany amt";
12goptions nodisplay;
5 Code Block
PROC G3D
Explanation :
The G3D procedure generates the first 3D graph based on the 'z1' variable. 'name="plot1"' saves the graph to the work catalog for later use. The text is set to white to prepare for superposition and avoid visual artifacts.
Copied!
1goptions ctext=white;
2 
3title ls=1.5 "Overlay Multiple G3D Surfaces, using Greplay";
4PROC G3D DATA=smoothed;
5 plot y*x=z1 /
6 grid zmin=0 zmax=30 xticknum=4 tilt=80
7 ctop=purple cbottom=cx00ff00 des='' name="plot1";
8RUN;
6 Code Block
PROC G3D
Explanation :
The G3D procedure generates the second 3D graph based on the 'z2' variable, using the same axis and angle parameters as the first to ensure perfect alignment. The graph is saved as 'plot2' and the text is set to black.
Copied!
1goptions ctext=black;
2 
3PROC G3D DATA=smoothed;
4 plot y*x=z2 /
5 grid zmin=0 zmax=30 xticknum=4 tilt=80
6 ctop=blue cbottom=red des='' name="plot2";
7RUN;
7 Code Block
PROC GREPLAY
Explanation :
The 'goptions display' option is re-enabled. The GREPLAY procedure overlays the two graphs ('plot1' and 'plot2') in a single area defined by the 'WHOLE' template. The 'treplay' statement executes this superposition, creating the final composite graph.
Copied!
1goptions display;
2 
3PROC GREPLAY tc=tempcat nofs igout=work.gseg;
4tdef WHOLE des="my template"
5 1/llx=0 lly=0
6 ulx=0 uly=100
7 urx=100 ury=100
8 lrx=100 lry=0
9 ;
10template = whole;
11treplay 1:plot1 1:plot2 des='' name="&name";
12RUN;
13QUIT;
8 Code Block
ODS
Explanation :
This block finalizes the process by closing the HTML output file and reactivating the default ODS LISTING destination.
Copied!
1ODS HTML CLOSE;
2ODS LISTING;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.