BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bharat_P
Fluorite | Level 6
proc sql;
  create table temp as
    select A.CNCPT_NM
         , A.TRRTRY_SHRT_NM
         , A.LMG_MEM_CARD_NUMBER
         , case when missing(B.REVENUE) then 'CHURN' end as FLG
    from PREV_CUST_LST A
      left join CURR_CUST_LST B on A.CNCPT_NM=B.CNCPT_NM and A.TRRTRY_SHRT_NM=B.TRRTRY_SHRT_NM and A.LMG_MEM_CARD_NUMBER=B.LMG_MEM_CARD_NUMBER
    ;

  create table CHURN_CUST as
    select CNCPT_NM
         , TRRTRY_SHRT_NM
         , count(case when FLG='CHURN' then LMG_MEM_CARD_NUMBER end) as CHURN_CUST
    from temp
    group by 1,2
    ;
  quit;

I have the basic idea of how hash coding works but struggling to resolve this with the case when missing part.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

And the whole operation should look like this:

proc sort data=prev_cust_lst;
by cncpt_nm trrtry_shrt_nm;
run;

data churn_cust (keep=cncpt_nm trrtry_shrt_nm churn_cust);
set prev_cust_lst;
by cncpt_nm trrtry_shrt_nm;
if _n_ = 1
then do;
  declare hash curr (dataset:"curr_cust_lst (keep=cncpt_nm trrtry_shrt_nm lmg_mem_card_number)");
  curr.definekey('cncpt_nm','trrtry_shrt_nm','lmg_mem_card_number');
  curr.definedone();
end;
if first.trrtry_shrt_nm then churn_cust = 0;
if curr.check() ne 0 then churn_cust + 1;
if last.trrtry_shrt_nm;
run;

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

This should look like

data temp;
set prev_cust_lst;
if _n_ = 1
then do;
  declare hash curr (dataset:"curr_cust_lst (keep=cncpt_nm trrtry_shrt_nm lmg_mem_card_number)");
  curr.definekey('cncpt_nm','trrtry_shrt_nm','lmg_mem_card_number');
  curr.definedone();
end;
if curr.check() ne 0 then flg = 'CHURN';
run;

For tested code, supply example data in usable form (data step with datalines).

JeanDo
Obsidian | Level 7

Hello,

You should try this code:

 

proc sql;
  create table temp as
    select A.CNCPT_NM
         , A.TRRTRY_SHRT_NM
         , A.LMG_MEM_CARD_NUMBER
         , case when missing(B.REVENUE) then 'CHURN' end as FLG
    from PREV_CUST_LST A
      left join CURR_CUST_LST B on A.CNCPT_NM=B.CNCPT_NM and A.TRRTRY_SHRT_NM=B.TRRTRY_SHRT_NM and A.LMG_MEM_CARD_NUMBER=B.LMG_MEM_CARD_NUMBER
    ;

  create table CHURN_CUST as
    select CNCPT_NM
         , TRRTRY_SHRT_NM
         , sum(ifn(FLG='CHURN',1,0)) as CHURN_CUST
    from temp
    group by 1,2
    ;
  quit;

Regards,

 

JD

Kurt_Bremser
Super User

And the whole operation should look like this:

proc sort data=prev_cust_lst;
by cncpt_nm trrtry_shrt_nm;
run;

data churn_cust (keep=cncpt_nm trrtry_shrt_nm churn_cust);
set prev_cust_lst;
by cncpt_nm trrtry_shrt_nm;
if _n_ = 1
then do;
  declare hash curr (dataset:"curr_cust_lst (keep=cncpt_nm trrtry_shrt_nm lmg_mem_card_number)");
  curr.definekey('cncpt_nm','trrtry_shrt_nm','lmg_mem_card_number');
  curr.definedone();
end;
if first.trrtry_shrt_nm then churn_cust = 0;
if curr.check() ne 0 then churn_cust + 1;
if last.trrtry_shrt_nm;
run;
Bharat_P
Fluorite | Level 6

You guys are unbelievably helpful. You guys make our worklife so easy.. thank you from the bottom of my heart

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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
  • 4 replies
  • 3125 views
  • 3 likes
  • 3 in conversation