BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
HEB1
Calcite | Level 5

Hi

my data looks like that: 

id year FD REPORTING
a 1999 0 0
a 2000 0 0
a 2001 1 0
a 2002 1 0
a 2003 0 1
a 2004 0 1
a 2005 0 1
b 2001 0 0
b 2002 0 0
b 2003 0 0
b 2004 0 1
b 2005 0 1
c 1998 0 0
c 1999 0 0
c 2000 1 0
c 2001 0 0
c 2002 0 0

I want to create new variables that equal 1 only for the first reporting=1 and first fd=1. Meaning- I want my data to look like that: 

id year FD REPORTING NEW_FD NEW_REPORTING
a 1999 0 0 0 0
a 2000 0 0 0 0
a 2001 1 0 1 0
a 2002 1 0 0 0
a 2003 0 1 0 1
a 2004 0 1 0 0
a 2005 0 1 0 0
b 2001 0 0 0 0
b 2002 0 0 0 0
b 2003 0 0 0 0
b 2004 0 1 0 1
b 2005 0 1 0 0
c 1998 0 0 0 0
c 1999 0 0 0 0
c 2000 1 0 1 0
c 2001 0 0 0 0
c 2002 0 0 0 0

Thanks! 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

It is easier if you post your example data as a datastep, e.g. like this:

data have;                                                                                                                              
input id $ year FD REPORTING;                                                                                                           
cards;                                                                                                                                  
a 1999 0 0                                                                                                                              
a 2000 0 0                                                                                                                              
a 2001 1 0                                                                                                                              
a 2002 1 0                                                                                                                              
a 2003 0 1                                                                                                                              
a 2004 0 1                                                                                                                              
a 2005 0 1                                                                                                                              
b 2001 0 0                                                                                                                              
b 2002 0 0                                                                                                                              
b 2003 0 0                                                                                                                              
b 2004 0 1                                                                                                                              
b 2005 0 1                                                                                                                              
c 1998 0 0                                                                                                                              
c 1999 0 0                                                                                                                              
c 2000 1 0                                                                                                                              
c 2001 0 0                                                                                                                              
c 2002 0 0                                                                                                                              
;run;              

I think you can get what you want like this:

data want;                                                                                                                              
  _FD=0;                                                                                                                                
  _report=0;                                                                                                                            
  do until(last.id);                                                                                                                    
    set have;                                                                                                                           
    by id;                                                                                                                              
    if fd and not _fd then do;                                                                                                          
      NEW_FD=1;                                                                                                                         
      _FD=1;                                                                                                                            
      end;                                                                                                                              
    else NEW_FD=0;                                                                                                                      
    if reporting and not _report then do;                                                                                               
      NEW_REPORTING=1;                                                                                                                  
      _report=1;                                                                                                                        
      end;                                                                                                                              
    else NEW_REPORTING=0;                                                                                                               
    output;                                                                                                                             
    end;                                                                                                                                
drop _:;                                                                                                                                
run;        

View solution in original post

2 REPLIES 2
s_lassen
Meteorite | Level 14

It is easier if you post your example data as a datastep, e.g. like this:

data have;                                                                                                                              
input id $ year FD REPORTING;                                                                                                           
cards;                                                                                                                                  
a 1999 0 0                                                                                                                              
a 2000 0 0                                                                                                                              
a 2001 1 0                                                                                                                              
a 2002 1 0                                                                                                                              
a 2003 0 1                                                                                                                              
a 2004 0 1                                                                                                                              
a 2005 0 1                                                                                                                              
b 2001 0 0                                                                                                                              
b 2002 0 0                                                                                                                              
b 2003 0 0                                                                                                                              
b 2004 0 1                                                                                                                              
b 2005 0 1                                                                                                                              
c 1998 0 0                                                                                                                              
c 1999 0 0                                                                                                                              
c 2000 1 0                                                                                                                              
c 2001 0 0                                                                                                                              
c 2002 0 0                                                                                                                              
;run;              

I think you can get what you want like this:

data want;                                                                                                                              
  _FD=0;                                                                                                                                
  _report=0;                                                                                                                            
  do until(last.id);                                                                                                                    
    set have;                                                                                                                           
    by id;                                                                                                                              
    if fd and not _fd then do;                                                                                                          
      NEW_FD=1;                                                                                                                         
      _FD=1;                                                                                                                            
      end;                                                                                                                              
    else NEW_FD=0;                                                                                                                      
    if reporting and not _report then do;                                                                                               
      NEW_REPORTING=1;                                                                                                                  
      _report=1;                                                                                                                        
      end;                                                                                                                              
    else NEW_REPORTING=0;                                                                                                               
    output;                                                                                                                             
    end;                                                                                                                                
drop _:;                                                                                                                                
run;        
HEB1
Calcite | Level 5
It works great! Many thanks 😉

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 646 views
  • 0 likes
  • 2 in conversation