<?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: Determine parent in a hierarchical classification structure in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/794001#M254551</link>
    <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;!!!! You did it! Wow! It works exactly as I had hoped. I am so impressed. This isn't the first time you saved the day: You solved a really complicated problem for me many years ago. Thank you again. You are a rock star!</description>
    <pubDate>Wed, 02 Feb 2022 12:56:13 GMT</pubDate>
    <dc:creator>sas-inquirer</dc:creator>
    <dc:date>2022-02-02T12:56:13Z</dc:date>
    <item>
      <title>Determine parent in a hierarchical classification structure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/793847#M254491</link>
      <description>&lt;P&gt;I need to determine the parent of a value. I have a classification structure that is hierarchical in nature. The logic: for each group, the parent of the class is the first value from an undetermined row above that has a smaller length than the current length.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input group $ class $ class_length;
datalines;
68 1 1
68 11 2
68 1101 4
68 1102 4
68 1103 4&lt;BR /&gt;68 12 2&lt;BR /&gt;68 1201 4&lt;BR /&gt;68 1202 4&lt;BR /&gt;27 1 1&lt;BR /&gt;27 1101 4
27 2 1
27 2201 4
27 2202 4
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The end result should look something like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;
input group $ class $ class_length parent $;
datalines;
68 1 1 .
68 11 2 1
68 1101 4 11
68 1102 4 11
68 1103 4 11&lt;BR /&gt;68 12 2 1&lt;BR /&gt;68 1201 4 12&lt;BR /&gt;68 1202 4 12&lt;BR /&gt;27 1 1 .&lt;BR /&gt;27 1101 4 1
27 2 1 .
27 2201 4 2
27 2202 4 2
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I have tried using lag and first but I always run into trouble with the siblings (i.e. [11,12], [1101,1102,1103] etc.).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would super appreciate any help someone could provide. Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Feb 2022 17:19:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/793847#M254491</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2022-02-01T17:19:24Z</dc:date>
    </item>
    <item>
      <title>Re: Determine parent in a hierarchical classification structure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/793856#M254494</link>
      <description>&lt;P&gt;I think you may want to expand on your actual rule(s) for assigning the Parent values.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Feb 2022 17:39:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/793856#M254494</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-02-01T17:39:32Z</dc:date>
    </item>
    <item>
      <title>Re: Determine parent in a hierarchical classification structure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/793859#M254496</link>
      <description>Say you take class value 1103 from group 68 and you have to determine which class is the parent of it.&lt;BR /&gt;1) Look at the length of 1103 which is 4.&lt;BR /&gt;2) Move up the list until you reach a length that is less than 4 (so skip 1102 and 1101).&lt;BR /&gt;3) The first instance of a length less than 4 above 1103 is 11 with a length of 2.&lt;BR /&gt;4) Identify the parent of 1103 which is 11 because it is the first instance above 1103 with a length less than 4.&lt;BR /&gt;&lt;BR /&gt;I hope this example helps.</description>
      <pubDate>Tue, 01 Feb 2022 18:19:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/793859#M254496</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2022-02-01T18:19:37Z</dc:date>
    </item>
    <item>
      <title>Re: Determine parent in a hierarchical classification structure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/793976#M254540</link>
      <description>&lt;P&gt;Which values for class_length are possible?&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 08:14:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/793976#M254540</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-02-02T08:14:33Z</dc:date>
    </item>
    <item>
      <title>Re: Determine parent in a hierarchical classification structure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/793977#M254541</link>
      <description>&lt;PRE&gt;data have;
input group $ class $ class_length;
datalines;
68 1 1
68 11 2
68 1101 4
68 1102 4
68 1103 4
68 12 2
68 1201 4
68 1202 4
27 1 1
27 1101 4
27 2 1
27 2201 4
27 2202 4
;
data want;
 if _n_=1 then do;
  if 0 then set have;
  length parent par $ 80; par=' ';
  length n len 8; n=.;len=.;
  declare hash h();
  h.definekey('n');
  h.definedata('len','par');
  h.definedone();
 end;
h.clear();
do i=1 by 1 until(last.group);
 set have ;
 by group notsorted;
 h.add(key:i,data:class_length,data:class);
 call missing(parent);
 do n=i-1 to 1 by -1;
   if h.find()=0 then do; if len &amp;lt; class_length then do;parent=par; leave; end;end;
 end;
 output;
end;
drop n len i par;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Feb 2022 08:15:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/793977#M254541</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-02-02T08:15:23Z</dc:date>
    </item>
    <item>
      <title>Re: Determine parent in a hierarchical classification structure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/794000#M254550</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; the max class_length possible is 15.</description>
      <pubDate>Wed, 02 Feb 2022 12:38:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/794000#M254550</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2022-02-02T12:38:28Z</dc:date>
    </item>
    <item>
      <title>Re: Determine parent in a hierarchical classification structure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/794001#M254551</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;!!!! You did it! Wow! It works exactly as I had hoped. I am so impressed. This isn't the first time you saved the day: You solved a really complicated problem for me many years ago. Thank you again. You are a rock star!</description>
      <pubDate>Wed, 02 Feb 2022 12:56:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/794001#M254551</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2022-02-02T12:56:13Z</dc:date>
    </item>
    <item>
      <title>Re: Determine parent in a hierarchical classification structure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/794002#M254552</link>
      <description>But your question is really weird . Does your data really look like this and the order of data is really as you showed ?</description>
      <pubDate>Wed, 02 Feb 2022 13:00:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/794002#M254552</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-02-02T13:00:25Z</dc:date>
    </item>
    <item>
      <title>Re: Determine parent in a hierarchical classification structure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/794005#M254553</link>
      <description>This is obviously mock data and the original data can have some 60K lines but for the most part, yes the data really looks like this. I'm working with a bunch of reports where each row represents a data point in a report with higher level classes (parent) being aggregates of the children below. Some reports take all of the class levels and other reports focus on varying levels of aggregates. The mock data I supplied here showcased the different scenarios I encountered. Thanks again. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Wed, 02 Feb 2022 13:21:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/794005#M254553</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2022-02-02T13:21:00Z</dc:date>
    </item>
    <item>
      <title>Re: Determine parent in a hierarchical classification structure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/794077#M254584</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp; I'm hoping you can help me once again. I've been testing the code on various reports to ensure the results are what is expected and have hit a scenario which requires an additional condition. I have one report that should not have any parents because there are no aggregates in it and another report that has only one or two parents (aggregates).&lt;/P&gt;&lt;P&gt;The condition would be&amp;nbsp;that it needs to move up until length is less than current length &lt;STRONG&gt;and&lt;/STRONG&gt; also that the parent is a substring of the current class.&amp;nbsp;Here is what I'm looking for with the new group 31 added.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;
input group $ class $ class_length parent $;
datalines;
68 1 1 .
68 11 2 1
68 1101 4 11
68 1102 4 11
68 1103 4 11
68 12 2 1
68 1201 4 12
68 1202 4 12
27 1 1 .
27 1101 4 1
27 2 1 .
27 2201 4 2
27 2202 4 2
31 1 1 .
31 2101 4 .
31 2102 4 .
31 220201 6 .
31 220202 6 .
31 22020201 8 220202
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So with the original logic for group 31, class 220201 would end up with the parent 2102 but because 2102 is not a substring of 220201 then the parent should be blank. No matter how far up you move, 220201 has no parent. I hope this makes sense.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please let me know if you know of a way to add this condition to the original code you supplied. Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 18:21:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/794077#M254584</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2022-02-02T18:21:30Z</dc:date>
    </item>
    <item>
      <title>Re: Determine parent in a hierarchical classification structure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/794166#M254621</link>
      <description>&lt;P&gt;I guess you want CLASS start with PARENT , not a substring ,otherwise why&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;31 2101 4 .&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;doesn't have parent 1 , since 1 is a substring within 2101 ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input group $ class $ class_length;
datalines;
68 1 1
68 11 2
68 1101 4
68 1102 4
68 1103 4
68 12 2
68 1201 4
68 1202 4
27 1 1
27 1101 4
27 2 1
27 2201 4
27 2202 4
31 1 1 .
31 2101 4 .
31 2102 4 .
31 220201 6 .
31 220202 6 .
31 22020201 8 
;
data want;
 if _n_=1 then do;
  if 0 then set have;
  length parent par $ 80; par=' ';
  length n len 8; n=.;len=.;
  declare hash h();
  h.definekey('n');
  h.definedata('len','par');
  h.definedone();
 end;
h.clear();
do i=1 by 1 until(last.group);
 set have ;
 by group notsorted;
 h.add(key:i,data:class_length,data:class);
 call missing(parent);
 do n=i-1 to 1 by -1;
   if h.find()=0 then do; if len &amp;lt; class_length and class =: strip(par) then do;parent=par; leave; end;end;
 end;
 output;
end;
drop n len i par;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Feb 2022 06:54:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/794166#M254621</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-02-03T06:54:39Z</dc:date>
    </item>
    <item>
      <title>Re: Determine parent in a hierarchical classification structure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/794205#M254642</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt; beautiful! That makes more sense than substring. I can't thank you enough. I have spent more time than I'd like to admit trying to figure this out.</description>
      <pubDate>Thu, 03 Feb 2022 12:29:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Determine-parent-in-a-hierarchical-classification-structure/m-p/794205#M254642</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2022-02-03T12:29:10Z</dc:date>
    </item>
  </channel>
</rss>

