BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sathish_jammy
Lapis Lazuli | Level 10

Hi Experts,

 

I share the Image of my table as well in the dataset format.

First please have a look at my images (Excel, sgplot) I want my diagram in the given format

 

 

data aaa;
input Age_Bins$ SysBP Dia_BP SysBP_M DiaBP_M;
cards;
20-29 199.8 82 116.3 76.9
30-39 134 88.4 119.3 78.3
40-49 135.2 85.5 121.4 79.9
50-59 141.3 82.6 124.6 80.7
60-69 142.7 79.4 128.4 81.4
70-80 148.9 82.6 132.8 81.4
;

 

 

img3.jpg

 
Proc sgplot data=aaa;
   reg x=Age_Bins y=DiaBP;
	reg x=Age_Bins y=DiaBP_M; 
run;
Using the above code I draw a sgplot.
img4.jpg
But I don't know how to put the group (INDIAB, MDRF), and the value of plot points in sgplot.
 
My expected plot which look like : (Sample)
 
Img5.jpg
 
Please suggest me some ideas to solve this method.
Thanks in advance!
1 ACCEPTED SOLUTION

Accepted Solutions
GraphGuy
Meteorite | Level 14

If you use a 'scatter' plot instead of a 'reg' plot, it will have an automatic legend at the bottom:

 

data aaa;
input Age_Bins$ SysBP Dia_BP SysBP_M DiaBP_M;
cards;
20-29 199.8 82 116.3 76.9
30-39 134 88.4 119.3 78.3
40-49 135.2 85.5 121.4 79.9
50-59 141.3 82.6 124.6 80.7
60-69 142.7 79.4 128.4 81.4
70-80 148.9 82.6 132.8 81.4
;
run;

title1 "Whatever title you want";
Proc sgplot data=aaa;
scatter x=Age_Bins y=Dia_BP / datalabel=Dia_BP;
scatter x=Age_Bins y=DiaBP_M / datalabel=DiaBP_M;
keylegend / title='Data:';
run;

 

legend.png

View solution in original post

7 REPLIES 7
sbxkoenk
SAS Super FREQ
PROC REPORT DATA=WORK.AAA LS=120 PS=64  SPLIT="/" CENTER 
    style(report)=[background=green cellspacing=2]
    style(column)=[background=yellow]
    style(header)=[background=pink foreground=blue]
	;    
COLUMN  Age_Bins ("Indiab"  SysBP Dia_BP ) 
                 ("MDRF"  SysBP_M DiaBP_M );

DEFINE  Age_Bins / DISPLAY FORMAT= $8. WIDTH=8    SPACING=2   LEFT  "Age_Bins" ;
DEFINE  SysBP    / SUM FORMAT= BEST9. WIDTH=9     SPACING=2   RIGHT "SBP"      ;
DEFINE  Dia_BP   / SUM FORMAT= BEST9. WIDTH=9     SPACING=2   RIGHT "DBP"      ;
DEFINE  SysBP_M  / SUM FORMAT= BEST9. WIDTH=9     SPACING=2   RIGHT "SBP_M"    ;
DEFINE  DiaBP_M  / SUM FORMAT= BEST9. WIDTH=9     SPACING=2   RIGHT "DBP_M"    ;
RUN;
sbxkoenk
SAS Super FREQ
Proc sgplot data=aaa;
   reg x=Age_Bins y=Dia_BP  / datalabel=Dia_BP; 
   reg x=Age_Bins y=DiaBP_M / datalabel=DiaBP_M; 
run;
Sathish_jammy
Lapis Lazuli | Level 10

Dear @sbxkoenk  @ballardw 

Thank you so much for your suggestions. Your code works well... I worked on both of your codes its wondering...

Everything is perfect except the LABEL description...

img5.jpg

I can't see that LABEL - DATA at the bottom of the plot which we used a group in sgplot...

( Data :   x Dia_BP      o DiaBP_M )  ???????? 

sbxkoenk
SAS Super FREQ

To put curvelabels, see here:

 

Advanced ODS Graphics: Curve labels and date axes

https://blogs.sas.com/content/graphicallyspeaking/2017/10/25/advanced-ods-graphics-curve-labels-and-...

 

By Warren F. Kuhfeld on the "Graphically Speaking" BLOG  | October 25, 2017

 

Cheers,

Koen

 

GraphGuy
Meteorite | Level 14

If you use a 'scatter' plot instead of a 'reg' plot, it will have an automatic legend at the bottom:

 

data aaa;
input Age_Bins$ SysBP Dia_BP SysBP_M DiaBP_M;
cards;
20-29 199.8 82 116.3 76.9
30-39 134 88.4 119.3 78.3
40-49 135.2 85.5 121.4 79.9
50-59 141.3 82.6 124.6 80.7
60-69 142.7 79.4 128.4 81.4
70-80 148.9 82.6 132.8 81.4
;
run;

title1 "Whatever title you want";
Proc sgplot data=aaa;
scatter x=Age_Bins y=Dia_BP / datalabel=Dia_BP;
scatter x=Age_Bins y=DiaBP_M / datalabel=DiaBP_M;
keylegend / title='Data:';
run;

 

legend.png

ballardw
Super User

@Sathish_jammy wrote:

Hi Experts,

 

I share the Image of my table as well in the dataset format.

First please have a look at my images (Excel, sgplot) I want my diagram in the given format

 

 

data aaa;
input Age_Bins$ SysBP Dia_BP SysBP_M DiaBP_M;
cards;
20-29 199.8 82 116.3 76.9
30-39 134 88.4 119.3 78.3
40-49 135.2 85.5 121.4 79.9
50-59 141.3 82.6 124.6 80.7
60-69 142.7 79.4 128.4 81.4
70-80 148.9 82.6 132.8 81.4
;

 

 

img3.jpg

 
Proc sgplot data=aaa;
   reg x=Age_Bins y=DiaBP;
	reg x=Age_Bins y=DiaBP_M; 
run;
Using the above code I draw a sgplot.
img4.jpg
But I don't know how to put the group (INDIAB, MDRF), and the value of plot points in sgplot.
 
My expected plot which look like : (Sample)
 
Img5.jpg
 
Please suggest me some ideas to solve this method.
Thanks in advance!

Please provide data step AND proc step that actually generate your output. The data step does not have a variable DIABP mentioned in the SGPLOT code. I assumed that should have been DIA_BP. However your X variable AGE_BINS is character in the data set and you can't use a character variable for regression.

Also you mention  "group (INDIAB, MDRF),". There are no variables or data values of those so where are we supposed to get that information. We need those variables, or the variable with those values in the data step.

 

Typically to get SGPLOT to do something similar to your desired graph you would be plotting a single y variable with a group variable that indicates which group of data each record comes from. IF dia_BP is supposed to be the INDIAB group and DIABP_M is supposed to be from the MDRF group then your data needs to be restructured to have a variable holding the group:

 

data aaa;
input Age_Bins  SysBP Dia_BP SysBP_M DiaBP_M;
cards;
20 199.8 82 116.3 76.9
30 134 88.4 119.3 78.3
40 135.2 85.5 121.4 79.9
50 141.3 82.6 124.6 80.7
60 142.7 79.4 128.4 81.4
70 148.9 82.6 132.8 81.4
;
run;

data want;
   set aaa;
   SysBP=SysBP   ; DiaBP=Dia_bp; pop="INDIAB"; output;
   SysBP=SysBP_M; DiaBP=DiaBP_M; pop="MDRF"; output;
   keep age_bins sysbp diabp pop;
run;


Proc sgplot data=want;
   reg x=Age_Bins y=DiaBP/ group=pop datalabel;
   label pop='Data';

run;

You would need a custom format to apply the labels to the X axis values as ranges. I leave that as an exercise for the interested reader.

 

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Sathish_jammy
Lapis Lazuli | Level 10

This too works Perfectly... for label change options...

 

Proc sgplot data=aaa;
scatter x=Age_Bins y=SysBP / datalabel=SysBP LEGENDLABEL = 'INDIAB' markerattrs=(symbol=CircleFilled size=10);
scatter x=Age_Bins y=SysBP_M / datalabel=SysBP_M LEGENDLABEL = 'MDRF' markerattrs=(symbol=CircleFilled size=10);
YAXIS LABEL = 'SysBP';
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 7 replies
  • 3551 views
  • 6 likes
  • 4 in conversation