The input data looks like below:
| State | County | HH | Var |
| 1 | 1 | 1 | 1 |
| 1 | 1 | 2 | 2 |
| 1 | 1 | 3 | |
| 1 | 2 | 1 | 4 |
| 1 | 2 | 2 | 5 |
| 1 | 3 | 1 | 6 |
| 2 | 1 | 1 | 1 |
| 2 | 1 | 2 | 2 |
| 2 | 2 | 1 | 3 |
| 2 | 3 | 1 | 4 |
| 2 | 3 | 2 |
The set of 3 variables state, county, and households (HH) specifies unique codes for observations.
The variable Var is, say, a characteristic of households and it has missing data. My question is: How can I replace those missing observations with the mean values of households of the same county?
The output data should look like:
| State | County | HH | Var |
| 1 | 1 | 1 | 1 |
| 1 | 1 | 2 | 2 |
| 1 | 1 | 3 | 1.5 |
| 1 | 2 | 1 | 4 |
| 1 | 2 | 2 | 5 |
| 1 | 3 | 1 | 6 |
| 2 | 1 | 1 | 1 |
| 2 | 1 | 2 | 2 |
| 2 | 2 | 1 | 3 |
| 2 | 3 | 1 | 4 |
| 2 | 3 | 2 | 4 |
After some search, it seems quite straightforward with PROC STANDARD. Sorry for bringing this up, but for those who are as novice:
proc standard data=have replace;
var Var;
by state county;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.