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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.