- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello all, here is what I have
| Year | VarValue |
|---|---|
| 2000 | 3 |
| 2000 | 5 |
| 2000 | |
| 2000 | 10 |
| 2001 | 7 |
| 2001 | 2 |
| 2002 | |
| 2002 | 9 |
| 2002 | |
| 2004 | 2 |
| 2004 |
I would like to impute the missing values of VarValue for each year with the minimum VarValue for that year. Something like this below. A datastep would be preferable as I do not know SQL (but I am not opposed to SQL as long as I can modify it easily). Thanks!!
| Year | VarValue |
|---|---|
| 2000 | 3 |
| 2000 | 5 |
| 2000 | 3 |
| 2000 | 10 |
| 2001 | 7 |
| 2001 | 2 |
| 2002 | 9 |
| 2002 | 9 |
| 2002 | 9 |
| 2004 | 2 |
| 2004 | 2 |
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Here's one fairly standard way to use a DATA step:
data want;
do until (last.year);
set have;
by year;
minimum_value = min(minimum_value, VarValue);
end;
do until (last.year);
set have;
by year;
if VarValue=. then VarValue = minimum_value;
output;
end;
drop minimum_value;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Here's one fairly standard way to use a DATA step:
data want;
do until (last.year);
set have;
by year;
minimum_value = min(minimum_value, VarValue);
end;
do until (last.year);
set have;
by year;
if VarValue=. then VarValue = minimum_value;
output;
end;
drop minimum_value;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Here's the SQL way.
proc sql;
create table want as
select a.year, a.varvalue, min(a.varvalue) as min, coalesce(a.varvalue, calculated min) as new_varValue
from want a
group by year;
quit;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you both