<?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: Perform hierarchical if condition within group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Perform-hierarchical-if-condition-within-group/m-p/905195#M357535</link>
    <description>&lt;P&gt;Except for "Administrative Agent" the LenderRole terms in your spreadsheet don't match the terms in your narrative.&lt;/P&gt;
&lt;P&gt;To make below code work you need to amend the terms in the informat so that they match your data (terms in the format must be uppercase, data can be mixed case).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dsd truncover;
  input Facilityid Lender:$40. LenderRole:$20. (LeadArrangerCredit AgentCredit) (:$3.);
  datalines;
1,Deutsche Bank,Arranger,Yes,No
1,Goldman,Admin Agent,No,No
1,JP Morgan,Participant,No,No
1,Citi,Administrative Agent,No,No
2,Morgan Stanley,Manadated Lead arranger,No,No
2,Wells Fargo,Bookrunner,No,No
2,UBS,Bookrunner,No,No
2,Credit Suisse,Bookrunner,Yes,No
3,Deutsche Bank,Administrative Agent,No,Yes
4,JP Morgan,Bookrunner,No,No
4,Goldman,Bookrunner,No,No
4,Citi,Bookrunner,No,No
4,UBS,Bookrunner,No,Yes
;

proc format;
  invalue lead_arr_hier (upcase)
  'ADMINISTRATIVE AGENT'  = 1
  'LEADARRANGERCREDIT'    = 2
  'AGENTCREDIT'           = 3
  other = 999
  ;
quit;

data inter;
  set have;
  lead_order=input(LenderRole,lead_arr_hier.);
run;

proc sort data=inter;
  by Facilityid lead_order;
run;
    
data want;
  set inter;
  by Facilityid lead_order;
  if first.Facilityid then
    do;
      if last.Facilityid or lead_order ne 999 then lead_arranger = 1;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 29 Nov 2023 16:15:00 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2023-11-29T16:15:00Z</dc:date>
    <item>
      <title>Perform hierarchical if condition within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Perform-hierarchical-if-condition-within-group/m-p/905167#M357526</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to perform a&amp;nbsp; hierarchical if condition within a group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data structure looks like the following:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;EM&gt;&lt;STRONG&gt;Facilityid&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;&lt;TD&gt;&lt;EM&gt;&lt;STRONG&gt;Lender&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;&lt;TD&gt;&lt;EM&gt;&lt;STRONG&gt;LenderRole&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;&lt;TD&gt;&lt;EM&gt;&lt;STRONG&gt;LeadArrangerCredit&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;&lt;TD&gt;&lt;EM&gt;&lt;STRONG&gt;AgentCredit&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Deutsche Bank&lt;/TD&gt;&lt;TD&gt;Arranger&lt;/TD&gt;&lt;TD&gt;Yes&lt;/TD&gt;&lt;TD&gt;No&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Goldman&lt;/TD&gt;&lt;TD&gt;Admin Agent&lt;/TD&gt;&lt;T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t;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;JP Morgan&lt;/TD&gt;&lt;TD&gt;Bookrunner&lt;/TD&gt;&lt;TD&gt;No&lt;/TD&gt;&lt;TD&gt;No&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;Goldman&lt;/TD&gt;&lt;TD&gt;Bookrunner&lt;/TD&gt;&lt;TD&gt;No&lt;/TD&gt;&lt;TD&gt;No&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;Citi&lt;/TD&gt;&lt;TD&gt;Bookrunner&lt;/TD&gt;&lt;TD&gt;No&lt;/TD&gt;&lt;TD&gt;No&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;UBS&lt;/TD&gt;&lt;TD&gt;Bookrunner&lt;/TD&gt;&lt;TD&gt;No&lt;/TD&gt;&lt;TD&gt;Yes&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to code a dummy variable (&lt;EM&gt;lead_arranger&lt;/EM&gt;) per &lt;EM&gt;Facilityid&lt;/EM&gt; group. Thereby, the following restrictions apply:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- If any &lt;EM&gt;Lender&lt;/EM&gt; has the role of "Administrative Agent" it is deemed as the lead arranger (i.e. &lt;EM&gt;lead_arranger&lt;/EM&gt; = 1)&lt;/P&gt;&lt;P&gt;- If there is no "Administrative Agent", the &lt;EM&gt;Lender&lt;/EM&gt; which is the &lt;EM&gt;LeadArrangerCredit&lt;/EM&gt; is the lead arranger&lt;/P&gt;&lt;P&gt;- If there is no "Administrative Agent" and no&amp;nbsp;&lt;EM&gt;LeadArrangerCredit&lt;/EM&gt;, the &lt;EM&gt;Lender&lt;/EM&gt; with the &lt;EM&gt;AgentCredit&lt;/EM&gt; is the lead arranger&lt;/P&gt;&lt;P&gt;- For loans with only one lender (i.e. only one observation per facilityid group), this lender is automatically the lead arranger&lt;/P&gt;&lt;P&gt;- If none of the above is true, there is no lead arranger&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much!&lt;/P&gt;</description>
      <pubDate>Wed, 29 Nov 2023 14:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Perform-hierarchical-if-condition-within-group/m-p/905167#M357526</guid>
      <dc:creator>edeede</dc:creator>
      <dc:date>2023-11-29T14:38:39Z</dc:date>
    </item>
    <item>
      <title>Re: Perform hierarchical if condition within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Perform-hierarchical-if-condition-within-group/m-p/905195#M357535</link>
      <description>&lt;P&gt;Except for "Administrative Agent" the LenderRole terms in your spreadsheet don't match the terms in your narrative.&lt;/P&gt;
&lt;P&gt;To make below code work you need to amend the terms in the informat so that they match your data (terms in the format must be uppercase, data can be mixed case).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dsd truncover;
  input Facilityid Lender:$40. LenderRole:$20. (LeadArrangerCredit AgentCredit) (:$3.);
  datalines;
1,Deutsche Bank,Arranger,Yes,No
1,Goldman,Admin Agent,No,No
1,JP Morgan,Participant,No,No
1,Citi,Administrative Agent,No,No
2,Morgan Stanley,Manadated Lead arranger,No,No
2,Wells Fargo,Bookrunner,No,No
2,UBS,Bookrunner,No,No
2,Credit Suisse,Bookrunner,Yes,No
3,Deutsche Bank,Administrative Agent,No,Yes
4,JP Morgan,Bookrunner,No,No
4,Goldman,Bookrunner,No,No
4,Citi,Bookrunner,No,No
4,UBS,Bookrunner,No,Yes
;

proc format;
  invalue lead_arr_hier (upcase)
  'ADMINISTRATIVE AGENT'  = 1
  'LEADARRANGERCREDIT'    = 2
  'AGENTCREDIT'           = 3
  other = 999
  ;
quit;

data inter;
  set have;
  lead_order=input(LenderRole,lead_arr_hier.);
run;

proc sort data=inter;
  by Facilityid lead_order;
run;
    
data want;
  set inter;
  by Facilityid lead_order;
  if first.Facilityid then
    do;
      if last.Facilityid or lead_order ne 999 then lead_arranger = 1;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Nov 2023 16:15:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Perform-hierarchical-if-condition-within-group/m-p/905195#M357535</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-29T16:15:00Z</dc:date>
    </item>
    <item>
      <title>Re: Perform hierarchical if condition within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Perform-hierarchical-if-condition-within-group/m-p/905198#M357536</link>
      <description>Thanks, Patrick. I think the spreadsheet fits the narrative. For facilityid 2 Credit Suisse would be lead_arranger, and for facilityid 4 it would be UBS, that is not refelcted in the code.</description>
      <pubDate>Wed, 29 Nov 2023 16:23:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Perform-hierarchical-if-condition-within-group/m-p/905198#M357536</guid>
      <dc:creator>edeede</dc:creator>
      <dc:date>2023-11-29T16:23:55Z</dc:date>
    </item>
    <item>
      <title>Re: Perform hierarchical if condition within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Perform-hierarchical-if-condition-within-group/m-p/905206#M357539</link>
      <description>&lt;P&gt;I had a similar approach as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt; with ranking but ended up using a nested SQL instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   select have.*, 
      case 
         when lenderscore is not null then 'Yes'
         when No = 1 then 'Yes' 
      else 'No' 
   end as Lead_Arranger
   from have 
   left join
   (select facilityid, 
      lender,
      count(*) as No,
      case
         when lenderRole = "Administrative Agent" then 3
         when LeadArrangerCredit = 'Yes' then 2
         when AgentCredit = 'Yes' then 1
         else 0 end as lenderScore
      from have
      group by facilityid
      having calculated lenderScore = max(calculated lenderScore)) as score
   on have.facilityid = score.facilityid and 
      have.lender = score.lender
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Edit&lt;/STRONG&gt;: this code works if a lender only can appear once per FacilityId &lt;/P&gt;</description>
      <pubDate>Wed, 29 Nov 2023 16:40:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Perform-hierarchical-if-condition-within-group/m-p/905206#M357539</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2023-11-29T16:40:35Z</dc:date>
    </item>
    <item>
      <title>Re: Perform hierarchical if condition within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Perform-hierarchical-if-condition-within-group/m-p/905214#M357544</link>
      <description>&lt;P&gt;Assign a RANK to each observation.&amp;nbsp; Sort by the RANK.&amp;nbsp; And set the flag true only on the first observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile cards dsd dlm='|' truncover;
  input Facilityid Lender :$20. LenderRole :$20. LeadArrangerCredit :$3. AgentCredit :$3.;
cards;
1|Deutsche Bank|Arranger|Yes|No
1|Goldman|Admin Agent|No|No
1|JP Morgan|Participant|No|No
1|Citi|Administrative Agent|No|No
2|Morgan Stanley|Manadated Lead arranger|No|No
2|Wells Fargo|Bookrunner|No|No
2|UBS|Bookrunner|No|No
2|Credit Suisse|Bookrunner|Yes|No
3|Deutsche Bank|Administrative Agent|No|Yes
4|JP Morgan|Bookrunner|No|No
4|Goldman|Bookrunner|No|No
4|Citi|Bookrunner|No|No
4|UBS|Bookrunner|No|Yes
;

data rank;
 set have;
 if lenderrole='Administrative Agent' then rank=1;
 else if LeadArrangerCredit='Yes' then rank=2;
 else if AgentCredit='Yes' then rank=3;
 else rank=4;
run;

proc sort;
  by facilityid rank;
run;

data want;
  set rank;
  by facilityid rank;
  FLAG = first.facilityid and (rank&amp;lt;4) ;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1701277121939.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90444i3A92DF79F3FCE844/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1701277121939.png" alt="Tom_0-1701277121939.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Nov 2023 16:59:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Perform-hierarchical-if-condition-within-group/m-p/905214#M357544</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-11-29T16:59:12Z</dc:date>
    </item>
    <item>
      <title>Re: Perform hierarchical if condition within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Perform-hierarchical-if-condition-within-group/m-p/905215#M357545</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/460351"&gt;@edeede&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thanks, Patrick. I think the spreadsheet fits the narrative. For facilityid 2 Credit Suisse would be lead_arranger, and for facilityid 4 it would be UBS, that is not refelcted in the code.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;True. I've misread your narrative. Below a data step option that should work&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data inter;
  set have;
  if LenderRole='Administrative Agent' then lead_order = 1;
  else if LeadArrangerCredit = 'Yes' then lead_order = 2;
  else if AgentCredit then lead_order= 3;
  else lead_order = 999;
run;

proc sort data=inter;
  by Facilityid lead_order;
run;
    
data want;
  set inter;
  by Facilityid lead_order;
  if first.Facilityid then
    do;
      if last.Facilityid or lead_order ne 999 then lead_arranger = 1;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Nov 2023 17:17:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Perform-hierarchical-if-condition-within-group/m-p/905215#M357545</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-29T17:17:43Z</dc:date>
    </item>
    <item>
      <title>Re: Perform hierarchical if condition within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Perform-hierarchical-if-condition-within-group/m-p/905216#M357546</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; thats awesome! Thank you! How can I assign the flag to all observations within a facilityid group if multiple observations have the same rank at the same time?</description>
      <pubDate>Wed, 29 Nov 2023 17:02:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Perform-hierarchical-if-condition-within-group/m-p/905216#M357546</guid>
      <dc:creator>edeede</dc:creator>
      <dc:date>2023-11-29T17:02:08Z</dc:date>
    </item>
  </channel>
</rss>

