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

I am trying to create a graph, using SGPLOT with both scatter and series statement. 

1. Please correct me if I am using the wrong type of Graphs statement for this kind of graphs ( I just want to show %chg Vs percentage of population). I want to show data point and a line connecting them.

2. Why my key legend shows 4 different groups, instead of showing two groups where circle should overlap the line?

SASuserlot_0-1639149371855.png

 

3. I would like to insert a big arrow symbol on both sides of '0' indicating score improve or Decline!.

SASuserlot_1-1639149821852.png

data trial;
input subject trt response chg;
cards;
1001 1 15 20
1002 1 16 25
1003 1 17 30
1004 1 -6 -20
1005 1 0 0
1006 1 -5 -25
1007 1 15 20
1008 1 16 25
1009 1 17 30
1010 1 18 30
1011 2 29 50
1012 2 -4 -12
1013 2 13 10
1014 2 12 8
1015 2 0 0
1016 2 28 45
1017 2 29 50
1018 2 -4 -12
1019 2 29 50
1020 2 -4 -12

;
run;

proc sort data=trial out= trial1; by trt response; run;
data trial2;
set trial1;
by trt response;
 if first.response then cnt=0;
 if .<chg<=response then cnt+1;
 if .<chg>response then cnt+1;
  if last.response then do;
   if trt=1 then yvalue=cnt/10 ;*10- indicated total poputation where trt=1 and 2;
   else if trt=2 then yvalue=cnt/10;
  end;
 run;

ods listing close;                                                                                                                      
ods html ;*file='scatterplot.html' path='.' style=styles.statistical;                                                                     
ods graphics / reset width=600px height=400px imagefmt=gif;                                                                             
                                                                                                                                        
/* The SCATTER statement generates the scatter plot with error bars. */                                                                 
/* The SERIES statement draws the line to connect the means.         */                                                                 
proc sgplot data=trial2 noautolegend;                                                                                                  
   scatter x=chg y=yvalue / group= trt;* markerattrs=(color=blue symbol=CircleFilled);                                                                
   series x=chg y=yvalue / group= trt ;*lineattrs=(color=blue pattern=2);  
	xaxis  min=-40 max=40; 
	yaxis  min=0 max= 0.5; 
keylegend / valueattrs=(size=12pt) titleattrs=(size=14pt);
run;                                                                                                                                    
                                                                                                                                        
ods html close;                                                                                                                         
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data trial;
input subject trt response chg;
cards;
1001 1 15 20
1002 1 16 25
1003 1 17 30
1004 1 -6 -20
1005 1 0 0
1006 1 -5 -25
1007 1 15 20
1008 1 16 25
1009 1 17 30
1010 1 18 30
1011 2 29 50
1012 2 -4 -12
1013 2 13 10
1014 2 12 8
1015 2 0 0
1016 2 28 45
1017 2 29 50
1018 2 -4 -12
1019 2 29 50
1020 2 -4 -12

;
run;

proc sort data=trial out= trial1; by trt response; run;
data trial2;
set trial1;
by trt response;
 if first.response then cnt=0;
 if .<chg<=response then cnt+1;
 if .<chg>response then cnt+1;
  if last.response then do;
   if trt=1 then yvalue=cnt/10 ;*10- indicated total poputation where trt=1 and 2;
   else if trt=2 then yvalue=cnt/10;
  end;
 run;

data arrow;
y=0;y1=0.05;
low1=-30;high1=-10;x1=-20;text='YYYYYYYY';output;
call missing(of _all_);
y=0;y1=0.05;
low2=10 ;high2=30;x2=20;text='XXXXXXX';output;
run;
data trial3;
 set trial2 arrow;
run;

ods graphics / reset width=600px height=400px imagefmt=gif;                                                                             
                                                                                                                                        
/* The SCATTER statement generates the scatter plot with error bars. */                                                                 
/* The SERIES statement draws the line to connect the means.         */                                                                 
proc sgplot data=trial3 ;                                                                                                  
   series x=chg y=yvalue / group= trt markers name='a';*lineattrs=(color=blue pattern=2);  
   highlow y=y low=low1 high=high1/lowcap=BARBEDARROW ; 
   highlow y=y low=low2 high=high2/highcap=BARBEDARROW ;
   text y=y1 x=x1 text=text/strip CONTRIBUTEOFFSETS=none;
   text y=y1 x=x2 text=text/strip CONTRIBUTEOFFSETS=none;
	xaxis  min=-40 max=40; 
	yaxis  min=0 max= 0.5; 
keylegend 'a' / valueattrs=(size=12pt) titleattrs=(size=14pt);
run;                                                                                                                                    
                                                                                                                                        

Ksharp_1-1639222306301.png

 

View solution in original post

7 REPLIES 7
ballardw
Super User

Remove the scatter statement; add the options MARKERS and MARKERATTRS=(SYMBOL=CIRCLE) to the series statement.

That way you only have 1 legend element and you can't get different legends to overlay.

 

There are many other other Marker related options for the Series statement so feel free to play with them.

 


@SASuserlot wrote:

I am trying to create a graph, using SGPLOT with both scatter and series statement. 

1. Please correct me if I am using the wrong type of Graphs statement for this kind of graphs ( I just want to show %chg Vs percentage of population). I want to show data point and a line connecting them.

2. Why my key legend shows 4 different groups, instead of showing two groups where circle should overlap the line?

SASuserlot_0-1639149371855.png

 

3. I would like to insert a big arrow symbol on both sides of '0' indicating score improve or Decline!.

SASuserlot_1-1639149821852.png

data trial;
input subject trt response chg;
cards;
1001 1 15 20
1002 1 16 25
1003 1 17 30
1004 1 -6 -20
1005 1 0 0
1006 1 -5 -25
1007 1 15 20
1008 1 16 25
1009 1 17 30
1010 1 18 30
1011 2 29 50
1012 2 -4 -12
1013 2 13 10
1014 2 12 8
1015 2 0 0
1016 2 28 45
1017 2 29 50
1018 2 -4 -12
1019 2 29 50
1020 2 -4 -12

;
run;

proc sort data=trial out= trial1; by trt response; run;
data trial2;
set trial1;
by trt response;
 if first.response then cnt=0;
 if .<chg<=response then cnt+1;
 if .<chg>response then cnt+1;
  if last.response then do;
   if trt=1 then yvalue=cnt/10 ;*10- indicated total poputation where trt=1 and 2;
   else if trt=2 then yvalue=cnt/10;
  end;
 run;

ods listing close;                                                                                                                      
ods html ;*file='scatterplot.html' path='.' style=styles.statistical;                                                                     
ods graphics / reset width=600px height=400px imagefmt=gif;                                                                             
                                                                                                                                        
/* The SCATTER statement generates the scatter plot with error bars. */                                                                 
/* The SERIES statement draws the line to connect the means.         */                                                                 
proc sgplot data=trial2 noautolegend;                                                                                                  
   scatter x=chg y=yvalue / group= trt;* markerattrs=(color=blue symbol=CircleFilled);                                                                
   series x=chg y=yvalue / group= trt ;*lineattrs=(color=blue pattern=2);  
	xaxis  min=-40 max=40; 
	yaxis  min=0 max= 0.5; 
keylegend / valueattrs=(size=12pt) titleattrs=(size=14pt);
run;                                                                                                                                    
                                                                                                                                        
ods html close;                                                                                                                         

 

 

SASuserlot
Barite | Level 11

Legend, data points fixed. Thanks @ballardw . How about inserting the 'Arrow' Labels under the axis like I mentioned in the image.

SASuserlot
Barite | Level 11

Thank you very much.

Ksharp
Super User
data trial;
input subject trt response chg;
cards;
1001 1 15 20
1002 1 16 25
1003 1 17 30
1004 1 -6 -20
1005 1 0 0
1006 1 -5 -25
1007 1 15 20
1008 1 16 25
1009 1 17 30
1010 1 18 30
1011 2 29 50
1012 2 -4 -12
1013 2 13 10
1014 2 12 8
1015 2 0 0
1016 2 28 45
1017 2 29 50
1018 2 -4 -12
1019 2 29 50
1020 2 -4 -12

;
run;

proc sort data=trial out= trial1; by trt response; run;
data trial2;
set trial1;
by trt response;
 if first.response then cnt=0;
 if .<chg<=response then cnt+1;
 if .<chg>response then cnt+1;
  if last.response then do;
   if trt=1 then yvalue=cnt/10 ;*10- indicated total poputation where trt=1 and 2;
   else if trt=2 then yvalue=cnt/10;
  end;
 run;

data arrow;
y=0;y1=0.05;
low1=-30;high1=-10;x1=-20;text='YYYYYYYY';output;
call missing(of _all_);
y=0;y1=0.05;
low2=10 ;high2=30;x2=20;text='XXXXXXX';output;
run;
data trial3;
 set trial2 arrow;
run;

ods graphics / reset width=600px height=400px imagefmt=gif;                                                                             
                                                                                                                                        
/* The SCATTER statement generates the scatter plot with error bars. */                                                                 
/* The SERIES statement draws the line to connect the means.         */                                                                 
proc sgplot data=trial3 ;                                                                                                  
   series x=chg y=yvalue / group= trt markers name='a';*lineattrs=(color=blue pattern=2);  
   highlow y=y low=low1 high=high1/lowcap=BARBEDARROW ; 
   highlow y=y low=low2 high=high2/highcap=BARBEDARROW ;
   text y=y1 x=x1 text=text/strip CONTRIBUTEOFFSETS=none;
   text y=y1 x=x2 text=text/strip CONTRIBUTEOFFSETS=none;
	xaxis  min=-40 max=40; 
	yaxis  min=0 max= 0.5; 
keylegend 'a' / valueattrs=(size=12pt) titleattrs=(size=14pt);
run;                                                                                                                                    
                                                                                                                                        

Ksharp_1-1639222306301.png

 

SASuserlot
Barite | Level 11

Perfect. for my future understanding .. we have to create a separate data sets guiding the axis points and append to the original dataset, in order to place any thing under the graph for the things that we can not  be achieved in the sgplot? Thank you.

Ksharp
Super User
Yes. I think so.
Or you could refer to the blog URL I posted before.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 7 replies
  • 1556 views
  • 5 likes
  • 3 in conversation