Select character variables that have all missing values

前端 未结 4 2146
一整个雨季
一整个雨季 2021-01-19 05:08

I have a SAS dataset with around 3,000 variables, and I would like to get rid of the character variables for which all values are missing. I know how to do this for numeric

4条回答
  •  无人共我
    2021-01-19 05:53

    Rob and cmjohns, thank you SO MUCH for your help. Based on your solutions and an idea I had over the weekend, here is what I came up with:

    %macro removeEmptyCols(origDset, outDset);
        * get the number of obs in the original dset;
        %let dsid  = %sysfunc(open(&origDset));
        %let origN = %sysfunc(attrn(&dsid, nlobs));
        %let rc    = %sysfunc(close(&dsid));
    
        proc transpose data= &origDset out= transpDset;
            var _all_;
        run;
    
        data transpDset;
            set transpDset;
            * proc transpose converted all old vars to character,
              so the . from old numeric vars no longer means 'missing';
            array oldVar_ _character_;
            do over oldVar_;
                if strip(oldVar_) = "." then oldVar_ = "";
            end;
    
            * each row from the old dset is now a column with varname starting with 'col';
            numMiss = cmiss(of col:);
    
            numCols = &origN;
        run;
    
        proc sql noprint;
          select _NAME_ into: varsToKeep separated by ' '
          from transpDset
          where numMiss < numCols;
        quit;
    
        data &outDset;
            set &origDset (keep = &varsToKeep);
        run;
    %mend removeEmptyCols;
    

    I will try all 3 ways and report back on which one is fastest...

    P.S. added 23 Dec 2010 for future reference: SGF Paper 048-2010: Dropping Automatically Variables with Only Missing Values

提交回复
热议问题