BookmarkSubscribeRSS Feed
HEB1
Calcite | Level 5

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! 

2 REPLIES 2
Astounding
PROC Star

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.

svh
Lapis Lazuli | Level 10 svh
Lapis Lazuli | Level 10
Spoiler alert: It does work.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 624 views
  • 1 like
  • 3 in conversation