SAS (Base SAS)+
data lab;
input usubjid $ hgb glu chol$ alb alt$ ast ldh$ crp;
datalines;
101 12.5 110 POS 4.2 NEG 21 . .
102 13.0 105 NEG 4.1 NEG 25 . .
103 . . . . . . . .
104 14.1 108 POS 4.5 NEG 20 . .
;
run;
data _null_;
set lab end=last;
array charvars {*} _character_;
array numvars {*} _numeric_;
length keep_chars keep_nums $32767;
retain keep_chars keep_nums;
do i = 1 to dim(charvars);
if charvars{i} ne '' then
keep_chars = catx(' ', keep_chars, vname(charvars{i}));
end;
do j = 1 to dim(numvars);
if numvars{j} ne . then
keep_nums = catx(' ', keep_nums, vname(numvars{j}));
end;
if last then
call symputx('keep_vars', catx(' ', keep_chars, keep_nums));
run;
data lab_clean;
set lab(keep=&keep_vars.);
run;- In SAS, the trick is to inspect every variable, collect the names of the ones with at least one non-missing value, and rebuild the dataset using a
KEEP=list. - A
data _null_;step walks the rows without producing output, using two arrays:array charvars {*} _character_;andarray numvars {*} _numeric_;. - Two
RETAINed accumulators (keep_chars,keep_nums) grow across rows; we usecatx(' ', ...)to append names as we discover non-missing values. vname(charvars{i})returns the original variable name associated with the i-th array element — exactly what a KEEP list needs.- On the last row (
set lab end=last;),call symputx('keep_vars', catx(' ', keep_chars, keep_nums));stores the final list into a macro variable. - A second DATA step then rebuilds the output:
data lab_clean; set lab(keep=&keep_vars.); run;. - We run and confirm LAB_CLEAN has the same rows as LAB but drops the fully-missing columns
ldhandcrp.