I have a datase order by decile bin. Some of the decile bin are missing. I know how to replace a missing value by the previous one, but I do not know how to replace a missing value by the next not missing value within the same group.
Could any one give me a suggestion?
Please show a smple of the data you have, and the data you want:
Your description of the problem does not match the sample data and output you provide: Apparently you want
For each SCORE group, you want to replace missing BIN values with
1. The next valid BIN value within the SCORE group, but also
2. Last valid BIN value carried forward for missing BIN value at the end of a SCORE group.
data have; input ID Score bin; datalines; 1 1 . . 2 1 0 3 1 0 4 1 0 5 1 0 6 1 0 7 1 . 8 2 . 9 2 1 10 2 1 11 2 1 12 2 . run; data want (drop=n i validbin); /* Set N to # of records until a non-missing BIN or end of score group */ do N=1 by 1 until (last.score or bin^=.); set have ; by score; retain validbin; if first.score then validbin=bin; else validbin=coalesce(bin,validbin); /*Keep the latest non-missing BIN */ end; /* Re-read the records and assign validbin */ do I=1 to N; set have; bin=validbin; end; run;
What if there are a series of missing value,what you gonna do ?
data have; input ID Score bin; datalines; 1 1 . . 2 1 0 3 1 0 4 1 0 5 1 0 6 1 0 7 1 . 8 2 . 9 2 1 10 2 1 11 2 1 12 2 . run; data temp; set have; by score notsorted; retain _bin; if first.score then call missing(_bin); if not missing(bin) then _bin=bin; drop bin; run; proc sort data=temp;by descending id;run; data want; set temp; by score notsorted; retain bin; if first.score then call missing(bin); if not missing(_bin) then bin=_bin; drop _bin; run; proc sort data=want;by id;run;
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.