<?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: Most frequent observation in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/m-p/63804#M18134</link>
    <description>[pre]&lt;BR /&gt;
data work.ddates;&lt;BR /&gt;
input id death_date:date9.;&lt;BR /&gt;
format death_date date9.;&lt;BR /&gt;
cards;&lt;BR /&gt;
1 24MAR2001&lt;BR /&gt;
1 24MAR2001&lt;BR /&gt;
1 25MAR2001&lt;BR /&gt;
2 01MAY2002&lt;BR /&gt;
2 01MAY2002&lt;BR /&gt;
2 01MAY2002&lt;BR /&gt;
2 29APR2002&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
proc freq data=ddates noprint;&lt;BR /&gt;
 tables id*death_date /list nopercent  out=freq;&lt;BR /&gt;
run;&lt;BR /&gt;
proc sort data=ddates;&lt;BR /&gt;
 by id death_date;&lt;BR /&gt;
run;&lt;BR /&gt;
data want;&lt;BR /&gt;
 merge ddates freq(drop=percent);&lt;BR /&gt;
 by id death_date;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
    <pubDate>Tue, 10 May 2011 03:36:03 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2011-05-10T03:36:03Z</dc:date>
    <item>
      <title>Most frequent observation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/m-p/63799#M18129</link>
      <description>I'm trying to figure out how to calculate the most frequent date for a particular observation. Basically, I have multiple rows for one person (one ID). In each of those rows are different death_dates. How do I find the most frequent death_date for all the patients in my data set?&lt;BR /&gt;
&lt;BR /&gt;
For example:&lt;BR /&gt;
&lt;BR /&gt;
ID      DEATH_DATE&lt;BR /&gt;
1        24MAR2001&lt;BR /&gt;
1        24MAR2001&lt;BR /&gt;
1        25MAR2001&lt;BR /&gt;
2        01MAY2002&lt;BR /&gt;
2        01MAY2002&lt;BR /&gt;
2        01MAY2002&lt;BR /&gt;
2        29APR2002&lt;BR /&gt;
&lt;BR /&gt;
My procedure would result in a new variable acknowledging 24MAR2001 for ID 1 and 01MAY2002 for ID 2.&lt;BR /&gt;
&lt;BR /&gt;
Any help would be greatly appreciated! I am completely stuck!</description>
      <pubDate>Mon, 09 May 2011 21:17:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/m-p/63799#M18129</guid>
      <dc:creator>sweetpeaindeed</dc:creator>
      <dc:date>2011-05-09T21:17:45Z</dc:date>
    </item>
    <item>
      <title>Re: Most frequent observation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/m-p/63800#M18130</link>
      <description>not commenting on the metaphysical nature of your question....&lt;BR /&gt;
&lt;BR /&gt;
If you don't like creating the views expressly, you could it via inline views...just a bit harder to follow.  Also not sure what you want to happen if there is a tie.&lt;BR /&gt;
&lt;BR /&gt;
data work.ddates;&lt;BR /&gt;
input id death_date:date9.;&lt;BR /&gt;
format death_date date9.;&lt;BR /&gt;
cards;&lt;BR /&gt;
1 24MAR2001&lt;BR /&gt;
1 24MAR2001&lt;BR /&gt;
1 25MAR2001&lt;BR /&gt;
2 01MAY2002&lt;BR /&gt;
2 01MAY2002&lt;BR /&gt;
2 01MAY2002&lt;BR /&gt;
2 29APR2002&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create view work.vDeadCounts as&lt;BR /&gt;
select &lt;BR /&gt;
	id, &lt;BR /&gt;
	death_date, &lt;BR /&gt;
	count(*) as Times_Dead &lt;BR /&gt;
from work.ddates &lt;BR /&gt;
group by id, death_date&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
create view work.vMaxCounts as&lt;BR /&gt;
select id, max(Times_Dead) as Max_Times_Dead&lt;BR /&gt;
from work.vDeadcounts group by id;&lt;BR /&gt;
&lt;BR /&gt;
select distinct&lt;BR /&gt;
	t1.*&lt;BR /&gt;
from &lt;BR /&gt;
	work.vDeadCounts t1&lt;BR /&gt;
	inner join work.vMaxCounts t2&lt;BR /&gt;
		on t1.id=t2.id and t1.Times_Dead=t2.Max_Times_Dead;&lt;BR /&gt;
quit;</description>
      <pubDate>Mon, 09 May 2011 21:50:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/m-p/63800#M18130</guid>
      <dc:creator>DBailey</dc:creator>
      <dc:date>2011-05-09T21:50:58Z</dc:date>
    </item>
    <item>
      <title>Re: Most frequent observation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/m-p/63801#M18131</link>
      <description>Thank you! &lt;BR /&gt;
&lt;BR /&gt;
In regards to the metaphysical nature of my work -- I work for Veteran Affairs so mortality is a pretty common variable. Depressing, I know. &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;BR /&gt;
&lt;BR /&gt;
But thank you again; I greatly appreciate it!</description>
      <pubDate>Mon, 09 May 2011 22:06:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/m-p/63801#M18131</guid>
      <dc:creator>sweetpeaindeed</dc:creator>
      <dc:date>2011-05-09T22:06:57Z</dc:date>
    </item>
    <item>
      <title>Re: Most frequent observation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/m-p/63802#M18132</link>
      <description>Ok, one more question. I need to somehow merge this back to my data set and not just in my output. I tried to use ods output to create an excel file but I can't seem to create that. Is there any other way to make the output it's own data set that I can merge back with my original data set?&lt;BR /&gt;
&lt;BR /&gt;
Thanks sooo much!</description>
      <pubDate>Mon, 09 May 2011 22:42:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/m-p/63802#M18132</guid>
      <dc:creator>sweetpeaindeed</dc:creator>
      <dc:date>2011-05-09T22:42:35Z</dc:date>
    </item>
    <item>
      <title>Re: Most frequent observation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/m-p/63803#M18133</link>
      <description>create table work.tmp as&lt;BR /&gt;
select distinct&lt;BR /&gt;
t1.*&lt;BR /&gt;
from &lt;BR /&gt;
work.vDeadCounts t1&lt;BR /&gt;
inner join work.vMaxCounts t2&lt;BR /&gt;
on t1.id=t2.id and t1.Times_Dead=t2.Max_Times_Dead;&lt;BR /&gt;
quit; &lt;BR /&gt;
&lt;BR /&gt;
then you could merge back to your original dataset.</description>
      <pubDate>Mon, 09 May 2011 22:53:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/m-p/63803#M18133</guid>
      <dc:creator>DBailey</dc:creator>
      <dc:date>2011-05-09T22:53:20Z</dc:date>
    </item>
    <item>
      <title>Re: Most frequent observation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/m-p/63804#M18134</link>
      <description>[pre]&lt;BR /&gt;
data work.ddates;&lt;BR /&gt;
input id death_date:date9.;&lt;BR /&gt;
format death_date date9.;&lt;BR /&gt;
cards;&lt;BR /&gt;
1 24MAR2001&lt;BR /&gt;
1 24MAR2001&lt;BR /&gt;
1 25MAR2001&lt;BR /&gt;
2 01MAY2002&lt;BR /&gt;
2 01MAY2002&lt;BR /&gt;
2 01MAY2002&lt;BR /&gt;
2 29APR2002&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
proc freq data=ddates noprint;&lt;BR /&gt;
 tables id*death_date /list nopercent  out=freq;&lt;BR /&gt;
run;&lt;BR /&gt;
proc sort data=ddates;&lt;BR /&gt;
 by id death_date;&lt;BR /&gt;
run;&lt;BR /&gt;
data want;&lt;BR /&gt;
 merge ddates freq(drop=percent);&lt;BR /&gt;
 by id death_date;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Tue, 10 May 2011 03:36:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/m-p/63804#M18134</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-05-10T03:36:03Z</dc:date>
    </item>
    <item>
      <title>Re: Most frequent observation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/m-p/63805#M18135</link>
      <description>The requested result is known as the MODE. It is available from either PROC UNIVARIATE or PROC MEANS/SUMMARY.&lt;BR /&gt;
&lt;BR /&gt;
Here is some better test data, raising the issue of ties:&lt;BR /&gt;
&lt;BR /&gt;
data work.ddates;&lt;BR /&gt;
input id death_date:date9.;&lt;BR /&gt;
format death_date date9.;&lt;BR /&gt;
cards;&lt;BR /&gt;
1 24MAR2001&lt;BR /&gt;
1 24MAR2001&lt;BR /&gt;
1 25MAR2001&lt;BR /&gt;
2 01MAY2002&lt;BR /&gt;
2 01MAY2002&lt;BR /&gt;
2 29APR2002&lt;BR /&gt;
2 29APR2002&lt;BR /&gt;
2 11AUG2002&lt;BR /&gt;
2 11AUG2002&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
Let PROC SUMMARY deliver the modes:&lt;BR /&gt;
&lt;BR /&gt;
proc summary data=ddates ;&lt;BR /&gt;
class id ;&lt;BR /&gt;
types id ;&lt;BR /&gt;
var death_date ;&lt;BR /&gt;
output out=modes(drop = _type_ _freq_) mode(death_date)=mode_dd ;&lt;BR /&gt;
run ;&lt;BR /&gt;
&lt;BR /&gt;
Then merge with the original data:&lt;BR /&gt;
&lt;BR /&gt;
data want1 ;&lt;BR /&gt;
merge ddates modes ;&lt;BR /&gt;
by id ;&lt;BR /&gt;
run ;&lt;BR /&gt;
&lt;BR /&gt;
It's also possible to do it all in a single SQL statement with a lot of nested inline views:&lt;BR /&gt;
&lt;BR /&gt;
create table want2 as&lt;BR /&gt;
 select * &lt;BR /&gt;
 from (select id , min(death_date) as mode_dd format=date9.&lt;BR /&gt;
        from (select *&lt;BR /&gt;
               from (select * , count(*) as many&lt;BR /&gt;
                      from ddates&lt;BR /&gt;
                      group by id , death_date&lt;BR /&gt;
                    )&lt;BR /&gt;
               group by id&lt;BR /&gt;
               having many EQ max(many)&lt;BR /&gt;
             )&lt;BR /&gt;
        group by id&lt;BR /&gt;
        having death_date EQ min(death_date)&lt;BR /&gt;
      )&lt;BR /&gt;
      natural join &lt;BR /&gt;
      ddates&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
The innermost view counts the occurrences of the dates. The next one finds the modes. Then, ties are eliminated by arbitrarily keeping the smallest value. Finally, the outermost SELECT joins the result with the original data.</description>
      <pubDate>Wed, 11 May 2011 22:02:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/m-p/63805#M18135</guid>
      <dc:creator>Howles</dc:creator>
      <dc:date>2011-05-11T22:02:15Z</dc:date>
    </item>
    <item>
      <title>Re: Most frequent observation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/m-p/63806#M18136</link>
      <description>This is a problem that I have had to deal with so many times (most frequent defect) that I built a macro.&lt;BR /&gt;
Purpose is to identify the most frequent value of a variable for each classification variable. The output is a data table.&lt;BR /&gt;
&lt;BR /&gt;
For your example the usage would be:&lt;BR /&gt;
%freq_class(dsnin=data_table_name, var=death_date, class=id, dsnout=death_dates);&lt;BR /&gt;
&lt;BR /&gt;
%macro freq_class(dsnin=, var=, class=, dsnout=);&lt;BR /&gt;
  /* dsnin  = input data set name     */&lt;BR /&gt;
  /* var    = variable to analyze     */&lt;BR /&gt;
  /* class  = classification variable */&lt;BR /&gt;
  /* dsnout = output data set name    */&lt;BR /&gt;
	proc freq data=&amp;amp;dsnin noprint;&lt;BR /&gt;
	  tables &amp;amp;var*&amp;amp;class /norow nocol nocum nopercent out=&amp;amp;dsnout;&lt;BR /&gt;
	run;&lt;BR /&gt;
&lt;BR /&gt;
	proc sort data=&amp;amp;dsnout;&lt;BR /&gt;
	  by &amp;amp;class descending count;&lt;BR /&gt;
	run;&lt;BR /&gt;
&lt;BR /&gt;
	data &amp;amp;dsnout;&lt;BR /&gt;
	  set &amp;amp;dsnout;&lt;BR /&gt;
		by &amp;amp;class;&lt;BR /&gt;
		if first.&amp;amp;class;&lt;BR /&gt;
	run;&lt;BR /&gt;
%mend;</description>
      <pubDate>Thu, 12 May 2011 08:17:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Most-frequent-observation/m-p/63806#M18136</guid>
      <dc:creator>barheat</dc:creator>
      <dc:date>2011-05-12T08:17:28Z</dc:date>
    </item>
  </channel>
</rss>

