BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
vidyasagar1
Obsidian | Level 7
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 ComponentComponent_New
17895078950
27895178950
36789078951
43123467890
44567867890
44567967890
43456767890
38945667890
48901267890
48901567890
46789267890
38901678951
27654878950
38945676548
48976589456
17895178951
29998778951
38989199987
47788989891
47798989891
47986789891
49876589891
38976399987
49865789763
46754389763
44563789763
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@vidyasagar1 

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;

View solution in original post

4 REPLIES 4
ballardw
Super User

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.

vidyasagar1
Obsidian | Level 7

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.

Patrick
Opal | Level 21

@vidyasagar1 

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;
vidyasagar1
Obsidian | Level 7

Thank you so much i struggled a lot for this.

I greatly appreciate your help.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1472 views
  • 5 likes
  • 3 in conversation