Kelly
Kelly

Reputation: 145

Create dummy variable in sas to indicate first time an observation appears

I have a data set that I have sorted by email address and date. This data set contains information about users who have utilized a service multiple times. I want to flag the first time a user (designated by email address) has first appeared with a flag =1 and all others as flag = 0. I have attempted to sort out the first appearance and then re-merge back into the data set but this was unsuccessful (and seems like far too many steps)

proc sort data = DB nodupkey out = db1;
by emailaddress date;
run;

proc sort data = db1;
by emailaddress date;
run;

data db2;
set db1;
obs = 1 ;
run;

data db3;
merge db2 db1;
by emailaddress date;
run; 

Thank you!!

Upvotes: 3

Views: 171

Answers (1)

Reeza
Reeza

Reputation: 21274

You can use FIRST for this.

proc sort data=db; 
by emailAddress Date;

data db1;
   set db;
   by emailaddress date;
   flag = first.emailAddress;
run;

Upvotes: 2

Related Questions