<?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: Getting frequency counts for different values of a variable by id in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Getting-frequency-counts-for-different-values-of-a-variable-by/m-p/774217#M246038</link>
    <description>&lt;OL&gt;&lt;LI&gt;&amp;nbsp;What if there is a tie? If there is a tie I need to check another database&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;&lt;LI&gt;Your sample seems to assume that SSN 111111 (first row) is the same person as SSN 1111111111 rows 2 through 4).&amp;nbsp; Is that just a typo? Yes this was a typo - it should be all the same SSN for the example&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;&lt;LI&gt;Is your data already sorted by ssn? Yes&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;&lt;LI&gt;Do you want to reduce multiple records to one as in your example?&amp;nbsp; This would be a problem if your original records have variables besides SEX that vary over a set of matching SSN's. -- I need to check multiple variables that have the same issue (take the most recurring value for each variable I need). My idea was to do this in segments to make it less complicated and then create a new dataset with one line per person with all of the necessary demographic information&amp;nbsp;&lt;/LI&gt;&lt;/OL&gt;</description>
    <pubDate>Thu, 14 Oct 2021 13:42:18 GMT</pubDate>
    <dc:creator>christineford</dc:creator>
    <dc:date>2021-10-14T13:42:18Z</dc:date>
    <item>
      <title>Getting frequency counts for different values of a variable by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-frequency-counts-for-different-values-of-a-variable-by/m-p/774206#M246031</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am working with a dataset that contains demographics data that does not always align (i.e. someone can have multiple records and they do not always match). I am trying to code my program so that I can count how many times someone is reported as sex=female or sex=male and then I need to make a new variable for sex based on which outcome occurs the most.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, this is kind of like what I have&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;SSN&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Sex&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;111111&lt;/TD&gt;&lt;TD&gt;M&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;SPAN&gt;111111111&lt;/SPAN&gt;&lt;/TD&gt;&lt;TD&gt;M&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;SPAN&gt;111111111&lt;/SPAN&gt;&lt;/TD&gt;&lt;TD&gt;M&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;SPAN&gt;111111111&lt;/SPAN&gt;&lt;/TD&gt;&lt;TD&gt;F&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And then I would want something like this (could look different, but this gist):&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;SSN&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;Sex1&lt;/TD&gt;&lt;TD&gt;N1&lt;/TD&gt;&lt;TD&gt;Sex2&lt;/TD&gt;&lt;TD&gt;N2&lt;/TD&gt;&lt;TD&gt;NewSex&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;SPAN&gt;111111111&lt;/SPAN&gt;&lt;/TD&gt;&lt;TD&gt;M&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;F&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;M&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone have any ideas on how I can achieve this?&amp;nbsp;&lt;/P&gt;&lt;P&gt;TIA&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Oct 2021 13:01:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-frequency-counts-for-different-values-of-a-variable-by/m-p/774206#M246031</guid>
      <dc:creator>christineford</dc:creator>
      <dc:date>2021-10-14T13:01:36Z</dc:date>
    </item>
    <item>
      <title>Re: Getting frequency counts for different values of a variable by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-frequency-counts-for-different-values-of-a-variable-by/m-p/774213#M246034</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=sashelp.heart noprint;
table BP_Status*sex/out=have list;
run;

proc sql noprint;
select max(n) into : n
 from (select count(*) as n from have group by BP_Status);
quit;

proc summary data=have;
by BP_Status ;
output out=temp(drop=_:) idgroup(out[&amp;amp;n] (sex count)=);
run;

data want;
 set temp;
 array x{*} sex:;
 array y{*} count:;
 new=x{  whichn(max(of y{*}),of y{*})  };
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Oct 2021 13:33:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-frequency-counts-for-different-values-of-a-variable-by/m-p/774213#M246034</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-10-14T13:33:24Z</dc:date>
    </item>
    <item>
      <title>Re: Getting frequency counts for different values of a variable by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-frequency-counts-for-different-values-of-a-variable-by/m-p/774214#M246035</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/246921"&gt;@christineford&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&amp;nbsp;What if there is a tie?&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Your sample seems to assume that SSN 111111 (first row) is the same person as SSN 1111111111 rows 2 through 4).&amp;nbsp; Is that just a typo?&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Is your data already sorted by ssn?&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Do you want to reduce multiple records to one as in your example?&amp;nbsp; This would be a problem if your original records have variables besides SEX that vary over a set of matching SSN's.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming the data are sorted, and there are no other variables that vary per #4, then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until (last.ssn);
    set have;
    by ssn;
    if sex='M' then nmales=sum(nmales,1); else
    if sex='F' then nfemales=sum(nfemales,1);
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Oct 2021 13:36:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-frequency-counts-for-different-values-of-a-variable-by/m-p/774214#M246035</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-10-14T13:36:23Z</dc:date>
    </item>
    <item>
      <title>Re: Getting frequency counts for different values of a variable by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-frequency-counts-for-different-values-of-a-variable-by/m-p/774217#M246038</link>
      <description>&lt;OL&gt;&lt;LI&gt;&amp;nbsp;What if there is a tie? If there is a tie I need to check another database&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;&lt;LI&gt;Your sample seems to assume that SSN 111111 (first row) is the same person as SSN 1111111111 rows 2 through 4).&amp;nbsp; Is that just a typo? Yes this was a typo - it should be all the same SSN for the example&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;&lt;LI&gt;Is your data already sorted by ssn? Yes&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;&lt;LI&gt;Do you want to reduce multiple records to one as in your example?&amp;nbsp; This would be a problem if your original records have variables besides SEX that vary over a set of matching SSN's. -- I need to check multiple variables that have the same issue (take the most recurring value for each variable I need). My idea was to do this in segments to make it less complicated and then create a new dataset with one line per person with all of the necessary demographic information&amp;nbsp;&lt;/LI&gt;&lt;/OL&gt;</description>
      <pubDate>Thu, 14 Oct 2021 13:42:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-frequency-counts-for-different-values-of-a-variable-by/m-p/774217#M246038</guid>
      <dc:creator>christineford</dc:creator>
      <dc:date>2021-10-14T13:42:18Z</dc:date>
    </item>
    <item>
      <title>Re: Getting frequency counts for different values of a variable by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-frequency-counts-for-different-values-of-a-variable-by/m-p/774223#M246040</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input SSN $ Sex :$1.;
datalines;
111111111 M
111111111 M
111111111 M
111111111 F
222222222 F
222222222 F
333333333 M
333333333 F
;

proc sql;
create table max as
select
  ssn,
  sex
from sum
group by ssn
having count = max(count);
quit;

data
  want (drop=ct)
  check
;
do until (last.ssn);
  set max;
  by ssn;
  ct = sum(ct,1);
end;
do until (last.ssn);
  set max;
  by ssn;
  if ct &amp;gt; 1
  then output check;
  else output want;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I avoided to create a wide dataset, as you will do this for several variables; you can always create a wide report with PROC REPORT if needed.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Oct 2021 13:58:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-frequency-counts-for-different-values-of-a-variable-by/m-p/774223#M246040</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-14T13:58:34Z</dc:date>
    </item>
    <item>
      <title>Re: Getting frequency counts for different values of a variable by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-frequency-counts-for-different-values-of-a-variable-by/m-p/774230#M246045</link>
      <description>This was perfect! Thank you</description>
      <pubDate>Thu, 14 Oct 2021 14:07:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-frequency-counts-for-different-values-of-a-variable-by/m-p/774230#M246045</guid>
      <dc:creator>christineford</dc:creator>
      <dc:date>2021-10-14T14:07:15Z</dc:date>
    </item>
  </channel>
</rss>

