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;
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).
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.
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).
Thanks!! This did exactly what I was looking for.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.