Skip to main content

Posts

Showing posts from August, 2017

Dummy variables in SAS

It is difficult to create indicator or dummy variables from factor variables in SAS programming language.

The macro below assumes an integer factor variable (factor levels with values 0, 1, 2, 3, ...) and no missing values or formats. Here is a way to ensure this simple assumption is fulfilled. Add a few lines to an appropriate data step:

if missing(factor) then factor=0; format factor; To convert a non-numeric factor variable to an integer factor variable create a format and construct an integer factor variable:

proc format library=WORK; value $factor 'firstlevel'='1' 'secondlevel'='2' ... 'finallevel'='n' ; run; data newdataset; set olddataset; newfactor=input(put(factor,$factor.),8); run; You are now able to define and run the macro.

/* Macro to generate dummy variables. */ %MACRO GETCON; %DO I = 1 %TO &N; %IF &&M&I = 0 %THEN %GOTO OUT; IF &factor = &&M&I THEN &factor&I = 1; ELS…