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

I created the multi-Panels graph by using the SGPANEL. These graphs include the scatter and the regression graphs. But I want to display the correlation values calculated and converted to a macro variable. Can the values be displayed on the respective panels? If not, what's the other way I can achieve this? We did so much customization in SGPANEL, we trying to keep the SGPANEL. 

The following is the example code where we want to display the 'Correlation' values that were converted to macro variables that want to be displayed on the respective 'Sex' panel graphs.

SASuserlot_0-1688420795121.png

 

data classfit;
set sashelp.classfit;
if age<=13 then group = 1;
if 13<age<=14 then group =2;
else group = 3;
if sex ='F' then sexn= 1;
else sexn =2;
run;

proc sort data = classfit; by sex sexn; run;

proc corr data=classfit ;*PLOTS=SCATTER(NVAR=all);
  var  height weight; /* Specify the variables you want to calculate the correlation for */
  by sex sexn;
  ods output PearsonCorr= corr; /* Create an output dataset to store the correlation values */
run;

data corr (keep =  variable weight pweight sex sexn);
set corr ;
where variable = "Height";
run;


data _null_;
     set corr;

call symput ('r_'||trim(variable)||trim(left(put(sexn,best.))),trim(put(weight,6.4)));
call symput ('p_'||trim(variable)||trim(left(put(sexn,best.))),trim(put(pweight,6.4)));

run;

 %put &r_height1;

ods html;

proc sgpanel data= classfit ;
panelby sex / rows=1 columns=2  sort =data novarname  uniscale= column 
	;*HEADERATTRS=(Color=Navy Family=Arial Size=8 
         Weight=Bold) HEADERBACKCOLOR=salmon;

scatter  x= height y = weight / group = sex markerattrs=(symbol=diamond );
reg     x= height  y = weight /   /*datalabel=trt01p*/  ;

colaxis  fitpolicy=ROTATEALWAYS VALUESROTATE=DIAGONAL2 
		;
rowaxis LABELATTRS=( Weight=Bold) label= 'xxx'  grid ;

run;

Thank you for your feed back

1 ACCEPTED SOLUTION

Accepted Solutions
whymath
Lapis Lazuli | Level 10

Yes, you can use INSET statement:

data merged;
  merge classfit corr(rename=(weight=r pweight=p));
  by sex sexn;

  r_text=strip(put(r,12.4));
  p_text=strip(put(p,pvalue6.4));
  label r_text='r' p_text='p-value';
run;

ods html;
proc sgpanel data= merged ;
  panelby sex / rows=1 columns=2  sort =data novarname  uniscale= column;
  scatter  x= height y = weight / group = sex markerattrs=(symbol=diamond );
  reg     x= height  y = weight /   /*datalabel=trt01p*/  ;
  inset r_text p_text/position=topleft title='Correlation' separator='=';
  colaxis  fitpolicy=rotatealways valuesrotate=diagonal2;
  rowaxis labelattrs=( weight=bold) label= 'xxx'  grid ;
run;

1.png

And here is the link from Help Documentation.

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26
You don't use &R_height anywhere. Where would it go? Show us code that works WITHOUT macro variables that uses a hard-coded height for the correlation.
--
Paige Miller
SASuserlot
Barite | Level 11

Thank you @PaigeMiller  for checking on this query.  Apologies if my question was not clear.

I did not use '&r_height''  anywhere because I am unsure where to use it in the SGPANAL code for display even without being a macro variable.

I am ok wherever it's displayed on the graph wall. It may be Top Left. based on the sex in both panels

After reading your question. I got a thought, is it possible to merge the R_height values from 'corr' dataset to 'classfit' dataset and use it in the 'INSET' statement? I can visually think but not sure that can be doable!

 

SASuserlot_0-1688433663796.png

 

whymath
Lapis Lazuli | Level 10

Yes, you can use INSET statement:

data merged;
  merge classfit corr(rename=(weight=r pweight=p));
  by sex sexn;

  r_text=strip(put(r,12.4));
  p_text=strip(put(p,pvalue6.4));
  label r_text='r' p_text='p-value';
run;

ods html;
proc sgpanel data= merged ;
  panelby sex / rows=1 columns=2  sort =data novarname  uniscale= column;
  scatter  x= height y = weight / group = sex markerattrs=(symbol=diamond );
  reg     x= height  y = weight /   /*datalabel=trt01p*/  ;
  inset r_text p_text/position=topleft title='Correlation' separator='=';
  colaxis  fitpolicy=rotatealways valuesrotate=diagonal2;
  rowaxis labelattrs=( weight=bold) label= 'xxx'  grid ;
run;

1.png

And here is the link from Help Documentation.

SASuserlot
Barite | Level 11

Thank you, @whymath . It worked.

PaigeMiller
Diamond | Level 26

@SASuserlot wrote:

Thank you @PaigeMiller  for checking on this query.  Apologies if my question was not clear.

I did not use '&r_height''  anywhere because I am unsure where to use it in the SGPANAL code for display even without being a macro variable.

I am ok wherever it's displayed on the graph wall. It may be Top Left. based on the sex in both panels

After reading your question. I got a thought, is it possible to merge the R_height values from 'corr' dataset to 'classfit' dataset and use it in the 'INSET' statement? I can visually think but not sure that can be doable!

 

SASuserlot_0-1688433663796.png

 


I see that @whymath has provided an answer ... which brings me back to my point ... don't specify that the solution you want is a macro or macro variable solution, when the problem was not how to use a macro or macro variable; the problem was how to allow correlation to appear in SGPANEL output. In the future, better to describe the problem rather than saying you want a specific tool as part of the solution. @SASuserlot: Would you please be kind and help everyone out by changing the subject line on your ORIGINAL post to say "How to place correlation into SGPANEL graph Panels".

--
Paige Miller
SASuserlot
Barite | Level 11

Thank you @PaigeMiller  for correcting me and for the suggestion. Unfortunately, when I posted the initial query, I was in the mindset that  I need to create a macro variable for each graph in the panel and display it using that macro variable. I think I was not thinking straight at that moment. But when you asked the question, it sparked me; I asked and did the concept incorrectly (maybe my brain stopped thinking, Sorry! for my mistake). I am trying to edit the question, but I think I am beyond the 'Edit' window regarding timing. This is the first time for me. Can you please suggest, How I can edit the question? Once I edit the question, I will close the post.

SASuserlot_0-1688483326940.png

 

PaigeMiller
Diamond | Level 26

You may be right, after a certain time period, you can't go back and edit your posts.

--
Paige Miller
SASuserlot
Barite | Level 11

Unfortunately, I can not edit the post title. This post deals with How to display the external calculation and use it in the 'Inset' statement of the Graphs while using SGPANEL. In this case, displaying the 'Correlation value' on plots.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 8 replies
  • 874 views
  • 3 likes
  • 3 in conversation