DATA Step, Macro, Functions and more

Delete Prior Record

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Delete Prior Record

I have a data set where I'm using the prior row to populate data in the subsequent row with missing data.  I then need to delete the prior row.  Any ideas on how to properly do this?

 

data research.master;

set research.prior;

 

prev_value = lag(value);

 

if id = lag(id) then do;

     if value = '' then value = prev_value;

 

    //I THEN NEED TO DELETE THE PRIOR ROW HERE

end;

run;


Accepted Solutions
Solution
‎06-22-2016 02:49 PM
Super User
Posts: 5,085

Re: Delete Prior Record

The UPDATE statement does what you are looking for:  take in multiple records per ID, keep track of the latest nonmissing values, and output one record per ID.  Assuming your data set is sorted by ID, here is how it could be applied here:

 

data research.master;

update research.prior (obs=0) research.prior;

by id;

run;

 

This assumes that research.master does not exist prior to this step.  If it does, the program would have to change slightly (unless research.master can be replaced without losing important data).

View solution in original post


All Replies
Super User
Super User
Posts: 7,407

Re: Delete Prior Record

You would be better off merging the base record to empty record and coalescing the data:

proc sql;
  create table WANT as
  select  COALESCE(A.VAR1,B.VAR1) as VAR1,
          COALESCE(A.VAR2,B.VAR2) as VAR2
  from    (select * from HAVE where RECORD=1) A
  left join (select * from HAVE where RECORD=2) B
  on      A.IDVAR=B.IDVAR;
quit;

As you have not presented test data ( in the form of a datastep) the above is purely an example and you will need ot modify to your data.

Solution
‎06-22-2016 02:49 PM
Super User
Posts: 5,085

Re: Delete Prior Record

The UPDATE statement does what you are looking for:  take in multiple records per ID, keep track of the latest nonmissing values, and output one record per ID.  Assuming your data set is sorted by ID, here is how it could be applied here:

 

data research.master;

update research.prior (obs=0) research.prior;

by id;

run;

 

This assumes that research.master does not exist prior to this step.  If it does, the program would have to change slightly (unless research.master can be replaced without losing important data).

Occasional Contributor
Posts: 7

Re: Delete Prior Record

Thanks!! This did exactly what I was looking for.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 309 views
  • 2 likes
  • 3 in conversation