<?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: Retrieving data within by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-data-within-by-group/m-p/257728#M49521</link>
    <description>Hi Reinhard, Thanks for your help. it worked for me.&lt;BR /&gt;input ID (Contact_type Contact) (&amp;amp; $20.); actually worked for me and I don't know why, but I learned a second thing(:$20.);&lt;BR /&gt; &lt;BR /&gt;whichc(put(left(Contact_Type),$upcase3.), 'HOM', 'MOB', 'CEL') is quite handy. &lt;BR /&gt;&lt;BR /&gt;Thank you,</description>
    <pubDate>Sat, 19 Mar 2016 02:11:05 GMT</pubDate>
    <dc:creator>mlogan</dc:creator>
    <dc:date>2016-03-19T02:11:05Z</dc:date>
    <item>
      <title>Retrieving data within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-data-within-by-group/m-p/257497#M49449</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;I have the following table with people's contact information. for each by group I have their Home/Mobile/Email and Cellular no. I need to find the observation with contact_type = 'Home'. If the by group don't have a 'Home' contact type then the code will bring the &lt;SPAN&gt;contact_type =&amp;nbsp;&lt;/SPAN&gt;'Mobile', if Mobile is missing then it will bring the &lt;SPAN&gt;contact_type =&amp;nbsp;&lt;/SPAN&gt;'Cellular'.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;ID &amp;nbsp; &amp;nbsp; Contact_Type &amp;nbsp; Contact&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;101 &amp;nbsp;Home &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4434323454&lt;/P&gt;
&lt;P&gt;101 &amp;nbsp;Email &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; abc@gmail.com&lt;/P&gt;
&lt;P&gt;101 &amp;nbsp;Mobile &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 8767876546&lt;/P&gt;
&lt;P&gt;102 &amp;nbsp;Mobile &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 8987609878&lt;/P&gt;
&lt;P&gt;102 &amp;nbsp;Email &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ghg@gmail.com&lt;/P&gt;
&lt;P&gt;103 &amp;nbsp;Cellular &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6779998786&lt;/P&gt;
&lt;P&gt;103 &amp;nbsp;Email &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;aaa@gmail.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Output will look like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;ID &amp;nbsp; &amp;nbsp; Contact_Type &amp;nbsp; Contact&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;101 &amp;nbsp;Home &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4434323454&lt;/P&gt;
&lt;P&gt;102 &amp;nbsp;Mobile &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 8987609878&lt;/P&gt;
&lt;P&gt;103 &amp;nbsp;Cellular &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6769998786&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can someone help me please. Thanks,&lt;/P&gt;</description>
      <pubDate>Fri, 18 Mar 2016 02:44:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-data-within-by-group/m-p/257497#M49449</guid>
      <dc:creator>mlogan</dc:creator>
      <dc:date>2016-03-18T02:44:06Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving data within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-data-within-by-group/m-p/257504#M49450</link>
      <description>&lt;P&gt;So basically you have a priority.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Code me the phone type to the priority level - you can use if/then, select, format or a merge/join.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sort by id and priority and use first.id to obtain the highest priority record.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Untested but should get you started.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc format;
InValue $ phone_type_fmt
'Home' = 1
'Mobile' = 2
....
Rest of your definitions;

Proc sql;
Create table temp as
Select a.*, input(contact_type, $phone_type_fmt.) as priority
From have
Where not missing(phone_number)
Order by id, priority;
Quit;

Then use a data step with first.id to obtain the highest priority number. &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Mar 2016 03:29:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-data-within-by-group/m-p/257504#M49450</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-03-18T03:29:11Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving data within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-data-within-by-group/m-p/257518#M49453</link>
      <description>If you would like to do this in one step, just slightly modify &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;'s SQL to include a group by, and a having priority = min(priority).</description>
      <pubDate>Fri, 18 Mar 2016 05:23:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-data-within-by-group/m-p/257518#M49453</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-03-18T05:23:20Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving data within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-data-within-by-group/m-p/257576#M49463</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35631"&gt;@mlogan﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To simplify the code further, you could use the WHICHC function rather than an informat to define variable PRIORITY:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want(drop=priority) as
select *, whichc(Contact_Type, 'Home', 'Mobile', 'Cellular', 'Email') as priority
from have
where not missing(Contact)
group by id
having priority=min(priority);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Mar 2016 12:01:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-data-within-by-group/m-p/257576#M49463</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-18T12:01:03Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving data within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-data-within-by-group/m-p/257664#M49484</link>
      <description>&lt;P&gt;Thanks Reinhard, your code works, but it doesn't work when 101 by group don't have an email&amp;nbsp;and 104 has only email (see the following below).&amp;nbsp;I want Home, Mobile and Cellular as priority order. If all are missing, then I don't want that observation (104 in this case). &amp;nbsp;Can you please help further. Thanks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input ID (Contact_type Contact) ( &amp;amp; $20.);&lt;BR /&gt;cards;&lt;/P&gt;
&lt;P&gt;101 Home 4434323454&lt;BR /&gt;101 Mobile 8767876546&lt;BR /&gt;102 Mobile 8987609878&lt;BR /&gt;102 Email ghg@gmail.com&lt;BR /&gt;103 Email aaa@gmail.com&lt;BR /&gt;103 Cellular 6779998786&lt;BR /&gt;104 Email sss@gmail.com&lt;BR /&gt;;&lt;BR /&gt;RUN&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table want(drop=priority) as&lt;BR /&gt;select *, whichc(Contact_Type, 'Home', 'Mobile', 'Cellular') as priority&lt;BR /&gt;from have&lt;BR /&gt;where not missing(Contact)&lt;BR /&gt;group by id&lt;BR /&gt;having priority=min(priority);&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Folowing is my expected output:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;101 Home 4434323454&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;102 Mobile 8987609878&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;103 Cellular 6779998786&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Mar 2016 16:49:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-data-within-by-group/m-p/257664#M49484</guid>
      <dc:creator>mlogan</dc:creator>
      <dc:date>2016-03-18T16:49:20Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving data within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-data-within-by-group/m-p/257667#M49487</link>
      <description>Hi Reeza, Would you please write the first.id for procedure for me. I want the output like:&lt;BR /&gt;&lt;BR /&gt;101  Home                 4434323454&lt;BR /&gt;102  Mobile               8987609878&lt;BR /&gt;103  Cellular             6779998786&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input ID    (Contact_type Contact) ( &amp;amp; $20.);&lt;BR /&gt;cards; &lt;BR /&gt;&lt;BR /&gt;101  Home                 4434323454&lt;BR /&gt;101  Mobile               8767876546&lt;BR /&gt;102  Mobile               8987609878&lt;BR /&gt;102  Email                 ghg@gmail.com&lt;BR /&gt;103  Email                aaa@gmail.com&lt;BR /&gt;103  Cellular             6779998786&lt;BR /&gt;104  Email                sss@gmail.com&lt;BR /&gt;;&lt;BR /&gt;RUN&lt;BR /&gt;&lt;BR /&gt;Proc format;&lt;BR /&gt;InValue $ Contact_type_fmt&lt;BR /&gt;'Home' = 1&lt;BR /&gt;'Mobile' = 2&lt;BR /&gt;'Cellular' = 3&lt;BR /&gt;'Email'= 4;&lt;BR /&gt;&lt;BR /&gt;Proc sql;&lt;BR /&gt;Create table temp as&lt;BR /&gt;Select have.*, input(contact_type, $contact_type_fmt.) as priority&lt;BR /&gt;From have&lt;BR /&gt;Where not missing(Contact)&lt;BR /&gt;Order by id, priority;&lt;BR /&gt;Quit;</description>
      <pubDate>Fri, 18 Mar 2016 17:09:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-data-within-by-group/m-p/257667#M49487</guid>
      <dc:creator>mlogan</dc:creator>
      <dc:date>2016-03-18T17:09:03Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving data within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-data-within-by-group/m-p/257692#M49500</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35631"&gt;@mlogan﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can reassure you: "My" code (well, actually it's primarily&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt;'s with&amp;nbsp;valuable input from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH﻿&lt;/a&gt;) works&amp;nbsp;&lt;/P&gt;
&lt;P&gt;even if there is no email address available in a particular BY group (but only some of the other three contact types).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Okay, so you don't want to see email addresses in your output dataset. This is no problem and you already took the first step in adapting the code correspondingly: You removed 'Email' from the arguments of WHICHC.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;As a consequence, however, any existing observation with Contact_Type='Email' (or with any other value of Contact_Type not listed in the arguments of WHICHC, for that matter) will be assigned priority 0.&amp;nbsp;This zero, in turn, will always end up as the minimum priority in the BY group and hence the observation will be selected (even more than one such observation, if any).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To prevent this, we can simply amend the WHERE clause to restrict the selection to observations with non-zero (and non-missing) priority:&lt;/P&gt;
&lt;PRE&gt;where not missing(Contact) &lt;STRONG&gt;&amp;amp; calculated priority&lt;/STRONG&gt;&lt;/PRE&gt;
&lt;P&gt;With this small change you will get your expected output from the new input data you posted. To read the raw data correctly, we have to correct your suggested INPUT statement, though (I assume the "&amp;amp;" was a typo):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;input ID (Contact_type Contact) (:$20.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note that values of Contact_Type not listed in WHICHC(...) could still cause unexpected results, as described above. This includes values such as 'HOME', 'mobile', 'Cell' or ' Home' (the latter with leading blank).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To make the code more robust, we could modify the function call as follows:&lt;/P&gt;
&lt;PRE&gt;whichc(put(left(Contact_Type),$upcase3.), 'HOM', 'MOB', 'CEL')&lt;/PRE&gt;
&lt;P&gt;This would handle the abovementioned spellings of contact types as well as the ordinary ones.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, it should be mentioned that an ID with two or more observations with "top priority" (e.g. two different 'Mobile' numbers and no 'Home') would occur with &lt;EM&gt;all&lt;/EM&gt; these observations in the output dataset.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Mar 2016 20:10:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-data-within-by-group/m-p/257692#M49500</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-18T20:10:22Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving data within by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-data-within-by-group/m-p/257728#M49521</link>
      <description>Hi Reinhard, Thanks for your help. it worked for me.&lt;BR /&gt;input ID (Contact_type Contact) (&amp;amp; $20.); actually worked for me and I don't know why, but I learned a second thing(:$20.);&lt;BR /&gt; &lt;BR /&gt;whichc(put(left(Contact_Type),$upcase3.), 'HOM', 'MOB', 'CEL') is quite handy. &lt;BR /&gt;&lt;BR /&gt;Thank you,</description>
      <pubDate>Sat, 19 Mar 2016 02:11:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-data-within-by-group/m-p/257728#M49521</guid>
      <dc:creator>mlogan</dc:creator>
      <dc:date>2016-03-19T02:11:05Z</dc:date>
    </item>
  </channel>
</rss>

