Search code examples
macrossasretainlocf

Populate fields in SAS by ID and Date


My dataset looks like this:

AsAtDate  ReleaseDate ID Var1 Var2 ... Var5
200701     200601      1   x    .    
200702     200601      1   .    a
200703     200601      1   .    .
200701     200702      2   .    b 
200702     200702      2   y    b
200703     200702      2   y    .
200702     200501      3   z    .
200703     200501      3   .    .

I want my results to look like this:

AsAtDate  ReleaseDate ID Var1 Var2 ... Var5
200701     200601      1   x    a    
200702     200601      1   x    a
200703     200601      1   x    a
200701     200702      2   .    . 
200702     200702      2   y    b
200703     200702      2   y    b
200702     200501      3   z    .
200703     200501      3   z    .

How do I get my data to consider the ReleaseDate as in, if ReleaseDate is earlier than AsAtDate then populate the fields by ID.


Solution

  • Building on your solution, we can add a CASE statement to conditionally fill in the data as needed.

    proc sql;
    create table want as
    select *, 
           case when ReleaseDate>AsAtDate then " " 
           else max(Var1) 
           end as _Var1
    from have
    group by ID
    order by ID, AsAtdate;
    quit;