7

How do you replace all missing values with zeroes in SAS? I have a text file that I dump into SAS to process some geo data, but whenever it has a missing value it breaks the operations. Is there a way to change this without specifying each field? I have over 200.

The way I do so is:

data geo_cali_north;
set geo_cali_north;
if polar_data eq . then 0;
if lat_xvar eq . then 0;
run;

How can I avoid doing this for every field?

Carey Gregory
  • 6,836
  • 2
  • 26
  • 47

2 Answers2

17

You can set all the missing values to 0 with like this:

data myData;
set myData;
array a(*) _numeric_;
do i=1 to dim(a);
if a(i) = . then a(i) = 0;
end;
drop i;

This will convert any numeric "." to a 0

isJustMe
  • 5,452
  • 2
  • 31
  • 47
  • 1
    no problem :) if you find it useful please consider upvoting and accepting this question so it remains useful for other users. – isJustMe Jun 01 '13 at 22:06
  • 2
    I'd suggest a slight modification; instead of `if a(i)-....` a better solution is `a[i]=coalesce(a[i],0);`; that will work for special missings (.A .B .Z etc.) in addition to . missing. (The MISSING function would also do that, this is somewhat faster I believe.) Coalesce returns the first nonmissing value from a list of values. – Joe Jun 02 '13 at 00:36
9

Another option:

proc stdize data=mydata reponly missing=0 out=newdata;
var _numeric_;
run;

If you have SAS/STAT, probably faster than the datastep option for large datasets.

Joe
  • 62,789
  • 6
  • 49
  • 67