Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I created a macro that will check for empty character columns and either remove them from the original or create a new data set with the empty columns removed. It takes two optional arguments: The name of the data set (default is the most recently created data set), and a suffix to name the new copy (set suffix to nothing to edit the original).</p> <p>It uses proc freq with the levels option and a custom format to determine the empty character columns. proc sql is then used to create a list of the columns to be removed and store them in a macro variable.</p> <p>Here is the macro:</p> <pre><code>%macro delemptycol(ds=_last_, suffix=_noempty); option nonotes; proc format; value $charmiss ' '= ' ' other='1'; run; %if "&amp;ds"="_last_" %then %let ds=&amp;syslast.; ods select nlevels; ods output nlevels=nlev; proc freq data=&amp;ds.(keep=_character_) levels ; format _character_ $charmiss.; run; ods output close; /* create macro var with list of cols to remove */ %local emptycols; proc sql noprint; select tablevar into: emptycols separated by ' ' from nlev where NNonMissLevels=0; quit; %if &amp;emptycols.= %then %do; %put DELEMPTYCOL: No empty character columns were found in data set &amp;ds.; %end; %else %do; %put DELEMPTYCOL: The following empty character columns were found in data set &amp;ds. : &amp;emptycols.; %put DELEMPTYCOL: Data set &amp;ds.&amp;suffix created with empty columns removed; data &amp;ds.&amp;suffix. ; set &amp;ds(drop=&amp;emptycols); run; %end; options notes; %mend; </code></pre> <p>Examples usage:</p> <pre><code>/* create some fake data: Here char5 will be empty */ data chardata(drop= j randnum); length char1-char5 $8.; array chars(5) char1-char5; do i=1 to 100; call missing(of char:); randnum=floor(10*ranuni(i)); do j=2 to 5; if (j-1)&lt;randnum&lt;=(j+1) then chars(j-1)="FOO"; end; output; end; run; %delemptycol(); /* uses default _last_ for the data and "_noempty" as the suffix */ %delemptycol(ds=chardata, suffix=); /* removes the empty columns from the original */ </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload