How do i do complex by group processing?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

How do i do complex by group processing?

Hi all,

 

I have a question regarding the table below (code used to generate it is at bottom of page). My objective is to do a deduplication by email based on the conditions below:

- if there are repeating values of email, priority is given to the origin with the highest rank (where 1=highest , 3=lowest);

- If an email with a rank of 1 is picked over one with a rank of 2, create another data set with a new variable - REASON - dropped since email exist in Europe:

 

Screen Shot 2018-04-05 at 04.37.36.png

So the two output tables are desired. One contained the emails picked and another containing the emails dropped as follows:

Screen Shot 2018-04-05 at 04.45.15.png

 

Screen Shot 2018-04-05 at 04.54.08.png

Here are the steps I have taken to get this done:

- sort by email and rank

- then output first record only for each by group

Codes used are below: But how do I create the desired drop out reason table above please?

 

proc sort data=t1;
by email rank;
run;

 

data t2;
set t1;
by email rank;
if first.email;
run;

 

I have been able to create the first desired table above but dont know how to go about creating the drop out reason table.

 

Please any ideas will help.

 

Thanks.

 

Code to generate the example data set is:

 

data t1;
length email $18 origin $8;
input email $ rank origin $;
datalines;
llp@lit.com 2 Canada
llp@lit.com 1 Europe
llp@lit.com 3 USA
brin@yahoo.com 3 USA
brin@yahoo.com 2 Canada
admin@gmail.com 2 Canada
admin@gmail.com 3 USA
;


Accepted Solutions
Solution
‎04-05-2018 02:32 AM
PROC Star
Posts: 2,370

Re: How do i do complex by group processing?

[ Edited ]
Posted in reply to ClareRobin1

Like this?

 

data HAVE(index=(A=(EMAIL RANK )));
  length EMAIL $18 ;
  input EMAIL $ RANK ORIGIN $;
datalines;
llp@lit.com 2 Canada
llp@lit.com 1 Europe
llp@lit.com 3 USA
brin@yahoo.com 3 USA
brin@yahoo.com 2 Canada
admin@gmail.com 2 Canada
admin@gmail.com 3 USA
run;
data PICKED(drop=REASON) DROPPED;
  set HAVE;
  by EMAIL ;
  length REASON $20;
  retain REASON;
  if first.EMAIL then do;
    REASON='Already in '||ORIGIN;
    output PICKED;
  end;
  else output DROPPED;
run;
EMAIL RANK ORIGIN
admin@gmail.com 2 Canada
brin@yahoo.com 2 Canada
llp@lit.com 1 Europe

 

 

EMAIL RANK ORIGIN REASON
admin@gmail.com 3 USA Already in Canada
brin@yahoo.com 3 USA Already in Canada
llp@lit.com 2 Canada Already in Europe
llp@lit.com 3 USA Already in Europe

 

View solution in original post


All Replies
Solution
‎04-05-2018 02:32 AM
PROC Star
Posts: 2,370

Re: How do i do complex by group processing?

[ Edited ]
Posted in reply to ClareRobin1

Like this?

 

data HAVE(index=(A=(EMAIL RANK )));
  length EMAIL $18 ;
  input EMAIL $ RANK ORIGIN $;
datalines;
llp@lit.com 2 Canada
llp@lit.com 1 Europe
llp@lit.com 3 USA
brin@yahoo.com 3 USA
brin@yahoo.com 2 Canada
admin@gmail.com 2 Canada
admin@gmail.com 3 USA
run;
data PICKED(drop=REASON) DROPPED;
  set HAVE;
  by EMAIL ;
  length REASON $20;
  retain REASON;
  if first.EMAIL then do;
    REASON='Already in '||ORIGIN;
    output PICKED;
  end;
  else output DROPPED;
run;
EMAIL RANK ORIGIN
admin@gmail.com 2 Canada
brin@yahoo.com 2 Canada
llp@lit.com 1 Europe

 

 

EMAIL RANK ORIGIN REASON
admin@gmail.com 3 USA Already in Canada
brin@yahoo.com 3 USA Already in Canada
llp@lit.com 2 Canada Already in Europe
llp@lit.com 3 USA Already in Europe

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 124 views
  • 1 like
  • 2 in conversation