Desktop productivity for business analysts and programmers

Replacing missing values with zeroes

Reply
N/A
Posts: 0

Replacing missing values with zeroes

Sounds easy, but I am not actually trying to replace the missing values with zeroes in a single column... I have some 250 columns representing fish species which I do not want to work on individually.

EG 4.1 can change column values in the query menu fairly easily, but I haven't found a way to automate this for all columns.

The Update statement in proc SQL can also easily change the missing values, but one variable at a time.

My question is: is it possible to batch proccess an entire dataset for this task? With some loop or special reference?

Thanks for your help!
SAS Super FREQ
Posts: 8,720

Re: Replacing missing values with zeroes

Marc:
Run this test program and see if it does what you want. It uses sashelp.class -- so you should be able to run it. You really only care about the array technique used in the second Data step program, but I needed to make up some missing data to test the program.
cynthia

[pre]
** make some data with missing values;
data newclass;
set sashelp.class;
if age = 13 then height = .;
else if age = 12 then weight = .;
else if age = 15 then do;
height = .;
weight = .;
end;
run;

proc print data=newclass;
title 'should have some missing';
run;

** For every observations, treat the numeric vars as though;
** they are in an array and use a do loop to set missing;
** to 0;
data fixmiss;
drop i;
set newclass;
array fix(*) _numeric_;
do i = 1 to dim(fix);
fix(i) = sum(fix(i),0);
end;
run;

proc print data=fixmiss;
title 'missing should be fixed';
run;
[/pre]
N/A
Posts: 0

Re: Replacing missing values with zeroes

That worked great!

Many thanks!

Marc
N/A
Posts: 0

Re: Replacing missing values with zeroes

Does anybody know if there any way to replace the missing values with zeroes for many columns WITHOUT USING A DATA STEP?
Thanks
Frequent Contributor
Posts: 81

Re: Replacing missing values with zeroes

Try:
OPTIONS MISSING = '0';

This doesn't change the missing values, but displays them as '0'.

........Phil Holland
N/A
Posts: 1

Re: Replacing missing values with zeroes

Is it possible to replace the missing values with zeroes only on variables ending with "_q" ?
Thanks in advance.
SAS Employee
Posts: 149

Re: Replacing missing values with zeroes

SAS data step code makes it easy to reference name prefix lists (e.g. all variables starting with q_) but unfortunately not what you're describing with a common suffix. In order to do that, I would write some SAS code to create a list of variable names as the value for a macro variable, then reference that macro variable in data step code similar to what Cynthia showed above.

Run the following code, then use the final data step shown by Cynthia. However, in that code, replace _numeric_ on the ARRAY statement with &qlist (the name of the macro variable we created). That should do the trick.

/* Create test data set */
data work.test1;
set sashelp.class;
if mod(_N_,2) = 1 then height=.;
/* Every second height missing */
if mod(_N_,5) = 1 then age=.;
/* Every fifth age missing */
rename height=height_q age=age_q;
run;

proc sql noprint;
select name
into : qlist separated by ", "
from dictionary.columns
where libname="WORK"
and memname="TEST1"
and upcase(name) like '%_Q';
quit;

%put &qlist;
Ask a Question
Discussion stats
  • 6 replies
  • 206 views
  • 1 like
  • 5 in conversation