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

I have two variables - spline variable of height and a binary variable.  How do I write contrast to get difference in spline effects with respect to a reference at height=50 when sex=M and sex=F?  How can this be wriiten using positional syntax?

 

data Have(rename=(height=BMI));
set sashelp.class;
keep Height sex age;
run;

proc phreg data=have;
class sex(ref="F")/param=ref;
effect bmiS=spline(bmi/ basis=tpf(noint) NATURALCUBIC details knotmethod=percentiles(4));
model age=sex bmiS bmiS*sex;
store result;
run;

%LET ref=50; %LET value=65;

proc plm restore=result;
**for females;
estimate "1. &value. VS &ref. sex level 1" bmis [-1, &ref.] [1, &value.] sex*bmis [-1, 1 &ref.] [1, 1 &value.]/ e exp cl;* cov;
estimate "2. &value. VS &ref. " bmis [-1, &ref.] [1, &value.] / e exp cl;* cov;

**for males;
estimate "3. &value. VS &ref. sex level 2" sex 1 bmis [-1, &ref.] [1, &value.] sex*bmis [-1, 2 &ref.] [1, 2 &value.]/ e exp cl;* cov;
estimate "4. &value. VS &ref. sex level 2 (ref)" sex 1 bmis [-1, &ref.] [1, &value.] sex*bmis [-1, 2 &ref.] [1, 2 &value.]/ e exp cl;* cov;

run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
StatDave
SAS Super FREQ

I assume you are trying to estimate the hazard ratio of changing the original HEIGHT variable (you've called it BMI) from 50 to 65 in each SEX level. If so, you don't need ESTIMATE statements - just use the HAZARDRATIO statement. But even if that is not the case, the HAZARDRATIO statement can be used as a trick to find out appropriate contrast coefficients for hypotheses in linear and generalized linear models that use the identity link (possibly involving spline effects) as discussed in this note (see also the links to related notes).

 

For this example (I'll stick with the original HEIGHT variable), the HAZARDRATIO statement produces the hazard ratio estimates and the E option shows the coefficients you would use in ESTIMATE statements (with positional syntax) to obtain the same estimates - this is done in the first two ESTIMATE statements. 

 

The last two ESTIMATE statements use nonpositional syntax. Note that for the HTS*SEX effect, the value of the splined variable (HEIGHT) follows the value for the CLASS variable (SEX) and the value used for SEX is its ordinal level - 2 meaning the second value, "F" (since you specified REF="F" making it the second level). The E options in all the statements confirm the same coefficients were used in each case. 

proc phreg data=sashelp.class;
class sex(ref="F")/param=ref;
effect htS=spline(height/ basis=tpf(noint) NATURALCUBIC knotmethod=percentiles(4));
model age=htS|sex;
hazardratio height / units=15 at(height=50) e;
estimate '50 to 65 sex=f' htS 15 49.467243 3.750341/ e exp cl;
estimate '50 to 65 sex=m' htS 15 49.467243 3.750341 htS*sex 15 49.467243 3.750341 / e exp cl;
estimate '50 to 65 sex=f' htS [-1,50] [1,65] / e exp cl;
estimate '50 to 65 sex=m' htS [-1,50] [1,65] htS*sex [-1,2 50] [1,2 65] / e exp cl;
run;

View solution in original post

15 REPLIES 15
PamG
Quartz | Level 8

I would like to know if the above contrast is correct and how can this be written using positional syntax.  Thanks.

PamG
Quartz | Level 8

I am wondering if @Rick would be able to give any pointers to this.  Thanks.

PamG
Quartz | Level 8

Sorry.  I meant to tag @Rick_SAS 

StatDave
SAS Super FREQ

I assume you are trying to estimate the hazard ratio of changing the original HEIGHT variable (you've called it BMI) from 50 to 65 in each SEX level. If so, you don't need ESTIMATE statements - just use the HAZARDRATIO statement. But even if that is not the case, the HAZARDRATIO statement can be used as a trick to find out appropriate contrast coefficients for hypotheses in linear and generalized linear models that use the identity link (possibly involving spline effects) as discussed in this note (see also the links to related notes).

 

For this example (I'll stick with the original HEIGHT variable), the HAZARDRATIO statement produces the hazard ratio estimates and the E option shows the coefficients you would use in ESTIMATE statements (with positional syntax) to obtain the same estimates - this is done in the first two ESTIMATE statements. 

 

The last two ESTIMATE statements use nonpositional syntax. Note that for the HTS*SEX effect, the value of the splined variable (HEIGHT) follows the value for the CLASS variable (SEX) and the value used for SEX is its ordinal level - 2 meaning the second value, "F" (since you specified REF="F" making it the second level). The E options in all the statements confirm the same coefficients were used in each case. 

proc phreg data=sashelp.class;
class sex(ref="F")/param=ref;
effect htS=spline(height/ basis=tpf(noint) NATURALCUBIC knotmethod=percentiles(4));
model age=htS|sex;
hazardratio height / units=15 at(height=50) e;
estimate '50 to 65 sex=f' htS 15 49.467243 3.750341/ e exp cl;
estimate '50 to 65 sex=m' htS 15 49.467243 3.750341 htS*sex 15 49.467243 3.750341 / e exp cl;
estimate '50 to 65 sex=f' htS [-1,50] [1,65] / e exp cl;
estimate '50 to 65 sex=m' htS [-1,50] [1,65] htS*sex [-1,2 50] [1,2 65] / e exp cl;
run;
PamG
Quartz | Level 8

This works very well.  Could you please explain where the 

15 49.467243 3.750341

following numbers come from? 

StatDave
SAS Super FREQ
As I mentioned in my discussion, the E option in the HAZARDRATIO statement provides those coefficients. You can use them in the ESTIMATE statement to reproduce the result. So, see the output from the HAZARDRATIO statement.
PamG
Quartz | Level 8

Thanks!  Now I understand.  Is there a way to get these coefficients if using PROC LOGISTIC?  I tried OddsRatio statement but that does not give it.

StatDave
SAS Super FREQ
Unfortunately, the ODDSRATIO statement in PROC LOGISTIC doesn't provide an E option to display those coefficients
PamG
Quartz | Level 8

How does the positional syntx change if sex is coded as numeric 1(Male) and 0(female)?  Thanks a lot for your help.  Your responses have taught me more than all the readings I have done for so many days.  Really appreciate it!!!

StatDave
SAS Super FREQ

Nothing changes for either the positional or the nonpositional syntax as long as you retain the Female level (whatever its coding) as the reference level.

PamG
Quartz | Level 8

Thank you so much for taking the time to answer all my queries!  I have learned tremendously.

PamG
Quartz | Level 8

I am trying to see if I got a handle on how positional syntax works and I get conflicting answers when I use sex coded as a character M/F vs sex coded as a character variable 0/1.  The positional numbers seem to be different based on how sex is coded using numbers or alphabets for character variable.  I seem to be missing somthing.  Here is a sample code.  The only change is I have recoded sex as character var with '0'/'1' and the results don't match.

**SEX AS CHARACTER VAR(M,F);
proc phreg data=sashelp.class;
class sex(ref="F")/param=ref;
model age=sex;
estimate 'sex=M' sex [1,2]/ e exp cl;
estimate 'sex=F' sex [1,1] / e exp cl;
estimate 'M Vs F' sex [1,2][-1,1]  / e exp cl;
run;

**SEX AS CHARACTER VAR('1','2');
data have;set sashelp.class; IF sex='M' THEN sex='1'; ELSE sex='2';run;

proc phreg data=have;
class sex(ref="2")/param=ref;
model age=sex;*htS|sex;
estimate 'sex=1(M)' sex [1,2] / e exp cl;
estimate 'sex=2(F)' sex [1,1]/ e exp cl;
estimate 'M Vs F' sex [1,2][-1,1]  / e exp cl;
run;
PamG
Quartz | Level 8

How does one find what the levels of a categorical variable are for the nonpositional syntax?

StatDave
SAS Super FREQ
With the nonpositional syntax, both the reference level and the sorted order of the CLASS variable levels matter. Note that the order of the CLASS levels can be changed with the ORDER= option in the CLASS statement. So, syntax like sex [1,2] says that you want to multiple 1 by the parameter associated with the 2nd ordered level of SEX. If the default ORDER= applies (as it does here), the sorted order of SEX is F then M (or 1 then 2 with your recoding). So the 2nd ordered level of SEX is M (or 2). Since the reference level for SEX is F (or 2), it has a parameter of 0 (the parameter for M (or 1) is the estimated parameter). If you change the ordering of the levels or the reference level, then what gets multiplied by 1 might change.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 15 replies
  • 1069 views
  • 7 likes
  • 2 in conversation