Hello
my data looks like that:
ID YEAR FD REPORTING
a 2000 1 0
a 2001 1 0
a 2002 0 0
a 2003 0 1
a 2004 0 1
a 2005 0 1
a 2006 0 1
b 1990 0 0
b 1991 0 1
b 1992 1 1
b 1993 1 1
b 1994 0 1
b 1995 0 1
c 2006 0 0
c 2007 0 0
c 2008 0 1
c 2009 0 1
I want to create a variable ( or 2 ) that equals 1 for the first year by ID that reporting=1. I also want a variable that equals 1 for the first year by ID that FD=1
So my data will look like that (if I will have 2 dummies):
ID YEAR FD REPORTING newv_for_reporting newv_for_FD
a 2000 1 0 0 1
a 2001 1 0 0 0
a 2002 0 0 0 0
a 2003 0 1 1 0
a 2004 0 1 0 0
a 2005 0 1 0 0
a 2006 0 1 0 0
b 1990 0 0 0 0
b 1991 0 1 1 0
b 1992 1 1 0 1
b 1993 1 1 0 0
b 1994 0 1 0 0
b 1995 0 1 0 0
c 2006 0 0 0 0
c 2007 0 0 0 0
c 2008 0 1 1 0
c 2009 0 1 0 0
THANK YOU!
It looks like your data is already sorted by ID. In that case, add a few more variables to complete the computations (then get rid of the ones not needed at the end):
data want;
set have;
by id;
if first.id then do;
flagged_fd=0;
flagged_reporting=0;
end;
retain flagged_fd flagged_reporting;
newv_for_fd=0;
newv_for_reporting=0;
if fd then do;
if flagged_fd=0 then newv_for_fd=1;
flagged_fd=1;
end;
if reporting then do;
if flagged_reporting=0 then newv_for_reporting=1;
flagged_reporting=1;
end;
drop flagged_fd flagged_reporting;
run;
It's convoluted. It's untested. But it looks like it should work.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.