top of page
Search

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 END=LAST;   IF LAST THEN CALL SYMPUT(‘N’, PUT(&factor, 8.)); RUN; /* Assign the initial value 0 to all macro variables. */ DATA _NULL_;   DO I = 1 TO &N;     CALL SYMPUT(‘M’||LEFT(PUT(I, 8.)), ‘0’);   END; RUN; /* Assign the value of CON to the corresponding macro variable. */ DATA _NULL_;   SET UNIQUE;   CALL SYMPUT(‘M’||LEFT(PUT(&factor, 8.)), PUT(&factor, 8.)); RUN; /* Create dummy variables. */ DATA &dataset;   SET &dataset;   %GETCON RUN; %mend;

2 views0 comments

Recent Posts

See All

dplyr or base R

dplyr and tidyverse are convenient frameworks for data management and technical analytic programming. With more than 25 years of R...

Comments


bottom of page