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

I have a list of customers and i need to check  

Want 

if customers labeled 'NEW' in a department where seen within 3 year of their visit this year (2018)

 

Have

 

Store    Dept    CustID         Date       Visit
Macys    Shoe   1245       17-Feb-13    NEW
Macys    Shoe   1245         25-Oct-15   OLD
Macys    Bags 1245 20-Jun-10 New
Macys Shoe 1245 4-Apr-18 NEW
Macys Bags 1245 2-Feb-11 OLD
Macys Makeup 1333 17-Aug-16 NEW
Macys Bags 1333 25-Sep-12 OLD
Macys Makeup 1333 21-Sep-18 New
Macys Shoe 1333 21-Sep-10 NEW
Macys Bags 1333 2-Feb-11 OLD
Nords Shoes 1444 17-Aug-09 NEW
Nords Shoes 1444 25-Sep-12 OLD
Nords Makeup 1444 21-Sep-18 New
Nords Bags 1555 21-Feb-10 OLD
Nords Shoe 1555 1-Oct-18 OLD

 

want


Store Dept CustID Date Visit Within 3 years
Macys Shoe 1245 4-Apr-18 NEW YES
Macys Makeup 1333 21-Sep-18 New YES
Nords Makeup 1444 21-Sep-18 New NO

 

 

a new column that tells me (yes or  no)  if a customer that is new this year was seen in the same dept within 3 years from there new visit this year  

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

UNTESTED CODE

Assumes your variable date is an actual SAS date value and not a character string.

 

proc sort data=have;
    by store dept custid date;
run;
data want;
    set have;
    length newcolumn $ 3;
    prev_date=lag(date);
    prev_custid=lag(custid);
    prev_dept=lag(dept);
    prev_store=lag(store);
    if year(date)=2018 and upcase(visit)='NEW' and prev_custid=custid 
        and prev_dept=dept and prev_store=store then do;
         if intck('year',prev_date,date,'c')<3 then newcolumn='YES';
         else newcolumn='NO';
         output;
    end;
    drop prev:;
run;
--
Paige Miller

View solution in original post

1 REPLY 1
PaigeMiller
Diamond | Level 26

UNTESTED CODE

Assumes your variable date is an actual SAS date value and not a character string.

 

proc sort data=have;
    by store dept custid date;
run;
data want;
    set have;
    length newcolumn $ 3;
    prev_date=lag(date);
    prev_custid=lag(custid);
    prev_dept=lag(dept);
    prev_store=lag(store);
    if year(date)=2018 and upcase(visit)='NEW' and prev_custid=custid 
        and prev_dept=dept and prev_store=store then do;
         if intck('year',prev_date,date,'c')<3 then newcolumn='YES';
         else newcolumn='NO';
         output;
    end;
    drop prev:;
run;
--
Paige Miller

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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