<?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: Finding median of a group excluding certain ids within that group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Finding-median-of-a-group-excluding-certain-ids-within-that/m-p/911584#M359453</link>
    <description>&lt;P&gt;You apparently want what I call neighborhood median individual income based on the compliment of each family.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming you want one observation per family, and the data are grouped by neighborhood/family, then I suspect the most efficient code would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=neigh_id fam_id med_inc);
  set have;
  by neigh_id fam_id ;

  array _fam_ids {30} $1 _temporary_ ;
  array _famsize {30}    _temporary_ ;
  array _inc{30,6}       _temporary_ ;
  array _tmp{6}          _temporary_ ;

  if first.neigh_id then call missing(of _fam_ids{*},of _famsize{*},of _inc{*},_fam);

  if first.fam_id then do;
    _fam+1;
    _mbr=0;
  end;

  _mbr+1;
  _inc{_fam,_mbr}=income;

  if last.fam_id then do;
    _fam_ids{_fam}=fam_id;
    _famsize{_fam}=coalesce(dif(_n_),_n_);
  end;

  if last.neigh_id;

  _nfams=_fam;

  do _fam=1 to _nfams;
    do _mbr=1 to _famsize{_fam};
      _tmp{_mbr}=_inc{_fam,_mbr};
      _inc{_fam,_mbr}=.;
    end;

    fam_id=_fam_ids{_fam};
    med_inc=median(of _inc{*});
    output;

    do _mbr=1 to _famsize{_fam};
      _inc{_fam,_mbr}=_tmp{_mbr};
      _tmp{_mbr}=.;
    end;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 15 Jan 2024 18:02:03 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2024-01-15T18:02:03Z</dc:date>
    <item>
      <title>Finding median of a group excluding certain ids within that group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-median-of-a-group-excluding-certain-ids-within-that/m-p/911543#M359442</link>
      <description>&lt;P&gt;I am having trouble coding this problem. Thanks in advance to anyone who can help!&lt;/P&gt;&lt;P&gt;Let's say we have individual-level data. The dataset has families that live in neighborhoods so the dataset would look something like this:&lt;/P&gt;&lt;P&gt;Have:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Fam_ID&lt;/TD&gt;&lt;TD&gt;Neigh_ID&lt;/TD&gt;&lt;TD&gt;Income&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;CC&lt;/TD&gt;&lt;TD&gt;100,000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;CC&lt;/TD&gt;&lt;TD&gt;200,000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;CC&lt;/TD&gt;&lt;TD&gt;60,000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;CC&lt;/TD&gt;&lt;TD&gt;200,000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;CC&lt;/TD&gt;&lt;TD&gt;120,000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;DD&lt;/TD&gt;&lt;TD&gt;35,000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;DD&lt;/TD&gt;&lt;TD&gt;40,000&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to find the median income of a neighborhood for each family that excludes the income of those in said family.&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, family A would have a med_neighborhood_income of 120,000 since the only other family in neighborhood CC is family B and the median income in family B is 120,000. The med_neighborhood_income value for family B would be ((100,000+200,000)/2) or 150,000.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this is clear.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jan 2024 05:27:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-median-of-a-group-excluding-certain-ids-within-that/m-p/911543#M359442</guid>
      <dc:creator>SwordOrator99</dc:creator>
      <dc:date>2024-01-15T05:27:06Z</dc:date>
    </item>
    <item>
      <title>Re: Finding median of a group excluding certain ids within that group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-median-of-a-group-excluding-certain-ids-within-that/m-p/911551#M359447</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/446451"&gt;@SwordOrator99&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Fam_ID $ Neigh_ID $ Income :comma.;
cards;
1 A CC 100,000
2 A CC 200,000
3 B CC 60,000
4 B CC 200,000
5 B CC 120,000
6 C DD 35,000
7 C DD 40,000
;

proc sql;
create table want as
select a.fam_id, a.neigh_id, median(b.income) as med_neigh_income
from (select distinct fam_id, neigh_id from have) a left join have b
on a.neigh_id=b.neigh_id &amp;amp; a.fam_id ne b.fam_id
group by a.fam_id, a.neigh_id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Jan 2024 10:01:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-median-of-a-group-excluding-certain-ids-within-that/m-p/911551#M359447</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-01-15T10:01:28Z</dc:date>
    </item>
    <item>
      <title>Re: Finding median of a group excluding certain ids within that group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-median-of-a-group-excluding-certain-ids-within-that/m-p/911584#M359453</link>
      <description>&lt;P&gt;You apparently want what I call neighborhood median individual income based on the compliment of each family.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming you want one observation per family, and the data are grouped by neighborhood/family, then I suspect the most efficient code would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=neigh_id fam_id med_inc);
  set have;
  by neigh_id fam_id ;

  array _fam_ids {30} $1 _temporary_ ;
  array _famsize {30}    _temporary_ ;
  array _inc{30,6}       _temporary_ ;
  array _tmp{6}          _temporary_ ;

  if first.neigh_id then call missing(of _fam_ids{*},of _famsize{*},of _inc{*},_fam);

  if first.fam_id then do;
    _fam+1;
    _mbr=0;
  end;

  _mbr+1;
  _inc{_fam,_mbr}=income;

  if last.fam_id then do;
    _fam_ids{_fam}=fam_id;
    _famsize{_fam}=coalesce(dif(_n_),_n_);
  end;

  if last.neigh_id;

  _nfams=_fam;

  do _fam=1 to _nfams;
    do _mbr=1 to _famsize{_fam};
      _tmp{_mbr}=_inc{_fam,_mbr};
      _inc{_fam,_mbr}=.;
    end;

    fam_id=_fam_ids{_fam};
    med_inc=median(of _inc{*});
    output;

    do _mbr=1 to _famsize{_fam};
      _inc{_fam,_mbr}=_tmp{_mbr};
      _tmp{_mbr}=.;
    end;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jan 2024 18:02:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-median-of-a-group-excluding-certain-ids-within-that/m-p/911584#M359453</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-01-15T18:02:03Z</dc:date>
    </item>
    <item>
      <title>Re: Finding median of a group excluding certain ids within that group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-median-of-a-group-excluding-certain-ids-within-that/m-p/911592#M359456</link>
      <description>&lt;P&gt;Thank you, this works!&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jan 2024 00:54:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-median-of-a-group-excluding-certain-ids-within-that/m-p/911592#M359456</guid>
      <dc:creator>SwordOrator99</dc:creator>
      <dc:date>2024-01-16T00:54:25Z</dc:date>
    </item>
  </channel>
</rss>

