<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Give Three Most Frequent Results for Group Variable in PROC REPORT in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Give-Three-Most-Frequent-Results-for-Group-Variable-in-PROC/m-p/663883#M198276</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 &amp;lt; 3 and sum_of_c &amp;lt;= 3 then output;
    if last.diagnose then sum_of_c = 0;
  end;
  sum_of_d = 0;
  drop sum_of:;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Sun, 21 Jun 2020 21:44:43 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2020-06-21T21:44:43Z</dc:date>
    <item>
      <title>Give Three Most Frequent Results for Group Variable in PROC REPORT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Give-Three-Most-Frequent-Results-for-Group-Variable-in-PROC/m-p/663867#M198264</link>
      <description>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.&lt;BR /&gt;&lt;BR /&gt;Sample code:&lt;BR /&gt;PROC REPORT DATA=have;&lt;BR /&gt;COLUMN dx cause;&lt;BR /&gt;DEFINE dx / group;&lt;BR /&gt;DEFINE cause / computed;&lt;BR /&gt;COMPUTE cause ...&lt;BR /&gt;(not sure what to put here to get what I want)&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;</description>
      <pubDate>Sun, 21 Jun 2020 19:04:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Give-Three-Most-Frequent-Results-for-Group-Variable-in-PROC/m-p/663867#M198264</guid>
      <dc:creator>tburus</dc:creator>
      <dc:date>2020-06-21T19:04:12Z</dc:date>
    </item>
    <item>
      <title>Re: Give Three Most Frequent Results for Group Variable in PROC REPORT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Give-Three-Most-Frequent-Results-for-Group-Variable-in-PROC/m-p/663872#M198268</link>
      <description>&lt;P&gt;&amp;nbsp;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe SQL + datastep could be a solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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_ &amp;lt;=3 then output;
  end;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jun 2020 20:27:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Give-Three-Most-Frequent-Results-for-Group-Variable-in-PROC/m-p/663872#M198268</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-06-21T20:27:37Z</dc:date>
    </item>
    <item>
      <title>Re: Give Three Most Frequent Results for Group Variable in PROC REPORT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Give-Three-Most-Frequent-Results-for-Group-Variable-in-PROC/m-p/663883#M198276</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 &amp;lt; 3 and sum_of_c &amp;lt;= 3 then output;
    if last.diagnose then sum_of_c = 0;
  end;
  sum_of_d = 0;
  drop sum_of:;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jun 2020 21:44:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Give-Three-Most-Frequent-Results-for-Group-Variable-in-PROC/m-p/663883#M198276</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-06-21T21:44:43Z</dc:date>
    </item>
    <item>
      <title>Re: Give Three Most Frequent Results for Group Variable in PROC REPORT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Give-Three-Most-Frequent-Results-for-Group-Variable-in-PROC/m-p/663885#M198278</link>
      <description>&lt;P&gt;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?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Jun 2020 21:55:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Give-Three-Most-Frequent-Results-for-Group-Variable-in-PROC/m-p/663885#M198278</guid>
      <dc:creator>tburus</dc:creator>
      <dc:date>2020-06-21T21:55:12Z</dc:date>
    </item>
  </channel>
</rss>

