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
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.