Barite | Level 11

## How to place the Macro variable in SGPANEL graphs Panels.

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.

``````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

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
Lapis Lazuli | Level 10

## Re: How to place the Macro variable in SGPANEL graphs Panels.

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;``````

And here is the link from Help Documentation.

8 REPLIES 8
Diamond | Level 26

## Re: How to place the Macro variable in SGPANEL graphs Panels.

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
Barite | Level 11

## Re: How to place the Macro variable in SGPANEL graphs Panels.

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!

Lapis Lazuli | Level 10

## Re: How to place the Macro variable in SGPANEL graphs Panels.

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;``````

And here is the link from Help Documentation.

Barite | Level 11

## Re: How to place the Macro variable in SGPANEL graphs Panels.

Thank you, @whymath . It worked.

Diamond | Level 26

## Re: How to place the correlation in SGPANEL graphs Panels.

@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!

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
Barite | Level 11

## Re: How to place the correlation in SGPANEL graphs Panels.

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.

Diamond | Level 26

## Re: How to place the correlation in SGPANEL graphs Panels.

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

--
Paige Miller
Barite | Level 11

## Re: How to place the Macro variable in SGPANEL graphs Panels.

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.

Discussion stats
• 8 replies
• 798 views
• 3 likes
• 3 in conversation