I have following data:
wei 01feb2018 car
wei 02feb2018 car
wei 02mar2019 bike
carlin 01feb2018 car
carlin 05feb2018 bike
carlin 07mar2018 bike
carlin 01mar2019 car
I want to identify new and continue customers, if a customer had no purchase in last 12 months then it will become a new customer. Required output be like
wei 01feb2018 car new
wei 02feb2018 car cont.
wei 02mar2019 bike new
carlin 01feb2018 car new
carlin 05feb2018 bike cont.
carlin 07mar2018 bike cont.
carlin 01mar2019 car new
Now if a customer has purchased any item in the same month for ex -customer a purchased car on 01jan and bike on 15jan then I want two classify customer a as new for Jan for one report and in another report I want customer a as both new and continue.
I'm trying but not getting the logic -
proc sql;
select a.*,(select count(name) from t where intnx("month",-12,a.date) >= 356)
as tot
from t a;
Quit;
You can use retain
:
proc sort data=test out=test2;
by name type date;
run;
data test2 ;
set test2;
retain retain 'new';
by name type date;
if first.type then retain='new';
else retain='con';
run;
proc sort data=test2 out=test2;
by name date;
run;
Output:
+--------+-----------+------+--------+
| name | date | type | retain |
+--------+-----------+------+--------+
| carlin | 01FEB2018 | car | new |
| carlin | 05FEB2018 | bike | new |
| carlin | 01MAR2019 | car | con |
| wei | 01FEB2018 | car | new |
| wei | 02FEB2018 | car | con |
| wei | 02MAR2019 | bike | new |
+--------+-----------+------+--------+
You appear to want two different 'status' variables, one for continuity over prior year and one for continuity within month.
In SQL an existential reflexive correlated sub-query result can be a case test for rows meeting the over and within criteria. Date arithmetic is used to compute days apart and INTCK
is used to compute months apart:
data have; input
customer $ date& date9. item& $; format date date9.; datalines;
wei 01feb2018 car
wei 02feb2018 car
wei 02mar2019 bike
carlin 01feb2018 car
carlin 05feb2018 bike
carlin 07mar2018 bike
carlin 01mar2019 car
run;
proc sql;
create table want as
select *,
case
when exists
(
select * from have as inner
where inner.customer=outer.customer
and (outer.date - inner.date) between 1 and 365
)
then 'cont.'
else 'new'
end as status_year,
case
when exists
(
select * from have as inner
where inner.customer=outer.customer
and outer.date > inner.date
and intck ('month', outer.date, inner.date) = 0
)
then 'cont.'
else 'new'
end as status_month
from have as outer
;
quit;