Hi below is the interview question which I faced,we can solve it by hardcoding ,can we solve it without hard coding. pls help me:
An exposure is linked to a limit. And a limit can have multiple exposures associated to it. Similarly a limit may have multiple collateral associated to it. Collateral can be shared between multiple limits. Please see the below sample input dataset,
ExposureId | LimitId | CollateralId |
1 | L1 | C1 |
2 | L1 | C2 |
3 | L2 | C1 |
4 | L2 | C3 |
5 | L3 | C4 |
6 | L3 | C5 |
7 | L4 | C6 |
8 | L5 | C5 |
You are given the task to write code and generate below output, to cluster the related exposure, limit and collateral together with a ClusterId.
ExposureId | LimitId | CollateralId | ClusterId |
1 | L1 | C1 | 1 |
2 | L1 | C2 | 1 |
3 | L2 | C1 | 1 |
4 | L2 | C3 | 1 |
5 | L3 | C4 | 2 |
6 | L3 | C5 | 2 |
7 | L4 | C6 | 3 |
8 | L5 | C5 | 2 |
NO. I am not going to talk about Hash Table here. It is a very big topic.
If you don't know anything about it ,check its documention at support.sas.com .
This isn't an easy problem to solve by the way, so it's an interesting problem from that perspective. It looks like a recursive chain look up and I think you can use the macro here. From a graph theory lens, it's asking to identify each branch.
https://gist.github.com/statgeek/14e3aa2a9f718f551cd98134e9ceed30
Or search 'recursive lookup' on here and you'll find some hash solutions.
I have answered this question before. Isn't it you ?
Here could give you a start. Once you got WANT table. It is easy to get what you want.
data have;
infile cards ;
input from $ to $ ;
cards;
1 2
1 3
4 5
5 2
9 4
6 7
8 7
;
run;
data full;
set have end=last;
if _n_ eq 1 then do;
declare hash h();
h.definekey('node');
h.definedata('node');
h.definedone();
end;
output;
node=from; h.replace();
from=to; to=node;
output;
node=from; h.replace();
if last then h.output(dataset:'node');
drop node;
run;
data want(keep=node household);
declare hash ha(ordered:'a');
declare hiter hi('ha');
ha.definekey('count');
ha.definedata('last');
ha.definedone();
declare hash _ha(hashexp: 20);
_ha.definekey('key');
_ha.definedone();
if 0 then set full;
declare hash from_to(dataset:'full(where=(from is not missing and to is not missing))',hashexp:20,multidata:'y');
from_to.definekey('from');
from_to.definedata('to');
from_to.definedone();
if 0 then set node;
declare hash no(dataset:'node');
declare hiter hi_no('no');
no.definekey('node');
no.definedata('node');
no.definedone();
do while(hi_no.next()=0);
household+1; output;
count=1;
key=node;_ha.add();
last=node;ha.add();
rc=hi.first();
do while(rc=0);
from=last;rx=from_to.find();
do while(rx=0);
key=to;ry=_ha.check();
if ry ne 0 then do;
node=to;output;rr=no.remove(key:node);
key=to;_ha.add();
count+1;
last=to;ha.add();
end;
rx=from_to.find_next();
end;
rc=hi.next();
end;
ha.clear();_ha.clear();
end;
stop;
run;
I dont have knowledge of hash tables,its abit difficult to understand,cud u pls brief me what is happening there actually
NO. I am not going to talk about Hash Table here. It is a very big topic.
If you don't know anything about it ,check its documention at support.sas.com .
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.