BookmarkSubscribeRSS Feed
dsbihill
Obsidian | Level 7

I am looking for a way to compare multiple observations and output a file that contains the final number from multiple part number change observations.

 

The data haveSorted below i have ordered only for ease of seeing how the part numbers are stored.  The actual data I have is not sorted in any way.

 

Any recommendations would be greatly appreciated as I have been unable to think of a method to do this.

 

DATA haveSorted;/*Manually sorted for ease of understanding*/
infile datalines delimiter=",";
input prev_part $ part_sup $;
datalines;
.,X1
X1,P2
P2,P5
P5,D3
.,4C3
4C3,7S2
7S2,6BW
.,5S2
.,6R1
6R1,3F3
3F3,24X
;


DATA have;
infile datalines delimiter=",";
input prev_part $ part_sup $;
datalines;
.,X1
X1,P2
3F3,24X
P2,P5
4C3,7S2
P5,D3
.,4C3
7S2,6BW
.,5S2
.,6R1
6R1,3F3
;


data want;
infile datalines delimiter=",";
input prev_part $ part_sup $ final_part $;
datalines;
.,X1,D3
X1,P2,D3
P2,P5,D3
P5,D3,D3
.,4C3,6BW
4C3,7S2,6BW
7S2,6BW,6BW
.,5S2,5S2
.,6R1,24X
6R1,3F3,24X
3F3,24X,24X
;

 

 

2 REPLIES 2
Haikuo
Onyx | Level 15

Since you seem to have only one branch (vs. multiple branches like train station problem), Hash could work on your 'have' out of box.

 

DATA have;
infile datalines delimiter=",";
input prev_part $ part_sup $;
datalines;
.,X1
X1,P2
3F3,24X
P2,P5
4C3,7S2
P5,D3
.,4C3
7S2,6BW
.,5S2
.,6R1
6R1,3F3
;


data want;
if _n_=1 then do;
dcl hash h(dataset:'have(rename=(prev_part=pre part_sup=final_part)', multidata:'y');
h.definekey('pre');
h.definedata(all:'y');
h.definedone();
end;
set have;
length pre final_part $ 8;
rc=h.find(key: coalescec(prev_part, part_sup));
if rc ne 0 then final_part=part_sup;
else
do rc=0 by 0 while (rc=0);
rc=h.find(key:final_part);
end;
drop rc pre;
run;

 

 Please note: for some reason, the forum macro messed up my code, it should be ... multidata:'y');

 

.. 

Reeza
Super User
You can also take a look at some of the procs under SAS/OR - possibly PROC BOM though I'm not sure.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 2 replies
  • 724 views
  • 0 likes
  • 3 in conversation