Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete blank observations in a data set in SAS

Tags:

sas

I want to delete ALL blank observations from a data set. I only know how to get rid of blanks from one variable:

data a;
set data(where=(var1 ne .)) ;
run;

Here I set a new data set without the blanks from var1. But how to do it, when I want to get rid of ALL the blanks in the whole data set?

Thanks in advance for your answers.

like image 927
user1626092 Avatar asked Jun 25 '13 07:06

user1626092


2 Answers

If you are attempting to get rid of rows where ALL variables are missing, it's quite easy:

/* Create an example with some or all columns missing */
data have;
set sashelp.class;
if _N_ in (2,5,8,13) then do;
  call missing(of _numeric_);
end;
if _N_ in (5,6,8,12) then do;
  call missing(of _character_);
end;
run;

/* This is the answer */
data want;
set have;
if compress(cats(of _all_),'.')=' ' then delete;
run;

Instead of the compress you could also use OPTIONS MISSING=' '; beforehand.

If you want to remove ALL Rows with ANY missing values, then you can use NMISS/CMISS functions.

data want;
set have;
if nmiss(of _numeric_) > 0 then delete;
run;

or

data want;
set have;
if nmiss(of _numeric_) + cmiss(of _character_) > 0 then delete;
run;

for all char+numeric variables.

like image 165
Joe Avatar answered Oct 25 '22 08:10

Joe


You can do something like this:

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

This will scan trough all the numeric variables and will delete the observation where it finds a missing value

like image 35
isJustMe Avatar answered Oct 25 '22 10:10

isJustMe