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

Hi community,

I've been struggling to teach myself Proc Template and have come up with the code below. It plots four scatter plots, but I only want a discrete legend in two of those plots, so I've created a _key dynamic variable to try to make this happen. The problem is that it is only creating the discrete legend in one of my graphics. 

 

I'd really appreciate any help or guidance you all have to share!

Thanks

Ben

PROC TEMPLATE;
	DEFINE STATGRAPH j;
		DYNAMIC _title _type _yvar _key;
		BEGINGRAPH /designheight=6in
					designwidth=8in;
			ENTRYTITLE _title;
			ENTRYFOOTNOTE HALIGN=left "";
		  
			DISCRETEATTRMAP NAME = 'SiteClassKey' / IGNORECASE = true DISCRETELEGENDENTRYPOLICY = attrmap;
				VALUE 'BC'		/ markerattrs = (color = black size = 8 symbol = circlefilled);
				VALUE 'NB'		/ markerattrs = (color = black size = 8 symbol = trianglefilled);
				VALUE 'SP'		/ markerattrs = (color = black size = 8 symbol = diamondfilled);
				VALUE 'Paro'	/ markerattrs = (color = black size = 8 symbol = circlefilled);
				VALUE 'Pers'	/ markerattrs = (color = black size = 8 symbol = diamondfilled);
			ENDDISCRETEATTRMAP;
		 
			DISCRETEATTRVAR attrvar = siteClassMarks var = siteClass attrmap = 'SiteClassKey';
			LAYOUT OVERLAY / XAXISOPTS = ( display=(label ticks tickvalues)
										   label = 'Active or Passive Site'
										 )
							 YAXISOPTS = ( display=(label ticks tickvalues)
										   label = 'Percent Recurrence'
										   LINEAROPTS=(viewmin = 0 viewmax = 2.5 tickvaluelist=(0 0.5 1.5 2.5))
										 );
				SCATTERPLOT X = type Y = _yvar /GROUP = siteClassMarks
												includemissinggroup = False
												JITTER = AUTO
												MARKERATTRS = (	SIZE = 8 )
												NAME = "SP"
												;
				REFERENCELINE Y=0.898 / CURVELABEL = "Y = 2.5%";
				DRAWRECTANGLE X = 89 Y = 125 WIDTH = 4 HEIGHT = 1 /	DISPLAY = ALL 
																	FILLATTRS = (COLOR = WHITE)
																	OUTLINEATTRS=(COLOR = WHITE)
																	TRANSPARENCY=0
																	DRAWSPACE = GRAPHPIXEL
																	ANCHOR = LEFT
																	TRANSPARENCY=0
																	;
				DRAWLINE X1=86 Y1=110 X2=103 Y2=118 / 	X1SPACE=LAYOUTPIXEL
														X2SPACE=LAYOUTPIXEL 
														Y1SPACE=LAYOUTPIXEL
														Y2SPACE=LAYOUTPIXEL
														LINEATTRS = (COLOR = GREY
																	 THICKNESS = 1
														);
				DRAWLINE X1=86 Y1=101.75 X2=103 Y2=109.75 / X1SPACE=LAYOUTPIXEL
															X2SPACE=LAYOUTPIXEL 
															Y1SPACE=LAYOUTPIXEL
															Y2SPACE=LAYOUTPIXEL
															LINEATTRS = (COLOR = GREY 
																	 	THICKNESS = 1
																	 );
					/* LINEATTRS = (COLOR = GRAY) / 
					DRAWSPACE = GRAPHPIXEL ANCHOR=LEFT BORDER=FALSE TRANSPARENCY=0*/
			  IF (_key = "Y")
				DISCRETELEGEND "SP" / TITLE = "TITLEEE";
			  ENDIF;
			ENDLAYOUT;
		ENDGRAPH;
	END;

	DEFINE STYLE jphrecstyle;
		PARENT = Styles.Statistical;
		REPLACE fonts /
			'TitleFont'=("Arial,Helvetica,Helv",24pt,Bold Italic)
			'TitleFont2'=("Arial,Helvetica,Helv",24pt,Bold Italic)
			'StrongFont'=("Arial, Helvetica, Helv",12pt,Bold)
			'EmphasisFont'=("Arial,Helvetica,Helv",10pt,Italic)
			'headingFont'=("Arial, Helvetica, Helv",24pt,Bold)
			'docFont'=("Arial, Helvetica, Helv",11pt)
			'footFont'=("Arial, Helvetica, Helv",8pt)
			'headingEmphasisFont' =("Arial, Helvetica, Helv",24pt,Bold)
			'FixedStrongFont' =("Arial, Helvetica, Helv",24pt,Bold)
			'BatchFixedFont' =("Arial, Helvetica, Helv",24pt,Bold)
			'FixedFont' =("Arial, Helvetica, Helv",24pt,Bold)
			'FixedEmphasisFont' =("Arial, Helvetica, Helv",24pt,Bold)
		; 

		REPLACE GraphFonts /
			'GraphAnnoFont'		=("Arial, Helvetica, Helv",10pt,Bold)	/*? Annotation*/
			'GraphDataFont'		=("Arial, Helvetica, Helv",10pt,Bold)	/*???*/
			'GraphFootnoteFont'	=("Arial, Helvetica, Helv",24pt,Bold)
		    'GraphLabelFont'	=("Arial, Helvetica, Helv",14pt) 		/*Y Axis Label*/
			'GraphLabel2Font'	=("Arial, Helvetica, Helv",14pt)		/*X Axis Label*/
			'GraphTitleFont'	=("Arial, Helvetica, Helv",14pt,Bold)	/*Title*/
			'GraphTitle1Font'	=("Arial, Helvetica, Helv",24pt,Bold)
		    'GraphUnicodeFont'	=("Arial, Helvetica, Helv",24pt,Bold)
		    'GraphValueFont'	=("Arial, Helvetica, Helv",14pt)		/*Axes Font*/
		    'NodeDetailFont'	=("Arial, Helvetica, Helv",24pt,Bold)
			'NodeInputLabelFont'=("Arial, Helvetica, Helv",24pt,Bold)
			'NodeLabelFont'		=("Arial, Helvetica, Helv",24pt,Bold)
			'NodeTitleFont'		=("Arial, Helvetica, Helv",24pt,Bold)
		;
	END;
RUN;

PROC SGRENDER DATA = simall TEMPLATE = j;
	DYNAMIC _title = 'A'
			_yvar = 'rr__rec_log'
			 _key = "N";
	FORMAT rr__rec_log loglabel.;
RUN;

PROC SGRENDER DATA = HuAll TEMPLATE = j;
	DYNAMIC	_title = 'B'
			_yvar = 'H_log'
			_key = "N";
	FORMAT H_log loglabel.;
RUN;

PROC SGRENDER DATA = simall TEMPLATE = j;
	DYNAMIC _key = "Y"
			_title = 'C'
			_type = "Hu"
			_yvar = 'z2rec_log';
	FORMAT z2rec_log loglabel.;
RUN;

PROC SGRENDER DATA = HuAll TEMPLATE = j;
	DYNAMIC _key = "Y"
			_title = 'D'
			_type = "Hu"
			_yvar = 'AH_log';
	FORMAT AH_log loglabel.;
RUN;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

The generic approach is to add a name to the plots in the options such as: Name="plot1" for each of your plots.

Then add a Discretelegend statement(s) that reference only the plots that you want the legends to display.

You can reference multiple plots in a single discretelegend statement.

 

I tend to put this just before the endlayout but they do have to appear withing the layout definition.

 

Discretelegend "plot1" "plot2" / <options>;

endlayout;

View solution in original post

5 REPLIES 5
ballardw
Super User

The generic approach is to add a name to the plots in the options such as: Name="plot1" for each of your plots.

Then add a Discretelegend statement(s) that reference only the plots that you want the legends to display.

You can reference multiple plots in a single discretelegend statement.

 

I tend to put this just before the endlayout but they do have to appear withing the layout definition.

 

Discretelegend "plot1" "plot2" / <options>;

endlayout;

benbuck
Obsidian | Level 7

Thanks! I really appreciate your effort and I think I'm almost there. I've attempted to make that change, but I keep getting an error that sas expected a quoted string for the SCATTERPLOT / NAME = option. My code is below:

 

PROC TEMPLATE;
	DEFINE STATGRAPH j;
		DYNAMIC _title _type _yvar _key;
		BEGINGRAPH /designheight=6in
					designwidth=8in;
			ENTRYTITLE _title;
			ENTRYFOOTNOTE HALIGN=left "";
		  
			DISCRETEATTRMAP NAME = 'SiteClassKey' / IGNORECASE = true DISCRETELEGENDENTRYPOLICY = attrmap;
				VALUE 'BC'		/ markerattrs = (color = black size = 8 symbol = circlefilled);
				VALUE 'NB'		/ markerattrs = (color = black size = 8 symbol = trianglefilled);
				VALUE 'SP'		/ markerattrs = (color = black size = 8 symbol = diamondfilled);
				VALUE 'Paro'	/ markerattrs = (color = black size = 8 symbol = circlefilled);
				VALUE 'Pers'	/ markerattrs = (color = black size = 8 symbol = diamondfilled);
			ENDDISCRETEATTRMAP;
		 
			DISCRETEATTRVAR attrvar = siteClassMarks var = siteClass attrmap = 'SiteClassKey';
			LAYOUT OVERLAY / XAXISOPTS = ( display=(label ticks tickvalues)
										   label = 'Active or Passive Site'
										 )
							 YAXISOPTS = ( display=(label ticks tickvalues)
										   label = 'Percent Recurrence'
										   LINEAROPTS=(viewmin = 0 viewmax = 2.5 tickvaluelist=(0 0.5 1.5 2.5))
										 );
				SCATTERPLOT X = type Y = _yvar /GROUP = siteClassMarks
												includemissinggroup = False
												JITTER = AUTO
												MARKERATTRS = (	SIZE = 8 )
												NAME = _plotname
												;
				REFERENCELINE Y=0.898 / CURVELABEL = "Y = 2.5%";
				DRAWRECTANGLE X = 89 Y = 125 WIDTH = 4 HEIGHT = 1 /	DISPLAY = ALL 
																	FILLATTRS = (COLOR = WHITE)
																	OUTLINEATTRS=(COLOR = WHITE)
																	TRANSPARENCY=0
																	DRAWSPACE = GRAPHPIXEL
																	ANCHOR = LEFT
																	TRANSPARENCY=0
																	;
				DRAWLINE X1=86 Y1=110 X2=103 Y2=118 / 	X1SPACE=LAYOUTPIXEL
														X2SPACE=LAYOUTPIXEL 
														Y1SPACE=LAYOUTPIXEL
														Y2SPACE=LAYOUTPIXEL
														LINEATTRS = (COLOR = GREY
																	 THICKNESS = 1
														);
				DRAWLINE X1=86 Y1=101.75 X2=103 Y2=109.75 / X1SPACE=LAYOUTPIXEL
															X2SPACE=LAYOUTPIXEL 
															Y1SPACE=LAYOUTPIXEL
															Y2SPACE=LAYOUTPIXEL
															LINEATTRS = (COLOR = GREY 
																	 	THICKNESS = 1
																	 );
					/* LINEATTRS = (COLOR = GRAY) / 
					DRAWSPACE = GRAPHPIXEL ANCHOR=LEFT BORDER=FALSE TRANSPARENCY=0*/
			  
				DISCRETELEGEND "A" / TITLE = "TITLEEE";
			 
			ENDLAYOUT;
		ENDGRAPH;
	END;

	DEFINE STYLE jphrecstyle;
		PARENT = Styles.Statistical;
		REPLACE fonts /
			'TitleFont'=("Arial,Helvetica,Helv",24pt,Bold Italic)
			'TitleFont2'=("Arial,Helvetica,Helv",24pt,Bold Italic)
			'StrongFont'=("Arial, Helvetica, Helv",12pt,Bold)
			'EmphasisFont'=("Arial,Helvetica,Helv",10pt,Italic)
			'headingFont'=("Arial, Helvetica, Helv",24pt,Bold)
			'docFont'=("Arial, Helvetica, Helv",11pt)
			'footFont'=("Arial, Helvetica, Helv",8pt)
			'headingEmphasisFont' =("Arial, Helvetica, Helv",24pt,Bold)
			'FixedStrongFont' =("Arial, Helvetica, Helv",24pt,Bold)
			'BatchFixedFont' =("Arial, Helvetica, Helv",24pt,Bold)
			'FixedFont' =("Arial, Helvetica, Helv",24pt,Bold)
			'FixedEmphasisFont' =("Arial, Helvetica, Helv",24pt,Bold)
		; 

		REPLACE GraphFonts /
			'GraphAnnoFont'		=("Arial, Helvetica, Helv",10pt,Bold)	/*? Annotation*/
			'GraphDataFont'		=("Arial, Helvetica, Helv",10pt,Bold)	/*???*/
			'GraphFootnoteFont'	=("Arial, Helvetica, Helv",24pt,Bold)
		    'GraphLabelFont'	=("Arial, Helvetica, Helv",14pt) 		/*Y Axis Label*/
			'GraphLabel2Font'	=("Arial, Helvetica, Helv",14pt)		/*X Axis Label*/
			'GraphTitleFont'	=("Arial, Helvetica, Helv",14pt,Bold)	/*Title*/
			'GraphTitle1Font'	=("Arial, Helvetica, Helv",24pt,Bold)
		    'GraphUnicodeFont'	=("Arial, Helvetica, Helv",24pt,Bold)
		    'GraphValueFont'	=("Arial, Helvetica, Helv",14pt)		/*Axes Font*/
		    'NodeDetailFont'	=("Arial, Helvetica, Helv",24pt,Bold)
			'NodeInputLabelFont'=("Arial, Helvetica, Helv",24pt,Bold)
			'NodeLabelFont'		=("Arial, Helvetica, Helv",24pt,Bold)
			'NodeTitleFont'		=("Arial, Helvetica, Helv",24pt,Bold)
		;
	END;
RUN;

PROC SGRENDER DATA = simall TEMPLATE = j;
	DYNAMIC _title = 'A' _plotname = 'A'
			_yvar = 'rr__rec_log'
			 _key = "N";
	FORMAT rr__rec_log loglabel.;
RUN;

PROC SGRENDER DATA = HuAll TEMPLATE = j;
	DYNAMIC	_title = 'B' _plotname = 'B'
			_yvar = 'H_log'
			_key = "N";
	FORMAT H_log loglabel.;
RUN;

PROC SGRENDER DATA = simall TEMPLATE = j;
	DYNAMIC _key = "Y"
			_title = 'C' _plotname = 'C'
			_type = "Hu"
			_yvar = 'z2rec_log';
	FORMAT z2rec_log loglabel.;
RUN;

PROC SGRENDER DATA = HuAll TEMPLATE = j;
	DYNAMIC _key = "Y"
			_title = 'D' _plotname = 'D'
			_type = "Hu"
			_yvar = 'AH_log';
	FORMAT AH_log loglabel.;
RUN;
ballardw
Super User
SCATTERPLOT X = type Y = _yvar /GROUP = siteClassMarks
				includemissinggroup = False
				JITTER = AUTO
				MARKERATTRS = (	SIZE = 8 )
				NAME = "_plotname"
				;

 Names in quotes like labels

 

 

Or you could create a dynamic variable to hold the names but that might get confusing as I can't think of a real good reason to do that.

djrisks
Barite | Level 11

Hi,

 

Can you share your datasets so I can take a further look please? I don't think the discrete legends are referenced properly in your examples.

 

Thanks.

benbuck
Obsidian | Level 7

Thanks for the offer to help! My colleagues and I decided to completely change the graph layout and now the legends are where we would like them to be.

 

Thanks!

Ben

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1377 views
  • 0 likes
  • 3 in conversation