data one;
infile datalines;
input Level 1. Component $5.;
datalines;
1 78950
2 78951
3 67890
4 31234
4 45678
4 45679
4 34567
3 89456
4 89012
4 89015
4 67892
3 89016
2 76548
3 89456
4 89765
1 78951
2 99987
3 89891
4 77889
4 77989
4 79867
4 98765
3 89763
4 98657
4 67543
4 45637
;run;I have data like above and i need to get the output like below;
| Level | Component | Component_New |
| 1 | 78950 | 78950 |
| 2 | 78951 | 78950 |
| 3 | 67890 | 78951 |
| 4 | 31234 | 67890 |
| 4 | 45678 | 67890 |
| 4 | 45679 | 67890 |
| 4 | 34567 | 67890 |
| 3 | 89456 | 67890 |
| 4 | 89012 | 67890 |
| 4 | 89015 | 67890 |
| 4 | 67892 | 67890 |
| 3 | 89016 | 78951 |
| 2 | 76548 | 78950 |
| 3 | 89456 | 76548 |
| 4 | 89765 | 89456 |
| 1 | 78951 | 78951 |
| 2 | 99987 | 78951 |
| 3 | 89891 | 99987 |
| 4 | 77889 | 89891 |
| 4 | 77989 | 89891 |
| 4 | 79867 | 89891 |
| 4 | 98765 | 89891 |
| 3 | 89763 | 99987 |
| 4 | 98657 | 89763 |
| 4 | 67543 | 89763 |
| 4 | 45637 | 89763 |
Below code returns how I understand the logic you describe. The result differs slightly from what you've posted as desired result.
I assumed typos in your desired result. If that's not the case then can you please explain the logic required to get to the values where the numbers differ.
data one;
infile datalines;
input Level 1. Component $6.;
datalines;
1 78950
2 78951
3 67890
4 31234
4 45678
4 45679
4 34567
3 89456
4 89012
4 89015
4 67892
3 89016
2 76548
3 89456
4 89765
1 78951
2 99987
3 89891
4 77889
4 77989
4 79867
4 98765
3 89763
4 98657
4 67543
4 45637
;
run;
data want;
set one;
by level notsorted;
array _comp_new {0:9} $6 _temporary_;
if last.level then
do;
_comp_new[level]=Component;
if level=1 then _comp_new[0]=Component;
end;
Component_New=_comp_new[level-1];
run;
So what is rule (or rules) involved in which value is "retained"?
I do not see any obvious pattern so you need to supply details.
If level=1 then component_new=component and if level=2 then component_new=component of Level1 and if level=3 then component_new=component of Level2 and if level=4 then component_new=component of Level3 and so on.
If level=1 component value changes then component_new for Level2 has to retain new value.
If level=2 component value changes then component_new for Level3 has to retain new value and so on.
Below code returns how I understand the logic you describe. The result differs slightly from what you've posted as desired result.
I assumed typos in your desired result. If that's not the case then can you please explain the logic required to get to the values where the numbers differ.
data one;
infile datalines;
input Level 1. Component $6.;
datalines;
1 78950
2 78951
3 67890
4 31234
4 45678
4 45679
4 34567
3 89456
4 89012
4 89015
4 67892
3 89016
2 76548
3 89456
4 89765
1 78951
2 99987
3 89891
4 77889
4 77989
4 79867
4 98765
3 89763
4 98657
4 67543
4 45637
;
run;
data want;
set one;
by level notsorted;
array _comp_new {0:9} $6 _temporary_;
if last.level then
do;
_comp_new[level]=Component;
if level=1 then _comp_new[0]=Component;
end;
Component_New=_comp_new[level-1];
run;
Thank you so much i struggled a lot for this.
I greatly appreciate your help.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.