BookmarkSubscribeRSS Feed
tburus
Obsidian | Level 7
I have a dataset containing whose rows are sports injuries, with each injury having a diagnoses and cause. I have cut this down to the three most frequently occurring diagnoses, and would now like to use PROC REPORT to summarize the three most frequent diagnoses along with the three most frequent causes for each.

Sample code:
PROC REPORT DATA=have;
COLUMN dx cause;
DEFINE dx / group;
DEFINE cause / computed;
COMPUTE cause ...
(not sure what to put here to get what I want)
RUN;

Is it possible to use a COMPUTE block to compute the three most frequent causes for each diagnosis and list them in order? I have hard coded a solution to this in a DATA step, but would like a programming solution that is more efficient. Thanks.
3 REPLIES 3
yabwon
Amethyst | Level 16

 Hi,

 

Maybe SQL + datastep could be a solution:

 

data  have;
  do injury  = "A", "B", "C";
    do diagnose = "1.0","2.0","3.0","4.0","5.0";
      do _N_ = 1 to input(diagnose, best32.);
        output;
      end;
    end;
  end; 
run;

proc sql; 
  create table tmp as 
  select injury, diagnose, count(diagnose) as cnt
  from have
  group by injury, diagnose
  order by injury, cnt desc
  ;
quit; 
data  want;
  do _N_ = 1 by 1 until(last.injury);
    set tmp;
    by injury;
    if _N_ <=3 then output;
  end;
run;
proc print;
run;

 

All the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Amethyst | Level 16

Hi,

 

sorry for my previous response, I didn't notice that you wanted to have "top 3" diagnoses with "top 3" causes, SQL + data step adjusted:

 

data  have;
  do injury  = "A", "B", "C";
    do d = "1.0","2.0","3.0","4.0","5.0"; drop d;
      diagnose = "diagnose" || d;      
      do _N_ = 1 to input(d, best32.);
        do c = "1.0","2.0","3.0","4.0","5.0"; drop c;
          cause = "cause" || c;
          do _iorc_ = 1 to input(c, best32.)*10 + _N_; 
            output;
          end;
        end;
      end;
    end;
  end; 
run;

proc sql; 
  create table tmp as 
  select d.injury, d.diagnose, d.cause, d.cnt_diag, c.cnt_cs
  from
  (  
    select distinct injury, diagnose, cause, count(diagnose) as cnt_diag
    from have
    group by injury, diagnose
  ) as d
  join
  (  
    select distinct injury, diagnose, cause, count(cause) as cnt_cs
    from have
    group by injury, diagnose, cause
  ) as c
  on d.injury   = c.injury   and
     d.diagnose = c.diagnose and
     d.cause    = c.cause   
  order by injury, d.cnt_diag desc, c.cnt_cs desc, d.diagnose, d.cause
  ;
quit; 
data  want;
  do _N_ = 1 by 1 until(last.injury);
    set tmp;
    by injury diagnose cause notsorted;
    sum_of_d + last.diagnose;
    sum_of_c + last.cause;
    if sum_of_d < 3 and sum_of_c <= 3 then output;
    if last.diagnose then sum_of_c = 0;
  end;
  sum_of_d = 0;
  drop sum_of:;
run;
proc print;
run;

 

All the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



tburus
Obsidian | Level 7

Thank you. I'm comfortable with getting what I want from PROC SQL. What I'm curious about is if there is a way to generate the same results using a COMPUTE step in a PROC REPORT? 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 730 views
  • 0 likes
  • 2 in conversation