<?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: How do I find the most commonly occurring sex, across different events, for unique personid? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-the-most-commonly-occurring-sex-across-different/m-p/563987#M158161</link>
    <description>&lt;P&gt;please try the proc sql&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as select count(personid) as count, eventid,personid,sex,year from have 
group by eventid,personid,sex,year
order by eventid,personid,sex,year;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 06 Jun 2019 08:00:12 GMT</pubDate>
    <dc:creator>Jagadishkatam</dc:creator>
    <dc:date>2019-06-06T08:00:12Z</dc:date>
    <item>
      <title>How do I find the most commonly occurring sex, across different events, for unique personid?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-the-most-commonly-occurring-sex-across-different/m-p/563975#M158154</link>
      <description>&lt;P&gt;Hi there,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have data from 2005 to 2016. The counting unit of the data is event_id, each row is one event id and has demographic data associated with that event. One of the variables in the dataset is a unique person identifier we shall call it personid, a person may have many different events. My boss would like me to count unique people by sex. However, as the data is of interesting quality you might have say 7 different events for one personid and they might say FEMALE 5 times then MALE once and unknown once. I would like to, over the entire collection (forgetting about year), attach the most commonly occurring sex to the unique person identifier, then I guess join the associated event ids to the person identifier for each year. I will make another data set for unique people at this point that I can pull from.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since there are like over 400,000 records/events, how do I go about finding the most occurring value for sex and attach it to the person identifier? (the more efficient the better). Using SAS 9.4&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you. An example of what my data looks like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;event id&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PERSONID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sex&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;other demographic&amp;nbsp; &amp;nbsp; year&lt;/P&gt;&lt;P&gt;86590&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 767898&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FEMALE&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; xxx&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2005&lt;/P&gt;&lt;P&gt;86591&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;767898&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FEMALE&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; xxx&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2005&lt;/P&gt;&lt;P&gt;86592&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 767898&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; MALE&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; xxx&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2007&lt;/P&gt;&lt;P&gt;86593&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 767898&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; UNKNOWN&amp;nbsp; &amp;nbsp; &amp;nbsp;xxx&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2008&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for your help!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2019 06:35:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-the-most-commonly-occurring-sex-across-different/m-p/563975#M158154</guid>
      <dc:creator>Annabeth</dc:creator>
      <dc:date>2019-06-06T06:35:37Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find the most commonly occurring sex, across different events, for unique personid?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-the-most-commonly-occurring-sex-across-different/m-p/563987#M158161</link>
      <description>&lt;P&gt;please try the proc sql&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as select count(personid) as count, eventid,personid,sex,year from have 
group by eventid,personid,sex,year
order by eventid,personid,sex,year;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Jun 2019 08:00:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-the-most-commonly-occurring-sex-across-different/m-p/563987#M158161</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-06-06T08:00:12Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find the most commonly occurring sex, across different events, for unique personid?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-the-most-commonly-occurring-sex-across-different/m-p/563995#M158163</link>
      <description>&lt;P&gt;You need to create an intermediary table for use in a subquery (I tried to pack it into a single subquery, but that would either fail or become quite unwieldy)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input event_id $ personid $ sex $ other_dem $ year;
datalines;
86590 767898 FEMALE  xxx 2005
86591 767898 FEMALE  xxx 2005
86592 767898 MALE    xxx 2007
86593 767898 UNKNOWN xxx 2008
;

proc sql;
create table sex as
select personid, sex, count(*) as count from have
group by personid, sex
;
create table want as
select
  a.event_id,
  a.personid,
  (
    select b.sex
    from sex b
    where b.personid = a.personid
    group by personid
    having count = max(count)
  ) as sex,
  a.other_dem,
  a.year
from have a
;
quit; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note how I presented your example data in readily usable form by putting it into a data step with datalines. Please do so yourself in the future, as it makes helping you a lot easier for us.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also do it in a data step by using double do loops:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by personid sex;
run;

data want;
maxcount = 0;
do until (last.personid);
  count = 0;
  do until (last.sex);
    set have;
    by personid sex;
    count + 1;
  end;
  if count &amp;gt; maxcount
  then do;
    maxcount = count;
    newsex = sex;
  end;
end;
do until (last.personid);
  set have;
  by personid;
  sex = newsex;
  output;
end;
drop count maxcount newsex;
run;

proc sort data=want;
by event_id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Jun 2019 08:51:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-the-most-commonly-occurring-sex-across-different/m-p/563995#M158163</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-06T08:51:16Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find the most commonly occurring sex, across different events, for unique personid?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-the-most-commonly-occurring-sex-across-different/m-p/563998#M158164</link>
      <description>&lt;P&gt;What should happen in case of ties?&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp; I believe your SQL falls over if there are ties.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2019 09:24:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-the-most-commonly-occurring-sex-across-different/m-p/563998#M158164</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-06-06T09:24:54Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find the most commonly occurring sex, across different events, for unique personid?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-the-most-commonly-occurring-sex-across-different/m-p/564022#M158177</link>
      <description>&lt;P&gt;Yep. I'm much more confident with the data step for this, as the preceding sort may be used to set a preference in case of ties.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2019 10:08:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-the-most-commonly-occurring-sex-across-different/m-p/564022#M158177</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-06T10:08:37Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find the most commonly occurring sex, across different events, for unique personid?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-the-most-commonly-occurring-sex-across-different/m-p/565091#M158582</link>
      <description>Hi there,&lt;BR /&gt;&lt;BR /&gt;Thank you for all of your help! I have been on holiday so only just been able to use and the do loop is working well!&lt;BR /&gt;When you say that you could set a preference in the preceding sort in the case of ties - how would I do that? Sorry I am such a beginner! For instance, when there is FEMALE and UNKNOWN or MALE and UNKNOWN that are ties I would like it to not be unknown and if there is FEMALE or MALE, I would like it to be deleted!&lt;BR /&gt;&lt;BR /&gt;How would you do that?&lt;BR /&gt;&lt;BR /&gt;Thank you in advance.</description>
      <pubDate>Tue, 11 Jun 2019 00:49:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-the-most-commonly-occurring-sex-across-different/m-p/565091#M158582</guid>
      <dc:creator>Annabeth</dc:creator>
      <dc:date>2019-06-11T00:49:36Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find the most commonly occurring sex, across different events, for unique personid?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-the-most-commonly-occurring-sex-across-different/m-p/565110#M158593</link>
      <description>&lt;P&gt;With your original data, UNKNOWN filters itself out if it's part of a tie. Keep in mind that it is the last in the alphabetic sorting sequence (F-M-U), and if its count is not larger than any other count, F or M will take precedence.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For a tie of F and M, you can check that in and after the first do loop in the data step. Keep separate counters for F and M, and if they are equal after the do loop, set to unknown or missing.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2019 05:29:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-the-most-commonly-occurring-sex-across-different/m-p/565110#M158593</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-11T05:29:41Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find the most commonly occurring sex, across different events, for unique personid?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-the-most-commonly-occurring-sex-across-different/m-p/565122#M158603</link>
      <description>&lt;P&gt;Amazing. Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2019 06:50:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-the-most-commonly-occurring-sex-across-different/m-p/565122#M158603</guid>
      <dc:creator>Annabeth</dc:creator>
      <dc:date>2019-06-11T06:50:16Z</dc:date>
    </item>
  </channel>
</rss>

