BookmarkSubscribeRSS Feed
Heejeong
Obsidian | Level 7

Thank you so much, @Rick_SAS, for your helpful comments in my previous post ("Re: Graphing with percentage values"), Graphing with percentage values. For some reason, my responses are not getting recorded in response to your helpful comments even after trying it out several times so I had to start a new chain here. I had two follow-up questions to your latest response:

 

1. First, I tried very hard to figure this out yesterday but I couldn't quite understand how subtracting and adding the P1 value is making the csrpa_mlm2 variable into a percentage. I apologize for not being able to find an answer to this question on my own but would greatly appreciate any explanation.

 

2. Second, I ran the code you have kindly provided and everything ran fine. However, when I tried the run the MARGIN MACRO (posted below), I received the following error message: ERROR: Variable csrpa_mlm2_Pct not found. Could I get any advice on how I can address this problem? 

proc means data=JH.Final3;
var csrpa_mlm2; 
output out=out mean=mean std=sd min=Min max=Max;
run;

data mdat;
set out;
keep csrpa_mlm2;
P1 = 0.01 / (Max - Min);
do csrpa_mlm2 = mean-P1, mean, mean+P1;
if csrpa_mlm2 = mean-P1 then csrpa_mlm2_Pct = -0.01;
else if csrpa_mlm2 = mean+P1 then csrpa_mlm2_Pct = 0.01;
else csrpa_mlm2_Pct = 0.0;
output;
end;
format csrpa_mlm2_Pct PERCENT6.2;
run;
%Margins(data=JH.Final3, response=Norepinephrine, model=cage ClinicSex marriedmidus work race_orig  cedu cHHtotalIncome EverSmokeReg Exercise20mins CNSmeds cBMI cCESD cNeuroticism  cChronCondNumb  cAnyStressWide_sum cpa_mlm2 csrpa_mlm2_Pct,
margins=csrpa_mlm2_Pct, margindata=mdat,
options=diff reverse cl)

Heejeong_1-1654102442957.png

After I got the error message, I ran the MARGINS MACRO using the original csrpa_mlm2 variable and the syntax ran fine, but this leaves me with the original non-percentage x-axis value.

 

Thank you so much for your additional thoughts on my question!

4 REPLIES 4
Rick_SAS
SAS Super FREQ

> how subtracting and adding the P1 value is making the csrpa_mlm2 variable into a percentage

The csrpa_mlm2 variable is the data scale and ranges from Min to Max.  The P1 value is 1% of the range (Max - Min).   From your existing program, it looked like you wanted to evaluate the model at the mean and at mean +/- Delta.  In your original post, you used Delta=Standard Deviation. You said you wanted to use 1% of the range instead, so the new program uses Delta=P1.

 

> when I tried the run the MARGIN MACRO (posted below), I received the following error message: ERROR: Variable csrpa_mlm2_Pct not found

 

You must use the original variables in the model and in the macro. Since I cannot run your code, I am not sure what the problem is, but probably the output data set from the %MARGINS macro does not include the csrpa_mlm2_Pct  variable. In that case, I put the code to create the csrpa_mlm2_Pct variable in the wrong file. Use a second DATA step after the macro call and before the PROC SGPLOT call to add the csrpa_mlm2_Pct variable to the _margins data.  Something like

data Margins;
set _margins;
  /* redefine P1 if it isn't in _margins */
   if csrpa_mlm2 = mean-P1 then csrpa_mlm2_Pct = -0.01;
   else if csrpa_mlm2 = mean+P1 then csrpa_mlm2_Pct = 0.01;
   else csrpa_mlm2_Pct = 0.0;
format csrpa_mlm2_Pct PERCENT6.2;
run;

proc sgplot data=Margins noautolegend;
band upper=UPPER lower=LOWER x=csrpa_mlm2_Pct ;
series y=ESTIMATE x=csrpa_mlm2_Pct;
,,,

 

 

 

Heejeong
Obsidian | Level 7

Thank you so much for the detailed explanation of the code you provided, @Rick_SAS!

 

I now understand what the P1 value means and everything makes sense! I just had one last question. So I created the new syntax below (to create the P1 value in the new margins dataset that is created from the MACRO statement). When I tried running the syntax, I received an error that there are no Min & Max values in the margins dataset created from the MACRO statement and that makes sense given that previously, we used the "out" data file to create the P1 values and the percentage values and now we are using the "_margins" dataset from the MACRO statement to create the P1 value. I initially thought that I could merge the Min & Max variables into the "_margins" dataset but realized that the "out" dataset does not have an identifiable ID variable that would allow me to merge the variables into a new dataset. Below is the syntax that I have so far and just need to figure out a way to add the Min & Max variables, and I was wondering if you had any tips on how I can do that?

proc means data=JH.Final3;
var csrpa_mlm2; 
output out=out mean=mean std=sd min=Min max=Max;
run;

data mdat;
set out;
keep csrpa_mlm2;
P1 = 0.01 / (Max - Min);
do csrpa_mlm2 = mean-P1, mean, mean+P1;
if csrpa_mlm2 = mean-P1 then csrpa_mlm2_Pct = -0.01;
else if csrpa_mlm2 = mean+P1 then csrpa_mlm2_Pct = 0.01;
else csrpa_mlm2_Pct = 0.0;
output;
end;
format csrpa_mlm2_Pct PERCENT6.2;
run;

%Margins(data=JH.Final3, response=Norepinephrine, model=cage ClinicSex marriedmidus work race_orig  cedu cHHtotalIncome EverSmokeReg Exercise20mins CNSmeds cBMI cCESD cNeuroticism  cChronCondNumb  cAnyStressWide_sum cpa_mlm2 csrpa_mlm2,
margins=csrpa_mlm2, margindata=mdat,
options=diff reverse cl)

data Margins;
set _margins;
  /* redefine P1 if it isn't in _margins */
   P1 = 0.01 / (Max - Min);
   if csrpa_mlm2 = mean-P1 then csrpa_mlm2_Pct = -0.01;
   else if csrpa_mlm2 = mean+P1 then csrpa_mlm2_Pct = 0.01;
   else csrpa_mlm2_Pct = 0.0;
format csrpa_mlm2_Pct PERCENT6.2;
run;

proc sgplot data=Margins noautolegend;
band upper=UPPER lower=LOWER x=csrpa_mlm2_Pct ;
series y=ESTIMATE x=csrpa_mlm2_Pct;
run;

 

Thank you SO much!

Rick_SAS
SAS Super FREQ

The code I gave you literally has a comment (which you copied) that says

/* redefine P1 if it isn't in _margins */

 

StatDave
SAS Super FREQ

From what you say your goal is (a graph showing the change in the response for increases in one of the model predictors) and given that your model assumes that the response is normally distributed, it seems clear that you don't need to use the Margins macro at all. You are looking for an estimate and graph of the marginal effect of the predictor on response. Since the model you specified assumes the response is normal and the continuous predictor has a linear effect on the response and doesn't interact with other predictors, the estimated marginal effect is simply the parameter estimate of the predictor. Yes, you could use the Margins macro to get that by specifying the predictor in the effect= option, but you can get it more simply when you fit the model using PROC GLM with the SOLUTION option. The same model and estimate can be obtained using PROC GENMOD and you can add the EFFECTPLOT statement to also get the plot showing the assumed linear effect of the predictor on the response.

proc genmod data=final3;
model Norepinephrine=cage ClinicSex marriedmidus work race_orig cedu cHHtotalIncome EverSmokeReg 
Exercise20mins CNSmeds cBMI cCESD cNeuroticism  cChronCondNumb  cAnyStressWide_sum cpa_mlm2 csrpa_mlm2;
effectplot fit(x=csrpa_mlm2);
run;

If you want the predictor to be on the percentile scale, then create of variable containing the percentile ranks of the predictor using PROC RANK with GROUPS=100 and use the rank variable in the MODEL and EFFECTPLOT statement instead of the original predictor.

proc rank data=final3 out=dat groups=100;
var csrpa_mlm2; ranks pct_crspa; run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 507 views
  • 1 like
  • 3 in conversation