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

Hi,

 

i need help in finding and  viewing only duplicates by policy_no and amount

 

Data Have                                           Data Want                        

policy_no    Amount                    policy_no      Amount 

1                 500                          1                    500

1                 500                          1                   500

2                 300                          3                    100

2                 -300                         3                   100

3                 100

3                 100

4                  450

1 ACCEPTED SOLUTION

Accepted Solutions
hashman
Ammonite | Level 13

@Solly7:

SQL can be your friend, regardless of whether the input is sorted or not:

data have;                                                                                                                              
  input policy_no amount ;                                                                                                              
  cards ;                                                                                                                               
1  500                                                                                                                                  
1  500                                                                                                                                  
2  300                                                                                                                                  
2 -300                                                                                                                                  
3  100                                                                                                                                  
3  100                                                                                                                                  
4  450                                                                                                                                  
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
proc sql ;                                                                                                                              
  create table want as select * from have natural join (select policy_no, amount from have group 1, 2 having count (*) > 1) ;           
quit ;                                                                                                                        

Kind regards

Paul D.

View solution in original post

3 REPLIES 3
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @Solly7 

 

You can get your result in a data step with both policy_no Amount as by-vars. It requires data to be sorted by policy_no Amount. Output only if the same Amount occurs more than once for the same  policy_no:

 

 

Data Have;
	input policy_no Amount;
	cards;
1 500    
1 500    
2 300    
2 -300   
3 100
3 100
4 450
;
run;
proc sort data=have;
	by policy_no Amount;
run;

data want; set have;
	by policy_no Amount;
	if not (first.Amount and last.Amount);
run;

 

fl.gif

 

 

Solly7
Pyrite | Level 9

thank you

hashman
Ammonite | Level 13

@Solly7:

SQL can be your friend, regardless of whether the input is sorted or not:

data have;                                                                                                                              
  input policy_no amount ;                                                                                                              
  cards ;                                                                                                                               
1  500                                                                                                                                  
1  500                                                                                                                                  
2  300                                                                                                                                  
2 -300                                                                                                                                  
3  100                                                                                                                                  
3  100                                                                                                                                  
4  450                                                                                                                                  
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
proc sql ;                                                                                                                              
  create table want as select * from have natural join (select policy_no, amount from have group 1, 2 having count (*) > 1) ;           
quit ;                                                                                                                        

Kind regards

Paul D.

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
  • 3 replies
  • 832 views
  • 0 likes
  • 3 in conversation