BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bal23
Lapis Lazuli | Level 10

Thank you. I have spent some time to read it. I understand the theory, but it lacks sample code. It is a little hard for me to put everything together.

Would it be possible  for you to put my code and your suggested line to add together as a sample code for me to work on?

 

Reeza
Super User

There's a fully worked example, with fake data I made and code that generates resulsts as indicated. I'm not sure what more sample code could be provided.

 

 

Bal23
Lapis Lazuli | Level 10

*Create sample data;
data have;
input mygroup $ subgroup $ b d;
cards;
se A 371130 11243
se N 214144 2214
se M 181841 5820
se F 168065 3288
ex M 779199 17987
ex F 155981 4578
ge s20 603224 14766
ge S25 263337 5918
ge o25 68607 1881
ac W 689531 17304
ac B 159304 3714
ac Ot 86345 1547
ed BH 1766 76
ed HS 806844 19491
ed Co 74217 2014
ed B 52171 979
q 99 66736 1373
q 92 376550 8726
q 64 259887 6356
q 49 221302 6009
q 29 2635 83
s fq 811106 19056
s tq 37385 1178
s pq 86689 2331
;

run;

data want;
set have;
by mygroup;
*tell sas to keep the denom across the rows;
retain denom;

*create the denominator value;
if _n_=1 then do;
rr=1;
denom=d/b;
end;

*calculate relative risk;
else rr=(d/b)/denom;

*calculate percent and 95%CI;
pct=d/b;
J=SQRT((((B-D)/D)/B)+(((B-D)/D)/B));
Lcl=EXP(log(pct)-(1.96*(J)));
Ucl=EXP(log(pct)-(1.96*(J)));
/*proc sort; by mygroup;*/

*format variables for appearance;
format pct percent8.1 rr 8.2;

run;

*print results;
proc print data=want;
run;

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Error message:

ERROR: BY variables are not properly sorted on data set WORK.HAVE.

I did add the by statment for the second time, please see the line / / , but the error message still exists

 

Reeza
Super User

Add the NOTSORTED option to your BY statement. However, if your data is not actually sorted correctly, this will provide incorrect answers.

 

 

by mygroup NOTSORTED;

Also, look back at my sample code and the first IF, does it match yours?

Bal23
Lapis Lazuli | Level 10

Thank you. That is my problem i did not use your code if first.mygroup

after I fixed it, notsorted option seems not necessary.

I need one more option, for each first one, with RR=1, at any subgroup, 95%CI needed to be calculated, that is to leave it as blank,

would you please let me know how to let sas do it?

Thank you again

Reeza
Super User

If first.mygroup identifies that record.  Try adding the condition in an IF statement, make sure it goes in the appropriate location (ie after the calculation). 

 

Give it a try and if you have difficulties post the error with your code. 

 

Otherwise I think your original question is now answered and please mark this question answered.

Bal23
Lapis Lazuli | Level 10

sorry  I do not understand your first sentence; therefore, I do not know how to do

Bal23
Lapis Lazuli | Level 10

I meant, the first one, with RR=1, no need to calculate 95%CI. Would you please let me know how to let SAS do this,, put  a marker as "-" for the first one of each subgroup with RR=1?

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 37 replies
  • 4980 views
  • 4 likes
  • 3 in conversation