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

Hi,

 

Is it possible to insert p-values of the regression line into proc sgplot? I'm using the below code but I'm not sure if this is possible with the inset statement?

proc sgplot data=work;

title"responsiveness vs. BMI";

reg y=fr x=factor1 /degree=1 clm cli alpha=0.05;

run;

 

Many thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You have logical errors all over the place. 

 

When you try and assign the p-value you use the estimate column and not the p-value column. 

else if _n_ = 1 then call symput('Pvalue', put(estimate, BEST6.));

You take the p-value from the intercept record not the slope line which is a bit unusual, but maybe what you wanted?? I would assume you'd want the estimate from ANOVA table not the parameter estimate but that's ultimately your choice on what you want to present. 

 

The IF/ELSE conditions don't make work, because it only evaluates the first condition. So the second IF statement matches the first IF (else if _n_ =1 ) will never be TRUE for the second line of your IF statements. You'll never execute that line with your current logic. 

 

if _n_ = 1 then call symput('Int', put(estimate, BEST6.));
*will never execute;
else if _n_ = 1 then call symput('Pvalue', put(estimate, BEST6.));
else call symput('Slope', put(estimate, BEST6.));

 

This works as expected, but gets the p-value from the ANOVA table instead. 

 

proc reg data=sashelp.class;
model weight = height;
ods output ParameterEstimates=PE anova=anv;
run;
 
data _null_;
set PE;
if _n_ = 1 then call symputx('Int', put(estimate, BEST6.));
if _n_ = 2 then call symputx('Slope', put(estimate, BEST6.));
run;

data _null_;
set ANV;
if _n_ = 1 then call symputx('pvalue', put(probF, pvalue6.4));
run;

%put ∫
%put &pvalue;
%put &slope;
 
proc sgplot data=sashelp.class;
reg y=weight x=height /degree=1 clm cli alpha=0.05;
inset "Intercept = &Int" "Slope = &Slope" "P-value = &Pvalue" /
border title="Parameter Estimates" position=topleft;
run;

 

 


@catch18 wrote:

Thanks @Reeza!

Yes, it's a long code and considering I have to do this for a long list of variables, but thanks.

It worked for the intercept and slope but not for the p-value. I did:

 

proc reg data=work;

model fr = factor1;

ods output ParameterEstimates=PE;

run;

 

data _null_;

set PE;

if _n_ = 1 then call symput('Int', put(estimate, BEST6.));

else if _n_ = 1 then call symput('Pvalue', put(estimate, BEST6.));

else call symput('Slope', put(estimate, BEST6.));

run;

 

proc sgplot data=work;

title"responsiveness vs. BMI";

reg y=fr x=factor1 /degree=1 clm cli alpha=0.05;

inset "Intercept = &Int" "Slope = &Slope" "Pvalue = &Pvalue" /

border title="Parameter Estimates" position=topleft;

run;

 

The p-value I get from running the regression line is different from what is inserted in the figure? I'm sure my pvalue statement in the data step is not right. I don't get any errors though.

 

Thanks.


 

View solution in original post

10 REPLIES 10
Reeza
Super User

Hoping someone has a better answer than this:

https://blogs.sas.com/content/iml/2013/02/27/slope-of-a-regression-line.html

 


@catch18 wrote:

Hi,

 

Is it possible to insert p-values of the regression line into proc sgplot? I'm using the below code but I'm not sure if this is possible with the inset statement?

proc sgplot data=work;

title"responsiveness vs. BMI";

reg y=fr x=factor1 /degree=1 clm cli alpha=0.05;

run;

 

Many thanks.


 

catch18
Obsidian | Level 7

Thanks @Reeza!

Yes, it's a long code and considering I have to do this for a long list of variables, but thanks.

It worked for the intercept and slope but not for the p-value. I did:

 

proc reg data=work;

model fr = factor1;

ods output ParameterEstimates=PE;

run;

 

data _null_;

set PE;

if _n_ = 1 then call symput('Int', put(estimate, BEST6.));

else if _n_ = 1 then call symput('Pvalue', put(estimate, BEST6.));

else call symput('Slope', put(estimate, BEST6.));

run;

 

proc sgplot data=work;

title"responsiveness vs. BMI";

reg y=fr x=factor1 /degree=1 clm cli alpha=0.05;

inset "Intercept = &Int" "Slope = &Slope" "Pvalue = &Pvalue" /

border title="Parameter Estimates" position=topleft;

run;

 

The p-value I get from running the regression line is different from what is inserted in the figure? I'm sure my pvalue statement in the data step is not right. I don't get any errors though.

 

Thanks.

ballardw
Super User

@catch18 wrote:

Thanks @Reeza!

Yes, it's a long code and considering I have to do this for a long list of variables, but thanks.

It worked for the intercept and slope but not for the p-value. I did:

 

proc reg data=work;

model fr = factor1;

ods output ParameterEstimates=PE;

run;

 

data _null_;

set PE;

if _n_ = 1 then call symput('Int', put(estimate, BEST6.));

else if _n_ = 1 then call symput('Pvalue', put(estimate, BEST6.));

else call symput('Slope', put(estimate, BEST6.));

run;

 

proc sgplot data=work;

title"responsiveness vs. BMI";

reg y=fr x=factor1 /degree=1 clm cli alpha=0.05;

inset "Intercept = &Int" "Slope = &Slope" "Pvalue = &Pvalue" /

border title="Parameter Estimates" position=topleft;

run;

 

The p-value I get from running the regression line is different from what is inserted in the figure? I'm sure my pvalue statement in the data step is not right. I don't get any errors though.

 

Thanks.


The BEST format will display values a bit differently than typical for SAS Procedures showing p-values. You might try using the PVALUE format instead of BEST in the data _null_ step;

 

data _null_;
   set PE;
   if _n_ = 1 then call symput('Int', put(estimate, BEST6.)); 
   else if _n_ = 1 then call symput('Pvalue', put(estimate, pvalue6.4)); 
   else call symput('Slope', put(estimate, BEST6.)); 

run;

 

Or provide the input data set as data step code so we can recreate your work. 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.

 

Then describe what about the result you want to change. 

catch18
Obsidian | Level 7

Thanks @ballardw 

The log for the data step:

NOTE: There were 2 observations read from the data set WORK.PE. - I'm thinking there should be 3 observations instead of 2?

 

Also, the pvalue inserted is the same as the value for the intercept. When I run the proc reg, the value inserted in the graph for the intercept is correct and this same intercept value is inserted also for the pvalue which is wrong.

 

Many thanks.

catch18
Obsidian | Level 7

@ballardw, Sorry, I missed the 'or' part of your comment. I'm thinking the code should work for any dataset so long as you can perform regression analysis on it. I have included my demo data here;

DATA work;
INPUT SUBJECT $ Factor1 FR;
DATALINES;
AE 28 4.5
FR 35 3.9
HT 37 3.9
IO 50 6.1
DP 69 4.3
YR 84 8.8
QD 40 2.1
SW 65 5.5
DF 29 5.7
ER 42 3.0
RR 51 7.1
TG 45 7.3
EF 31 3.3
TJ 40 5.2
;
RUN;

 

Thanks.

Reeza
Super User

You have logical errors all over the place. 

 

When you try and assign the p-value you use the estimate column and not the p-value column. 

else if _n_ = 1 then call symput('Pvalue', put(estimate, BEST6.));

You take the p-value from the intercept record not the slope line which is a bit unusual, but maybe what you wanted?? I would assume you'd want the estimate from ANOVA table not the parameter estimate but that's ultimately your choice on what you want to present. 

 

The IF/ELSE conditions don't make work, because it only evaluates the first condition. So the second IF statement matches the first IF (else if _n_ =1 ) will never be TRUE for the second line of your IF statements. You'll never execute that line with your current logic. 

 

if _n_ = 1 then call symput('Int', put(estimate, BEST6.));
*will never execute;
else if _n_ = 1 then call symput('Pvalue', put(estimate, BEST6.));
else call symput('Slope', put(estimate, BEST6.));

 

This works as expected, but gets the p-value from the ANOVA table instead. 

 

proc reg data=sashelp.class;
model weight = height;
ods output ParameterEstimates=PE anova=anv;
run;
 
data _null_;
set PE;
if _n_ = 1 then call symputx('Int', put(estimate, BEST6.));
if _n_ = 2 then call symputx('Slope', put(estimate, BEST6.));
run;

data _null_;
set ANV;
if _n_ = 1 then call symputx('pvalue', put(probF, pvalue6.4));
run;

%put ∫
%put &pvalue;
%put &slope;
 
proc sgplot data=sashelp.class;
reg y=weight x=height /degree=1 clm cli alpha=0.05;
inset "Intercept = &Int" "Slope = &Slope" "P-value = &Pvalue" /
border title="Parameter Estimates" position=topleft;
run;

 

 


@catch18 wrote:

Thanks @Reeza!

Yes, it's a long code and considering I have to do this for a long list of variables, but thanks.

It worked for the intercept and slope but not for the p-value. I did:

 

proc reg data=work;

model fr = factor1;

ods output ParameterEstimates=PE;

run;

 

data _null_;

set PE;

if _n_ = 1 then call symput('Int', put(estimate, BEST6.));

else if _n_ = 1 then call symput('Pvalue', put(estimate, BEST6.));

else call symput('Slope', put(estimate, BEST6.));

run;

 

proc sgplot data=work;

title"responsiveness vs. BMI";

reg y=fr x=factor1 /degree=1 clm cli alpha=0.05;

inset "Intercept = &Int" "Slope = &Slope" "Pvalue = &Pvalue" /

border title="Parameter Estimates" position=topleft;

run;

 

The p-value I get from running the regression line is different from what is inserted in the figure? I'm sure my pvalue statement in the data step is not right. I don't get any errors though.

 

Thanks.


 

catch18
Obsidian | Level 7

@Reeza, It surely worked!!  Many thanks!!

 

I just wondered, why the double quotation marks at the end of the two lines in the null data step when only 1 is opened in each?

 

I know my last question probably has no immediate solution - is it possible to run this without having to rewrite the whole code for each of 60 variables?

 

Thanks.

 

Reeza
Super User

I see no extra quotes, please clarify. 

You can create a macro and call it 60 times via call execute or you can change your code to use BY groups. I’d suggest you look into both. BY groups will process faster. Macro tutorials are below. I recommend the second link in this case but your choice. 

UCLA introductory tutorial on macro variables and macros

https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/

Tutorial on converting a working program to a macro

This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

Examples of common macro usage

https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

catch18
Obsidian | Level 7

Please ignore the quotation mark question.

 

Thank you so so much!!!

mariko5797
Pyrite | Level 9

Is it possible to do this with SGPANEL where each panel has its own regression line and p-value? For example, I make an interaction term of time*BMI and plot concentration vs. time panel by BMI groups. I want the not-obese panel to have a regression line and the p-value from time*not_obese and so forth.

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
  • 10 replies
  • 5194 views
  • 2 likes
  • 4 in conversation