BookmarkSubscribeRSS Feed
hoangtuyen90
Calcite | Level 5

I have 2 column, column_A is origination, column B is source of origination, how to we have result in Column C

Examples: A7 - A5 is 1, A9 -- A7 is 2 due to A9 from A7 and A7 from A5, the same A10 -- A7 is 3 due to A10 from A9 and A9 from A7 and A7 from A5.

 

Column_A      Column_B    Result_Column_C

    A5

    A7                 A5               1

    A8                 B1               1

    A9                 A7               2

    A10               A9               3

3 REPLIES 3
Kurt_Bremser
Super User

If I insert an artificial "parent" obs for B1, this code works:

data have;
infile datalines truncover;
input col_a $ col_b $;
datalines;
A5
A7 A5
A8 B1
A9 A7
A10 A9
B1
;

data want;
set have;
if _n_ = 1
then do;
  declare hash a (dataset:'have (keep=col_a)');
  a.definekey('col_a');
  a.definedone();
  declare hash b (dataset:'have');
  b.definekey('col_b');
  b.definedata('col_a');
  b.definedone();
end;
if a.find(key:col_b) ne 0; /* master parent */
col_c = .;
output;
col_c = 1;
col_b = col_a;
rc = b.find();
do while (rc = 0);
  output;
  col_c + 1;
  col_b = col_a;
  rc = b.find();
end;
drop rc;
run;
Ksharp
Super User

It’s just one to one match ?

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore Now →
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1320 views
  • 0 likes
  • 3 in conversation