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;
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;
I would like to know if the above contrast is correct and how can this be written using positional syntax. Thanks.
I am wondering if @Rick would be able to give any pointers to this. Thanks.
Sorry. I meant to tag @Rick_SAS
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;
This works very well. Could you please explain where the
15 49.467243 3.750341
following numbers come from?
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.
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!!!
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.
Thank you so much for taking the time to answer all my queries! I have learned tremendously.
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;
How does one find what the levels of a categorical variable are for the nonpositional syntax?
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.
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.
Ready to level-up your skills? Choose your own adventure.