Skip to main content

Posts

Showing posts from December, 2019

Indicators from numeric factor variables

The macro call below will generate indicators for all factor levels of xfaktor from the data set regdata represented by at least 5 observations. I have developed the macroes to avoid class variables in SAS regression analyses.

%indicator(xfaktor,regdata,5);

/* Macro to generate dummy variables. */
%MACRO GETCON;
  %DO I = 1 %TO &N;
  %IF &&M&I NE 0 %THEN %PUT &factor&I;
  %IF &&M&I = 0 %THEN %GOTO OUT;
    IF &factor = &&M&I THEN &factor&I = 1;
    ELSE &factor&I = 0;
  %OUT: %END;
%MEND GETCON;

%macro indicator(factor,dataset,size);
data tbl_data_;
set &dataset(keep=&factor);
run;
proc sort data=tbl_data_; by &factor; run;
data tbl_;
set tbl_data_;
by &factor;
retain sz;
if first.&factor then sz=1;
else sz=sz+1;
if last.&factor then output;
run;
PROC SORT DATA=tbl_(where=(sz GE &size)) OUT=UNIQUE NODUPKEY;
  BY &factor;
RUN;
/* Assign the largest value of CON to the macro variable N. */
DATA _NULL_;
  SET UNIQUE E…