DATA Step, Macro, Functions and more

Flag First Instance

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

Flag First Instance

[ Edited ]

 

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

Accepted Solutions
Solution
‎01-26-2018 03:07 PM
Super User
Posts: 2,061

Re: Flag First Instance

Posted in reply to mikemangini
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;

View solution in original post


All Replies
Solution
‎01-26-2018 03:07 PM
Super User
Posts: 2,061

Re: Flag First Instance

Posted in reply to mikemangini
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;
Contributor
Posts: 36

Re: Flag First Instance

Posted in reply to novinosrin

worked perfectly. Thank you for your help!

 

Mike

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 167 views
  • 0 likes
  • 2 in conversation