BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
erdem_ustun
Obsidian | Level 7

Hi,

I have a data as it below and  lots of records.

 

id,sid,ssn_1,ssn_2,ssn_3,fname,mname,is_same
1,1,101,245,655,aa,bb,base
2,1,101,202,655,AAA,bb,
3,1,103,200,657,CCC,bb,
4,1,103,245,655,aa,DDE,
5,1,104,245,699,BBB,MKE,
6,2,303,490,1310,Maa,bb,base
7,2,303,404,1310,FFF,bb,
8,2,309,400,1314,KKK,bb,
9,2,309,490,1310,Maa,DDE,
10,3,312,735,2097,BBB,TME,base
11,3,303,735,1965,aa,bb,
12,4,1212,808,2620,LARE,KEAN,base
13,4,1236,800,2628,CCC,bb,
14,4,1236,808,2620,LARE,KAANE,
15,4,1212,980,2796,BBB,KEAN,
..,..,..,..,..,..,..,..

I want to do is to compare with hash object  the records  and fields by same sid .İf it is same all fields .

All variables are the same ' put YYYYY' to is_same.if only first (ssn_1) and third(ssn_3) fields are the same put YNYNN' to is_same.

is it possible to use nested hash object?

The reference point for each sid where result='base' 

for example first loop is 

reference_1: id=1 and sid=1 and ssn_1=101 and ssn_2=245 and ssn_3=655 and fname=aa and sname=bb

compare_1:  id=2 and sid=1 and ssn_1=101 and ssn_2=202 and ssn_3=655 and fname=AAA and sname=bb

result_1 :for ssn_1 is same and dor ssn_2 is not the same and for ssn_3 is same and for fname  is not the same and for sname is same then result=YNYNY

....loop

if sid changed, the reference point will change.

 

 How do I resolve this situation.

I want to result  for sid=1

 

id,sid,ssn_1,ssn_2,ssn_3,fname,mname,is_same
1,1,101,245,655,aa,bb,base
2,1,101,202,655,AAA,bb,YNYNY
3,1,103,200,657,CCC,bb,NNNNN
4,1,103,245,655,aa,DDE,NYYYN
5,1,104,245,699,BBB,MKE,NYNNN
..,..,..,..,..,..,..,..

 result.jpg

is it possible to solve to nested and conditional hash object for in order to be fast and effective.

 

best regards..

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
Hash Object has a method EQUAL() to judge whether two Hash Object is equal .But the way I looked, you don't need this method .

data have;
infile cards truncover dlm=',';
input id sid ssn_1 ssn_2 ssn_3 fname $ mname $ is_same $;
cards;
1,1,101,245,655,aa,bb,base
2,1,101,202,655,AAA,bb,
3,1,103,200,657,CCC,bb,
4,1,103,245,655,aa,DDE,
5,1,104,245,699,BBB,MKE,
6,2,303,490,1310,Maa,bb,base
7,2,303,404,1310,FFF,bb,
8,2,309,400,1314,KKK,bb,
9,2,309,490,1310,Maa,DDE,
10,3,312,735,2097,BBB,TME,base
11,3,303,735,1965,aa,bb,
12,4,1212,808,2620,LARE,KEAN,base
13,4,1236,800,2628,CCC,bb,
14,4,1236,808,2620,LARE,KAANE,
15,4,1212,980,2796,BBB,KEAN,
;
run;
data want;
 set have;
 by sid;
 if first.sid then do;
  _ssn_1=ssn_1; _ssn_2=ssn_2; _ssn_3=ssn_3; _fname=fname; _mname=mname;
 end;
 else do;
  is_same=cats(ifc(ssn_1=_ssn_1,'Y','N'),
               ifc(ssn_2=_ssn_2 ,'Y','N'),
               ifc(ssn_3=_ssn_3 ,'Y','N'),
               ifc(fname=_fname ,'Y','N'),
               ifc(mname=_mname,'Y','N'));
 end;
 retain _ssn_1 _ssn_2 _ssn_3 _fname _mname;
 drop _:;
run;

View solution in original post

2 REPLIES 2
Ksharp
Super User
Hash Object has a method EQUAL() to judge whether two Hash Object is equal .But the way I looked, you don't need this method .

data have;
infile cards truncover dlm=',';
input id sid ssn_1 ssn_2 ssn_3 fname $ mname $ is_same $;
cards;
1,1,101,245,655,aa,bb,base
2,1,101,202,655,AAA,bb,
3,1,103,200,657,CCC,bb,
4,1,103,245,655,aa,DDE,
5,1,104,245,699,BBB,MKE,
6,2,303,490,1310,Maa,bb,base
7,2,303,404,1310,FFF,bb,
8,2,309,400,1314,KKK,bb,
9,2,309,490,1310,Maa,DDE,
10,3,312,735,2097,BBB,TME,base
11,3,303,735,1965,aa,bb,
12,4,1212,808,2620,LARE,KEAN,base
13,4,1236,800,2628,CCC,bb,
14,4,1236,808,2620,LARE,KAANE,
15,4,1212,980,2796,BBB,KEAN,
;
run;
data want;
 set have;
 by sid;
 if first.sid then do;
  _ssn_1=ssn_1; _ssn_2=ssn_2; _ssn_3=ssn_3; _fname=fname; _mname=mname;
 end;
 else do;
  is_same=cats(ifc(ssn_1=_ssn_1,'Y','N'),
               ifc(ssn_2=_ssn_2 ,'Y','N'),
               ifc(ssn_3=_ssn_3 ,'Y','N'),
               ifc(fname=_fname ,'Y','N'),
               ifc(mname=_mname,'Y','N'));
 end;
 retain _ssn_1 _ssn_2 _ssn_3 _fname _mname;
 drop _:;
run;

erdem_ustun
Obsidian | Level 7

Thank you very much for your answer.

I wanted to do with has object for the speed and efficiency.

I understand that you said  there hash object  Where equality occurs of this method.

Thank you for your explanation.

 

The code you have written comparing the next record.I did an insert for code and results occurred exactly what I expected.


if first.sid and is_same='base' then do;

In the final code below ;

data want;
 set have;
 by sid;
 if first.sid and is_same='base' then do;
  _ssn_1=ssn_1; _ssn_2=ssn_2; _ssn_3=ssn_3; _fname=fname; _mname=mname;
 end;
 else do;
  is_same=cats(ifc(ssn_1=_ssn_1,'Y','N'),
               ifc(ssn_2=_ssn_2 ,'Y','N'),
               ifc(ssn_3=_ssn_3 ,'Y','N'),
               ifc(fname=_fname ,'Y','N'),
               ifc(mname=_mname,'Y','N'));
 end;
 retain _ssn_1 _ssn_2 _ssn_3 _fname _mname;
 drop _:;
run;

best regards..

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 496 views
  • 0 likes
  • 2 in conversation