'SAS: assign a value to a variable based on characteristics of other variables
I'd like to assign to an empty field a value based on many values of other entries. Here my dataset:
input ID date $10. type typea $10. ;
  datalines;
 1 10/11/2006   1    a     
 2 10/12/2006   2    a     
 2 .            2    b     
 3 20/01/2007   5    p    
 4 .            1    r    
 5 11/09/2008   1    ca    
 5 .            1    cb     
 5 .            2    b    
 
 ;
 run;
My goal is the following: for all empty entries of the variable "date", assign to it the same date of the record which has the same ID, the same type, but a different typea. If there aren't other records with the criteria described, leave the date field empty. So the output should be:
  data temp;
input ID date $10. type typea $10.;
  datalines;
 1 10/11/2006   1    a 
 2 10/12/2006   2    a 
 2 10/12/2006   2    b  
 3 20/01/2007   5    p
 4 .            1    r
 5 11/09/2008   1    ca
 5 11/09/2008   1    cb 
 5 .            2    b 
 
 ;
 run;
I tried with something like that based on another answer on SO (SAS: get the first value where a condition is verified by group), but it doesn't work:
by ID type typea ;
run;
  data temp;
     set temp;
     by ID type typea ;
  
     if cat(first.ID, first.type, first.typea) then  date_store=date;
     if cat(ID eq ID and type ne type and typea eq typea)  then do;
      date_change_type1to2=date_store;
    
     end;
  run;
Do you have any hints? Thanks a lot!
Solution 1:[1]
You could use UPDATE statement to help you carry-forward the DATE values for a group.
data have;
  input ID type typea :$10. date :yymmdd.  ;
  format date yymmdd10.;
datalines;
1 1 a  2006-11-10
2 2 a  2006-12-10
2 2 b  .
3 5 p  2007-01-20
4 1 r  .
5 1 ca 2008-09-11
5 1 cb .
5 2 b  .
;
data want;
  update have(obs=0) have;
  by id type ;
  output;
run;
If there are also missing values of TYPEA then those will also be carried forward. If you don't want that to happen you could re-read just those variables after the update.
data want;
  update have(obs=0) have;
  by id type ;
  set have(keep=typea);
  output;
run;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | Tom | 
