<?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: Inheritence in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Inheritence/m-p/298362#M62737</link>
    <description>&lt;P&gt;Questions to get started:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Does the real data set actually contain many P1 records, with a different product for each?&lt;/LI&gt;
&lt;LI&gt;Does the variable PRODUCT actually have a nonmissing value on the P1 record?&amp;nbsp; (If it doesn't, could it be added?)&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;I'm looking at an approach that would sort the records in reverse order ... making it easy to see all the P3 records for a given P2 record before having to assign a P2 status, for example.&amp;nbsp; So in the worst case we could take this approach by adding the record number fo the data.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 14 Sep 2016 16:11:27 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2016-09-14T16:11:27Z</dc:date>
    <item>
      <title>Inheritence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inheritence/m-p/298350#M62732</link>
      <description>&lt;P&gt;Data Specifications:&lt;/P&gt;&lt;P&gt;I have three Different levels P1, P2 and P3 for each Product. The relation between them is P3 is a level under P2 and P2 is a level under P1. There will be only one row for P1, but there may multiple rows for P2 and P3. Also there will be a status Value (either A,B or C) always for Level-P3. And for P1 and P2 it will be empty.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So my requirement here is if All P3’s Have a status called “A” then I should get the same status for my P2. Now if all P2’s has the same status then my P1 also should get same status.&lt;/P&gt;&lt;P&gt;If all of my P3’s under a specific P2 are having 2 different statuses then I should get a 4th status called D for my P2, and if all of my P2 have two different status I should get status D for my P1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data Have;&lt;BR /&gt;input P_Level$ Product$ PL1$ PL2$ B_Flag Status$;&lt;BR /&gt;infile cards missover;&lt;BR /&gt;cards;&lt;BR /&gt;P1 X .&lt;BR /&gt;P2 X PL11 .&lt;BR /&gt;P3 X PL11 PL21 1 A&lt;BR /&gt;P3 X PL11 PL22 1 A&lt;BR /&gt;P3 X PL11 PL23 1 A&lt;BR /&gt;P2 X PL12 .&lt;BR /&gt;P3 X PL12 PL21 1 A&lt;BR /&gt;P3 X PL12 PL22 1 A&lt;BR /&gt;P3 X PL12 PL23 1 A&lt;BR /&gt;P2 X PL13 .&lt;BR /&gt;P3 X PL13 PL21 1 A&lt;BR /&gt;P3 X PL13 PL22 1 A&lt;BR /&gt;P3 X PL13 PL23 1 A&lt;BR /&gt;P1 Y .&lt;BR /&gt;P2 Y PL11 .&lt;BR /&gt;P3 Y PL11 PL21 1 A&lt;BR /&gt;P3 Y PL11 PL22 1 B&lt;BR /&gt;P3 Y PL11 PL23 1 A&lt;BR /&gt;P2 Y PL12 .&lt;BR /&gt;P3 Y PL12 PL21 1 A&lt;BR /&gt;P3 Y PL12 PL22 1 A&lt;BR /&gt;P3 Y PL12 PL23 1 A&lt;BR /&gt;P2 Y PL13 .&lt;BR /&gt;P3 Y PL13 PL21 1 A&lt;BR /&gt;P3 Y PL13 PL22 1 A&lt;BR /&gt;P3 Y PL13 PL23 1 C&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;data Want;&lt;BR /&gt;input P_Level$ Product$ PL1$ PL2$ B_Flag Status$;&lt;BR /&gt;infile cards missover;&lt;BR /&gt;cards;&lt;BR /&gt;P1 X . . . A&lt;BR /&gt;P2 X PL11 . . A&lt;BR /&gt;P3 X PL11 PL21 1 A&lt;BR /&gt;P3 X PL11 PL22 1 A&lt;BR /&gt;P3 X PL11 PL23 1 A&lt;BR /&gt;P2 X PL12 . . A&lt;BR /&gt;P3 X PL12 PL21 1 A&lt;BR /&gt;P3 X PL12 PL22 1 A&lt;BR /&gt;P3 X PL12 PL23 1 A&lt;BR /&gt;P2 X PL13 . . A&lt;BR /&gt;P3 X PL13 PL21 1 A&lt;BR /&gt;P3 X PL13 PL22 1 A&lt;BR /&gt;P3 X PL13 PL23 1 A&lt;BR /&gt;P1 Y . . . D&lt;BR /&gt;P2 Y PL11 . . D&lt;BR /&gt;P3 Y PL11 PL21 1 A&lt;BR /&gt;P3 Y PL11 PL22 1 B&lt;BR /&gt;P3 Y PL11 PL23 1 A&lt;BR /&gt;P2 Y PL12 . . A&lt;BR /&gt;P3 Y PL12 PL21 1 A&lt;BR /&gt;P3 Y PL12 PL22 1 A&lt;BR /&gt;P3 Y PL12 PL23 1 A&lt;BR /&gt;P2 Y PL13 . . D&lt;BR /&gt;P3 Y PL13 PL21 1 A&lt;BR /&gt;P3 Y PL13 PL22 1 A&lt;BR /&gt;P3 Y PL13 PL23 1 C&lt;BR /&gt;;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2016 15:06:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inheritence/m-p/298350#M62732</guid>
      <dc:creator>sascodequestion</dc:creator>
      <dc:date>2016-09-14T15:06:10Z</dc:date>
    </item>
    <item>
      <title>Re: Inheritence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inheritence/m-p/298361#M62736</link>
      <description>&lt;P&gt;The key is to 'group'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data h1;
set have;
grp+(lag(p_level)='P3' and p_level ne 'P3')+0;
run;

data want;
 do until (last.grp);
  set h1;
    by grp;
	if status ne _sta and not missing(_sta) then _flag=1;
	_sta=status;
 end;

  do until (last.grp);
  set h1;
    by grp;
	status=ifc(p_level='P3',status, ifc(_flag=1,'D',_sta));
	output;
 end;&lt;BR /&gt;drop _:;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Sep 2016 16:09:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inheritence/m-p/298361#M62736</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-09-14T16:09:57Z</dc:date>
    </item>
    <item>
      <title>Re: Inheritence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inheritence/m-p/298362#M62737</link>
      <description>&lt;P&gt;Questions to get started:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Does the real data set actually contain many P1 records, with a different product for each?&lt;/LI&gt;
&lt;LI&gt;Does the variable PRODUCT actually have a nonmissing value on the P1 record?&amp;nbsp; (If it doesn't, could it be added?)&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;I'm looking at an approach that would sort the records in reverse order ... making it easy to see all the P3 records for a given P2 record before having to assign a P2 status, for example.&amp;nbsp; So in the worst case we could take this approach by adding the record number fo the data.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2016 16:11:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inheritence/m-p/298362#M62737</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-09-14T16:11:27Z</dc:date>
    </item>
    <item>
      <title>Re: Inheritence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inheritence/m-p/298389#M62747</link>
      <description>&lt;P&gt;Does the real data set actually contain many P1 records, with a different product for each?&lt;/P&gt;&lt;P&gt;ANs : Yes&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does the variable PRODUCT actually have a nonmissing value on the P1 record?&amp;nbsp; (If it doesn't, could it be added?)&lt;/P&gt;&lt;P&gt;ANs: For the P1 record , Product is the last non missing value&lt;/P&gt;&lt;P&gt;For the P2 record, Pl1 is the last non Missing value and for P3-Pl2.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2016 17:39:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inheritence/m-p/298389#M62747</guid>
      <dc:creator>sascodequestion</dc:creator>
      <dc:date>2016-09-14T17:39:18Z</dc:date>
    </item>
    <item>
      <title>Re: Inheritence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inheritence/m-p/298402#M62760</link>
      <description>&lt;P&gt;Here's some untested code.&amp;nbsp; It looks a little clumsy, but should be a viable approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have2;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;recno = _n_;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have2;&lt;/P&gt;
&lt;P&gt;by descending recno;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have2;&lt;/P&gt;
&lt;P&gt;length P1_Status P2_Status $ 1;&lt;/P&gt;
&lt;P&gt;retain P1_Status P2_Status;&lt;/P&gt;
&lt;P&gt;if p_level = 'P3' then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if p2_status = ' ' then p2_status=status;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; else if status ne p2_status then p2_status='D';&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;else if p_level = 'P2' then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; status = p2_status;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if p1_status = ' ' then p1_status = p2_status;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; else if p2_status ne p1_status then p1_status = 'D';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; p2_status = ' ';&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;else if p_level = 'P1' then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; status = p1_status;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; p1_status = ' ';&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop p1_status p2_status;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=want;&lt;/P&gt;
&lt;P&gt;by recno;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2016 18:48:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inheritence/m-p/298402#M62760</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-09-14T18:48:05Z</dc:date>
    </item>
    <item>
      <title>Re: Inheritence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inheritence/m-p/298491#M62782</link>
      <description>&lt;P&gt;Here is an SQL based solution&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table P2 as
select "P2" as P_Level, 
    Product, 
    PL1, 
    case 
        when count(distinct Status) &amp;gt; 1 then "D"
        else max(Status)
        end as Status
from have
where p_level = "P3"
group by Product, PL1;
create table P1 as
select "P1" as P_Level, 
    Product,
    case 
        when count(distinct Status) &amp;gt; 1 then "D"
        else max(Status)
        end as Status
from P2
group by Product;
quit;

data want;
set have(where=(p_level="P3")) P1 P2;
run;

proc sort data=want; by product PL1 PL2; run;

proc print data=want; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Sep 2016 02:42:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inheritence/m-p/298491#M62782</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-09-15T02:42:27Z</dc:date>
    </item>
    <item>
      <title>Re: Inheritence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inheritence/m-p/298501#M62787</link>
      <description>&lt;PRE&gt;

data Have;
input P_Level$ Product$ PL1$ PL2$ B_Flag Status$;
infile cards missover;
cards;
P1 X .
P2 X PL11 .
P3 X PL11 PL21 1 A
P3 X PL11 PL22 1 A
P3 X PL11 PL23 1 A
P2 X PL12 .
P3 X PL12 PL21 1 A
P3 X PL12 PL22 1 A
P3 X PL12 PL23 1 A
P2 X PL13 .
P3 X PL13 PL21 1 A
P3 X PL13 PL22 1 A
P3 X PL13 PL23 1 A
P1 Y .
P2 Y PL11 .
P3 Y PL11 PL21 1 A
P3 Y PL11 PL22 1 B
P3 Y PL11 PL23 1 A
P2 Y PL12 .
P3 Y PL12 PL21 1 A
P3 Y PL12 PL22 1 A
P3 Y PL12 PL23 1 A
P2 Y PL13 .
P3 Y PL13 PL21 1 A
P3 Y PL13 PL22 1 A
P3 Y PL13 PL23 1 C
;
run;
data p1 p2 p3;
 set have;
 if p_level='P1' then output p1;
  else if p_level='P2' then output p2;
   else if p_level='P3' then output p3;
run;
proc sql;
create table n_levels as 
 select product,PL1,count(distinct status) as n 
  from p3
   group by product,PL1;
  
create table levels as
 select distinct product,PL1,status
  from p3;
quit;
data p2;
 if _n_=1 then do;
  if 0 then set n_levels;
  declare hash ha_n(dataset:'n_levels');
  ha_n.definekey('product','PL1');
  ha_n.definedata('n');
  ha_n.definedone();
  
  if 0 then set levels;
  declare hash ha_le(dataset:'levels');
  ha_le.definekey('product','PL1');
  ha_le.definedata('status');
  ha_le.definedone();
 end;
 set p2;
 ha_n.find();
 if n ne 1 then status='D';
  else ha_le.find();
 drop n;
run;

/*****************/
proc sql;
create table n_levels as 
 select product,count(distinct status) as n 
  from p2
   group by product;
  
create table levels as
 select distinct product,status
  from p2;
quit;
data p1;
 if _n_=1 then do;
  if 0 then set n_levels;
  declare hash ha_n(dataset:'n_levels');
  ha_n.definekey('product');
  ha_n.definedata('n');
  ha_n.definedone();
  
  if 0 then set levels;
  declare hash ha_le(dataset:'levels');
  ha_le.definekey('product');
  ha_le.definedata('status');
  ha_le.definedone();
 end;
 set p1;
 ha_n.find();
 if n ne 1 then status='D';
  else ha_le.find();
 drop n;
run;
 
data want;
 set p1 p2 p3;
 by  Product PL1 p_level;
run;

&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Sep 2016 04:48:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inheritence/m-p/298501#M62787</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-09-15T04:48:43Z</dc:date>
    </item>
    <item>
      <title>Re: Inheritence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inheritence/m-p/298503#M62788</link>
      <description>&lt;PRE&gt;
Or make an index variable to keep the original order:


data Have;
input P_Level$ Product$ PL1$ PL2$ B_Flag Status$;
idx+1;
infile cards missover;
cards;
P1 X .
P2 X PL11 .
P3 X PL11 PL21 1 A
P3 X PL11 PL22 1 A
P3 X PL11 PL23 1 A
P2 X PL12 .
P3 X PL12 PL21 1 A
P3 X PL12 PL22 1 A
P3 X PL12 PL23 1 A
P2 X PL13 .
P3 X PL13 PL21 1 A
P3 X PL13 PL22 1 A
P3 X PL13 PL23 1 A
P1 Y .
P2 Y PL11 .
P3 Y PL11 PL21 1 A
P3 Y PL11 PL22 1 B
P3 Y PL11 PL23 1 A
P2 Y PL12 .
P3 Y PL12 PL21 1 A
P3 Y PL12 PL22 1 A
P3 Y PL12 PL23 1 A
P2 Y PL13 .
P3 Y PL13 PL21 1 A
P3 Y PL13 PL22 1 A
P3 Y PL13 PL23 1 C
;
run;
data p1 p2 p3;
 set have;
 if p_level='P1' then output p1;
  else if p_level='P2' then output p2;
   else if p_level='P3' then output p3;
run;
proc sql;
create table n_levels as 
 select product,PL1,count(distinct status) as n 
  from p3
   group by product,PL1;
  
create table levels as
 select distinct product,PL1,status
  from p3;
quit;
data p2;
 if _n_=1 then do;
  if 0 then set n_levels;
  declare hash ha_n(dataset:'n_levels');
  ha_n.definekey('product','PL1');
  ha_n.definedata('n');
  ha_n.definedone();
  
  if 0 then set levels;
  declare hash ha_le(dataset:'levels');
  ha_le.definekey('product','PL1');
  ha_le.definedata('status');
  ha_le.definedone();
 end;
 set p2;
 ha_n.find();
 if n ne 1 then status='D';
  else ha_le.find();
 drop n;
run;

/*****************/
proc sql;
create table n_levels as 
 select product,count(distinct status) as n 
  from p2
   group by product;
  
create table levels as
 select distinct product,status
  from p2;
quit;
data p1;
 if _n_=1 then do;
  if 0 then set n_levels;
  declare hash ha_n(dataset:'n_levels');
  ha_n.definekey('product');
  ha_n.definedata('n');
  ha_n.definedone();
  
  if 0 then set levels;
  declare hash ha_le(dataset:'levels');
  ha_le.definekey('product');
  ha_le.definedata('status');
  ha_le.definedone();
 end;
 set p1;
 ha_n.find();
 if n ne 1 then status='D';
  else ha_le.find();
 drop n;
run;
 
data want;
 set p3 p2 p1;
run;
proc sort data=want;by idx;run;

&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Sep 2016 04:52:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inheritence/m-p/298503#M62788</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-09-15T04:52:03Z</dc:date>
    </item>
  </channel>
</rss>

