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

Hi all,

 

I have the following data  

 

 

term event count drug
Anthralgia yes 1 A
Anthralgia no 5 A
Nausea yes 1 A
Nausea no 5 A
Anorexia yes 1 A
Anorexia no 5 A
Cough yes 1 B
Cough no 1 B
Malaise yes 1 B
Malaise no 1 B

 

I tried the following code to get relative risk and confidence interval

 

ods output relativerisks=rel;
 
proc freq data=test order=data;
        weight count;
        tables drug*event / relrisk ;
       by   term ;
        run;
 
ods output close;

 

I am getting the following message in log and ods output is not being created

 

No statistics are computed for  DRUG* EVENT because  DRUG has fewer than 2 nonmissing levels.

May I know how to get the code work or any other proc to derive relative risk & CL for above data.

 

Appreciate response!

 

Regards

Sri

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data test;
infile cards delimiter='|';
input term :$10.  event :$3.	count drug $;
cards;
Anthralgia|yes|1|A
Anthralgia|no|5|A
Nausea|yes|1|A
Nausea|no|5|A
Anorexia|yes|1|A
Anorexia|no|5|A
Cough|yes|1|B
Cough|no|1|B
Malaise|yes|1|A
Malaise|no|1|A
;


%macro relrisk(dsn=);
proc sql;
create table want as
select * from &dsn 
 group by term
  having count(distinct catx(' ',event,drug))=4 and sum(count=0)=0 ;
quit;

%let dsid=%sysfunc(open(want));
%let nlobs=%sysfunc(attrn(&dsid,nlobs));
%let dsid=%sysfunc(close(&dsid.));
%if &nlobs. %then %do;
  ods output relativerisks=rel;
proc freq data=want order=data;
        weight count;
        tables drug*event / relrisk ;
       by   term ;
        run;
%end;
%mend;

%relrisk(dsn=test)

View solution in original post

12 REPLIES 12
PaigeMiller
Diamond | Level 26

From now on, please present data as working SAS data step code, like this (and not in any other format)

 

data test;
infile cards delimiter='|';
input term :$10.  event :$3.	count drug $;
cards;
Anthralgia|yes|1|A
Anthralgia|no|5|A
Nausea|yes|1|A
Nausea|no|5|A
Anorexia|yes|1|A
Anorexia|no|5|A
Cough|yes|1|B
Cough|no|1|B
Malaise|yes|1|B
Malaise|no|1|B
;


From now on, please present the ENTIRE log, not just one line, and place the log in a code box by clicking on the </> icon:

 

 
 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 NOTE: ODS statements in the SAS Studio environment may disable some output features.
 69         
 70         data test;
 71         infile cards delimiter='|';
 72         input term :$10.  event :$3.count drug $;
 73         cards;
 
 NOTE: The data set WORK.TEST has 10 observations and 4 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       user cpu time       0.00 seconds
       system cpu time     0.00 seconds
       memory              674.65k
       OS Memory           21412.00k
       Timestamp           09/24/2023 02:58:41 PM
       Step Count                        104  Switch Count  2
       Page Faults                       0
       Page Reclaims                     128
       Page Swaps                        0
       Voluntary Context Switches        13
       Involuntary Context Switches      0
       Block Input Operations            0
       Block Output Operations           264
       
 
 84         ;
 85         
 86         ods output relativerisks=rel;
 87         
 88         proc freq data=test order=data;
 89                 weight count;
 90                 tables drug*event / relrisk ;
 91                by   term ;
 92                 run;
 
 NOTE: No statistics are computed for drug * event because drug has fewer than 2 nonmissing levels.
 NOTE: The above message was for the following BY group:
       term=Anthralgia
 ERROR: Data set WORK.TEST is not sorted in ascending sequence. The current BY group has term = Nausea and the next BY group has 
        term = Anorexia.
 NOTE: The SAS System stopped processing this step because of errors.
 WARNING: Output 'relativerisks' was not created.  Make sure that the output object name, label, or path is spelled correctly.  
          Also, verify that the appropriate procedure options are used to produce the requested output object.  For example, verify 
          that the NOPRINT option is not used.
 NOTE: There were 5 observations read from the data set WORK.TEST.
 NOTE: PROCEDURE FREQ used (Total process time):
       real time           0.01 seconds
       user cpu time       0.02 seconds
       system cpu time     0.01 seconds
       memory              2233.78k
       OS Memory           22444.00k
       Timestamp           09/24/2023 02:58:41 PM
       Step Count                        105  Switch Count  4
       Page Faults                       0
       Page Reclaims                     308
       Page Swaps                        0
       Voluntary Context Switches        22
       Involuntary Context Switches      0
       Block Input Operations            0
       Block Output Operations           536
       
 93         
 94         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 104        

Once we see the ENTIRE log as shown above, it is clear you did not present to us the major ERROR, which is highlighted in red.

 

By specifying this in the code:

 

       by   term ;

 

the error is obvious now ... SAS can't do this by TERM because the data is not sorted by TERM. In addition, as soon as you limit the analysis to be done BY TERM, there is indeed only a single level DRUG in each value of TERM.

--
Paige Miller
sri1
Obsidian | Level 7

I have provided sample data , the data was sorted before deriving relative risk and I have provided the major note in the log which says 

No statistics are computed for drug * event because drug has fewer than 2 nonmissing levels.

 

PaigeMiller
Diamond | Level 26

@sri1 wrote:

I have provided sample data , the data was sorted before deriving relative risk and I have provided the major note in the log which says 

No statistics are computed for drug * event because drug has fewer than 2 nonmissing levels.

 


 

Sample data needs to be provided in the format I have shown, not in the format you used. Even if you sorted the data (which you did not state in your original message), the explanation of that error is stated in my message.

--
Paige Miller
sri1
Obsidian | Level 7

I hope this group to provide a solution rather than commenting on how to present data .I tried to explain sorting error wasn't an issue to not get the code work as sorting was done  in real time.

PaigeMiller
Diamond | Level 26

And I explained to you why you are getting the error. I said

 

as soon as you limit the analysis to be done BY TERM, there is indeed only a single level DRUG in each value of TERM.

 

Thus, the error message from SAS is correct, and so no relative risk can be computed.

 

By the way, we're trying to help you, but you need to help us too. From now, please present data in the best possible format for us. Thanks.

--
Paige Miller
sri1
Obsidian | Level 7

Sure! Thanks for your response.

Ksharp
Super User
You have to remove "by term ;" of PROC FREQ.
Since it is only calculated RELRISK for 2x2 table,
but for term='Cough' would not form 2x2 table.
sri1
Obsidian | Level 7

Thanks for your response. I need the output "by term". Any alternative other than proc freq?

PaigeMiller
Diamond | Level 26

@sri1 wrote:

Thanks for your response. I need the output "by term". Any alternative other than proc freq?


With by term; in the code, you cannot compute relative risk, that is what SAS is telling you. There is only one level of  DRUG when you limit the analysis with the BY statement. In this case, relative risk is not mathematically defined, and so I doubt any PROC in SAS can do this.

--
Paige Miller
Ksharp
Super User
You gotta know
for 2x2 table :

a1 a2
b1 b2

for the first column
RELRISK= (a1/(a1+a2)) / (b1/(b1+b2));

for the second column
RELRISK= (a2/(a1+a2)) / (b2/(b1+b2)) ;

if you don't have b1 and b2 (a.k.a b1=0,b2=0), how could you get RELRISK?
sri1
Obsidian | Level 7

Thanks for your response.For the above data as relrisk can't be computed , can you please let me know how can I define in a macro not to run above procedure as there is no enough data  and don't get warning in log that "Output 'relativerisks' was not created" and end the macro.

Ksharp
Super User
data test;
infile cards delimiter='|';
input term :$10.  event :$3.	count drug $;
cards;
Anthralgia|yes|1|A
Anthralgia|no|5|A
Nausea|yes|1|A
Nausea|no|5|A
Anorexia|yes|1|A
Anorexia|no|5|A
Cough|yes|1|B
Cough|no|1|B
Malaise|yes|1|A
Malaise|no|1|A
;


%macro relrisk(dsn=);
proc sql;
create table want as
select * from &dsn 
 group by term
  having count(distinct catx(' ',event,drug))=4 and sum(count=0)=0 ;
quit;

%let dsid=%sysfunc(open(want));
%let nlobs=%sysfunc(attrn(&dsid,nlobs));
%let dsid=%sysfunc(close(&dsid.));
%if &nlobs. %then %do;
  ods output relativerisks=rel;
proc freq data=want order=data;
        weight count;
        tables drug*event / relrisk ;
       by   term ;
        run;
%end;
%mend;

%relrisk(dsn=test)

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
  • 12 replies
  • 2338 views
  • 1 like
  • 3 in conversation