Hello, This works only if one component belongs to one parent. Data ProductStructure; Input @1 Parent $7. @9 Component $7. @17 Alternative $2. Percentage_Quantity; dummy=Parent!!"_"!!Component!!"_"!!Alternative; Datalines; 2000000 4000000 01 0.4 2000000 4600000 01 0.6 2000000 4000009 02 0.4 2000000 4600000 02 0.6 2000000 4000000 03 0.3 2000000 4600000 03 0.7 4600000 4500000 01 1 4600000 4500000 02 0.8 4600000 4500001 02 0.2 4600000 4500001 03 0.5 4600000 4500004 03 0.5 3000000 4000000 01 0.5 3000000 4700000 01 0.5 3000000 4000009 02 0.6 3000000 4700000 02 0.4 4700000 4500000 01 1 4700000 4500000 02 0.8 4700000 4500001 02 0.2 ; Run; proc sql; create table int as ( select x.parent, coalesce(y.inicomp, x.component) as component,x.alternative, coalesce(Percentage_Quantity*iniperc,x.Percentage_Quantity) as percent, x.component as subcomp from ProductStructure (drop=dummy) x left join ( select b.parent as inipar,b.component as inicomp, b.alternative as inialt, b.Percentage_Quantity as iniperc from ProductStructure a , ProductStructure b where a.component=b.parent and a.alternative=b.alternative) y on x.component=y.inipar where x.parent not in (select b.parent from ProductStructure a , ProductStructure b where a.component=b.parent and a.alternative=b.alternative)) order by parent, alternative, inialt ; quit; data want; retain Perc_Ini Comp_Ini Perc_Ini ;*keep initial values; set int; by parent alternative;*data already from sorted from the sql syntax; BoM_ID=catx("_",parent,alternative,subcomp,alternative); output;*write the current record to data set; percsum+percent; if first.alternative then /*set initial values;*/ do; Comp_Ini=component; Perc_Ini=percent; percsum=Perc_Ini; end; if round(percsum,0.1)=1 and not last.alternative then /*write a second line to data set if sum of percents is 1 and not the last record within group*/ do; component=Comp_Ini; percent=Perc_Ini; output; percsum=Perc_Ini; end; drop Perc_Ini Comp_Ini Perc_Ini percsum subcomp; run;
... View more