Question about consecutive variables

Reply
Contributor
Posts: 53

Question about consecutive variables

[ Edited ]

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
;

 

 

Respected Advisor
Posts: 3,156

Re: Question about consecutive variables

[ Edited ]

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');

 

.. 

Super User
Posts: 19,180

Re: Question about consecutive variables

You can also take a look at some of the procs under SAS/OR - possibly PROC BOM though I'm not sure.
Ask a Question
Discussion stats
  • 2 replies
  • 247 views
  • 0 likes
  • 3 in conversation