<?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 Mode of nominal variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Mode-of-nominal-variables/m-p/727847#M226439</link>
    <description>&lt;P&gt;Hello, I basically need to find out how to calculate the mode / the most frequent value from a column of strings, not numbers.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the data I have: many customers for many days&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Have;
	input ID callDate :ddmmyy. employment;
	format callDate ddmmyy10.;
	datalines;
1 15/02/2021 full-time
1 17/02/2021 full-time
1 19/02/2021 full-time
2 10/02/2021 part-time
2 15/02/2021 part-time
3 17/02/2021 full-time
3 19/02/2021 full-time
3 20/02/2021 full-time
4 08/02/2021 full-time
4 10/02/2021 full-time
4 13/02/2021 full-time
4 15/02/2021 full-time
4 17/02/2021 full-time
4 19/02/2021 full-time
5 10/02/2021 unemployed
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am creating a table of statistics for each day of my table, where I count some variables and I average others. I owuld be interested in getting the mode per each day. The basic structre of this code is the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table daily_stats as
select calldate
, count( distinct id) as id_number
, *insert mode code here*

from have
group by calldate;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It would have to look like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data daily_stats;
	input callDate :ddmmyy. id_number mode;
	format callDate ddmmyy10.;
	datalines;
08/02/2021 1 full-time
13/02/2021 1 full-time
10/02/2021 3 (to be fair i don't know what the answer would be in this case)
15/02/2021 3 full-time
17/02/2021 3 full-time
19/02/2021 3 full-time
20/02/2021 1 full-time
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If you can think of an answer in PROC SQL it would be great. Thanks!&lt;/P&gt;</description>
    <pubDate>Fri, 19 Mar 2021 18:45:18 GMT</pubDate>
    <dc:creator>catkat96</dc:creator>
    <dc:date>2021-03-19T18:45:18Z</dc:date>
    <item>
      <title>Mode of nominal variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Mode-of-nominal-variables/m-p/727847#M226439</link>
      <description>&lt;P&gt;Hello, I basically need to find out how to calculate the mode / the most frequent value from a column of strings, not numbers.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the data I have: many customers for many days&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Have;
	input ID callDate :ddmmyy. employment;
	format callDate ddmmyy10.;
	datalines;
1 15/02/2021 full-time
1 17/02/2021 full-time
1 19/02/2021 full-time
2 10/02/2021 part-time
2 15/02/2021 part-time
3 17/02/2021 full-time
3 19/02/2021 full-time
3 20/02/2021 full-time
4 08/02/2021 full-time
4 10/02/2021 full-time
4 13/02/2021 full-time
4 15/02/2021 full-time
4 17/02/2021 full-time
4 19/02/2021 full-time
5 10/02/2021 unemployed
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am creating a table of statistics for each day of my table, where I count some variables and I average others. I owuld be interested in getting the mode per each day. The basic structre of this code is the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table daily_stats as
select calldate
, count( distinct id) as id_number
, *insert mode code here*

from have
group by calldate;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It would have to look like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data daily_stats;
	input callDate :ddmmyy. id_number mode;
	format callDate ddmmyy10.;
	datalines;
08/02/2021 1 full-time
13/02/2021 1 full-time
10/02/2021 3 (to be fair i don't know what the answer would be in this case)
15/02/2021 3 full-time
17/02/2021 3 full-time
19/02/2021 3 full-time
20/02/2021 1 full-time
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If you can think of an answer in PROC SQL it would be great. Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 19 Mar 2021 18:45:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Mode-of-nominal-variables/m-p/727847#M226439</guid>
      <dc:creator>catkat96</dc:creator>
      <dc:date>2021-03-19T18:45:18Z</dc:date>
    </item>
    <item>
      <title>Re: Mode of nominal variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Mode-of-nominal-variables/m-p/727882#M226454</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/314297"&gt;@catkat96&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's one suggestion:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table daily_stats as
select calldate
, count(distinct id) as id_number
, (select max(e) from
    (select e from
      (select employment as e, count(*) as c
       from have b
       where b.calldate=a.calldate
       group by e)
     having c=max(c))) as mode
from have a
group by calldate;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The innermost query creates a frequency table of the EMPLOYMENT values within the current CALLDATE value ("BY group").&lt;/LI&gt;
&lt;LI&gt;Then the&amp;nbsp;EMPLOYMENT value(s) -- plural in the case of ties -- having the maximum frequency are selected.&lt;/LI&gt;
&lt;LI&gt;From this list of tied distinct&amp;nbsp;EMPLOYMENT value(s) you can select what you like:
&lt;UL&gt;
&lt;LI&gt;my suggestion &lt;FONT face="courier new,courier"&gt;max(e)&lt;/FONT&gt;, i.e., the last value in alphabetical order&lt;/LI&gt;
&lt;LI&gt;or&amp;nbsp;&lt;FONT face="courier new,courier"&gt;min(e)&lt;/FONT&gt;, i.e., the first value in alphabetical order&lt;/LI&gt;
&lt;LI&gt;or something else (e.g., by using a HAVING clause on that nesting level).&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Fri, 19 Mar 2021 23:31:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Mode-of-nominal-variables/m-p/727882#M226454</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-19T23:31:33Z</dc:date>
    </item>
    <item>
      <title>Re: Mode of nominal variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Mode-of-nominal-variables/m-p/728729#M226738</link>
      <description>That is really great, it works fine!&lt;BR /&gt;I do wonder if you can think of a way to get many modes at the same time in a compressed manner? I've been copying this code for each mode I need to calculate but I have to do a few so it became very long, and maybe you have an idea &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Wed, 24 Mar 2021 12:23:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Mode-of-nominal-variables/m-p/728729#M226738</guid>
      <dc:creator>catkat96</dc:creator>
      <dc:date>2021-03-24T12:23:11Z</dc:date>
    </item>
    <item>
      <title>Re: Mode of nominal variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Mode-of-nominal-variables/m-p/728740#M226741</link>
      <description>&lt;P&gt;Glad to hear that it works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you have more categorical variables like &lt;FONT face="courier new,courier"&gt;employment&lt;/FONT&gt;.&amp;nbsp; Here's an example including a second categorical variable, &lt;FONT face="courier new,courier"&gt;catvar2&lt;/FONT&gt;.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Have;
 input ID callDate :ddmmyy. employment :$15. catvar2 :$1.;
 format callDate ddmmyy10.;
 datalines;
1 15/02/2021 full-time A
1 17/02/2021 full-time B
1 19/02/2021 full-time B
2 10/02/2021 part-time A
2 15/02/2021 part-time C
3 17/02/2021 full-time C
3 19/02/2021 full-time A
3 20/02/2021 full-time A
4 08/02/2021 full-time B
4 10/02/2021 full-time A
4 13/02/2021 full-time A
4 15/02/2021 full-time C
4 17/02/2021 full-time A
4 19/02/2021 full-time B
5 10/02/2021 unemployed B
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To get more compact PROC SQL code you can put the repetitive part of the SELECT statement into a macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mode(var);
(select max(x) from
  (select x from
    (select &amp;amp;var as x, count(*) as c
     from have b
     where b.calldate=a.calldate
     group by x)
   having c=max(c))) as mode_&amp;amp;var
%mend mode;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you just need a short macro call per categorical variable in the PROC SQL step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table daily_stats as
select calldate
, count(distinct id) as id_number
, %mode(employment)
, %mode(catvar2)
from have a
group by calldate;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;                                    mode_        mode_
Obs     callDate     id_number    employment    catvar2

 1     08/02/2021        1        full-time        B
 2     10/02/2021        3        unemployed       A
 3     13/02/2021        1        full-time        A
 4     15/02/2021        3        full-time        C
 5     17/02/2021        3        full-time        C
 6     19/02/2021        3        full-time        B
 7     20/02/2021        1        full-time        A&lt;/PRE&gt;
&lt;P&gt;This works for character and numeric categorical variables.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Mar 2021 13:04:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Mode-of-nominal-variables/m-p/728740#M226741</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-24T13:04:02Z</dc:date>
    </item>
  </channel>
</rss>

