UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

SAS Code Fragments
Imputing the median

Say that you wanted to impute the median of "x" when x is missing. 

* First we make a little data file;
data test;
  input x;
cards;
1
2
3
.
4
5
6
7
.
8
9
10
;
run;
* Here we compute the median and save it into a data file using ;
* ods output, see How to use the ODS system for more information on this;
proc univariate data=test;
  var x;
  ods output BasicMeasures=basic;
run;

proc print data=basic;
run;

* We subset the data file to just have "locvalue" for the median;
data basic2;
  set basic;
  if LocMeasure="Median" then output;
  keep LocValue;
run;

proc print data=basic2;
run;
* This data step allows us to impute the median when "x" was missing;
data test2;
  retain LocValue;
  set test;
  if _N_ = 1 then set basic2 ; 
  ximp = x;
  if ximp = . then ximp = locvalue;
run;

proc print data=test2;
run;

How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.