Hi, I have a data set that tracks unique person identifier, and number of activities they had on a website and call center per YearMonth. An example below, I have the first 4 variables. What I would like to do is flag the YearMonth where the FIRST event occurred and populate the variable with 1 and 0 otherwise. Also my actual data set has multiple years of data.
Have | Want | ||||
PersonID | YearMonth | Logons | Calls | FirstLogon | FirstCall |
A | 201701 | 0 | 5 | 0 | 1 |
A | 201702 | 0 | 0 | 0 | 0 |
A | 201703 | 0 | 0 | 0 | 0 |
A | 201704 | 1 | 0 | 1 | 0 |
A | 201705 | 5 | 1 | 0 | 0 |
A | 201706 | 0 | 0 | 0 | 0 |
A | 201707 | 0 | 0 | 0 | 0 |
A | 201708 | 2 | 0 | 0 | 0 |
A | 201709 | 1 | 0 | 0 | 0 |
A | 201710 | 0 | 2 | 0 | 0 |
A | 201711 | 2 | 0 | 0 | 0 |
A | 201712 | 1 | 0 | 0 | 0 |
data have;
input PersonID $ YearMonth Logons Calls;
datalines;
A 201701 0 5
A 201702 0 0
A 201703 0 0
A 201704 1 0
A 201705 5 1
A 201706 0 0
A 201707 0 0
A 201708 2 0
A 201709 1 0
A 201710 0 2
A 201711 2 0
A 201712 1 0
;
data want;
set have;
by PersonID;
retain _temp1 _temp2 ;
if first.personid then do;_temp1=0; _temp2=0;end;
if Logons and not(_temp1) then do; _temp1=1;FirstLogon=1;end;else FirstLogon=0;
if calls and not(_temp2) then do; _temp2=1;Firstcall=1;end;else firstcall=0;
drop _:;
run;
data have;
input PersonID $ YearMonth Logons Calls;
datalines;
A 201701 0 5
A 201702 0 0
A 201703 0 0
A 201704 1 0
A 201705 5 1
A 201706 0 0
A 201707 0 0
A 201708 2 0
A 201709 1 0
A 201710 0 2
A 201711 2 0
A 201712 1 0
;
data want;
set have;
by PersonID;
retain _temp1 _temp2 ;
if first.personid then do;_temp1=0; _temp2=0;end;
if Logons and not(_temp1) then do; _temp1=1;FirstLogon=1;end;else FirstLogon=0;
if calls and not(_temp2) then do; _temp2=1;Firstcall=1;end;else firstcall=0;
drop _:;
run;
worked perfectly. Thank you for your help!
Mike
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.