DATA Step, Macro, Functions and more

replace missing value with mean

Reply
Contributor
Posts: 26

replace missing value with mean

Hi I need to replace all missing values in a dataset with respective mean.

Find the sample i wrote.

%macro replace_missing(dataset);

      proc means data=&dataset mean ; ;

            output out=t (drop=_type_ _freq_) mean=/autoname;

    run;

      proc transpose data=t out=tr;

      run;

data _null_;

      set Tr end=last ;

      call symput('mean'||left(_n_),col1);

      if last then call symput('maxi', _n_);

            /* x=left(_N_) */;

            /* put _N_ maxi last x ; */;

run;

data &dataset;

      set &dataset;

      array num(*) _numeric_;

            %do n=1 %to &maxi;

                  if num(&n)=. then num(&n)= &&mean&n;

            %end;

run;

%mend;

%replace_missing(drop_missing);

run;

But this is not working..

Please help

SAS Super FREQ
Posts: 709

Re: replace missing value with mean

Posted in reply to supersonic

supersonic

Proc STANDARD will do this for you, see code sample below.

data someTest;
  infile cards dlm=",";
 
input
    v1
    v2
  ;
cards;
1,100
2,.
3,3
4,.
.,20
;

proc standard data=someTest out=sometest2 replace print;
run;

proc print data=someTest;
run;

proc print data=someTest2;
run;

proc means data=someTest2;
  var v:;
run;
Contributor
Posts: 45

Re: replace missing value with mean

Posted in reply to supersonic

Works well with this dataset:

data one;

input thing;

datalines;

100

101

.

103

104

105

;

Can you give the log or the dataset you tried to use it with?

Contributor
Posts: 26

Re: replace missing value with mean

Posted in reply to Murray_Court

Hi Murray

Its a very huge dataset.

My aim is to replace all the missing values with the corresponding mean value

Super User
Posts: 5,438

Re: replace missing value with mean

Posted in reply to supersonic

As stated by https://communities.sas.com/people/Murray_Court, attach a log or describe in more detail what it is that does not work...

Data never sleeps
Trusted Advisor
Posts: 3,215

Re: replace missing value with mean

Posted in reply to supersonic

Base SAS(R) 9.4 Procedures Guide (PROC standard replacing missing by mean) == Bruno-s  ==

The advantage of this procedure is it is part of SAS/Base Procs

If it should be more advanced like being in the Eminer processing, That one is using

SAS/STAT(R) 12.3 User's Guide (Proc MI) .

You nee to be licnesed for that. 

---->-- ja karman --<-----
Contributor
Posts: 32

Re: replace missing value with mean

Posted in reply to supersonic

proc stdize data=old out=new missing=mean reponly;
  var _numeric_;
run;


You can also calculate the median first, then read values from a second data set

Ask a Question
Discussion stats
  • 6 replies
  • 5071 views
  • 1 like
  • 6 in conversation