3 users online | 3 Guests and 0 Registered

Can I delete observations from a dataset where all values are missing?


 Yes. 

A perfect example of a utility macro which retrieves the number of variables in the dataset, counts the number of missing character / numeric variables and compares the sum of these to the total number.

Consider the code:

%macro delallmiss(dsn) ;
  %local dsn dsid rc chars nums i ;

  %let dsid = %sysfunc(open(&dsn)) ;

  %let chars = 0 ;
  %let nums  = 0 ;

  %if &dsid %then
  %do ;
    %do i = 1 %to %sysfunc(attrn(&dsid,nvars)) ;
      %if %sysfunc(vartype(&dsid,&i)) = C %then %let chars = %eval(&chars + 1) ;
      %else %let nums = %eval(&nums + 1 ) ;
    %end ;

    sum(
        %if &chars ne 0                %then cmiss(of _character_) ;
        %if &chars ne 0 and &nums ne 0 %then ,                     ;
        %if &nums  ne 0                %then nmiss(of _numeric_)   ;
       ) = %sysfunc(attrn(&dsid.nvars))
  %end ;
  %else 
  %do ;
    0
  %end ;
%mend delallmiss ;

Calling the macro program:

data class ;
  set sashelp.class ;
    if %delallmiss(sashelp.class) then delete ;
run ;

The main points of the code involve OPENing the dataset to access the metadata and assign a unique numeric identifier %let dsid = %sysfunc(open(&dsn)) ; then if this process is successful, create a DO loop by retrieving the numeric attribute for the number of variables %sysfunc(attrn(&dsid,nvars)).

For each variable determine the variable type (C|N) %sysfunc(vartype(&dsid,&i)) and increment the corresponding variable count.

Build the returned value:

sum(<character count if non-zero>
     <, separator if both variable type counts are non-zero>  
     <numeric count if non-zero>
    ) = total numer of variables

If the dataset could not be successfully OPENed, then return a boolean 0 instead.

Author:
Alan D Rudland
Revision:
1.0
Average rating:0 (0 Votes)

You cannot comment on this entry

Chuck Norris has counted to infinity. Twice.

Records in this category

Tags