top of page

Generate indicators in SAS based on factor variables

Generating indicators from factor variable defined in SAS datafile using a generic procedure in three steps. The code is easy to modify to suite more complicated needs.

  1. Produce table with factor levels

  2. Use data step to generate program

  3. Append indicators by running SAS data step using %include

An example of generative programming…

proc sql;    create table levels_data_set_name as    select distinct factor_variable_name as indicator_name    from input_data_set_name;  quit;  data _NULL_;    file ‘C:\PATH_TO_GENERATED_SAS_PROGRAM\’;        put ‘data somelib.indicator_enriched_data_set;’;    put ‘set input_data_set_name;’;  run;  data _NULL_;  set levels_data_set_name;    file ‘C:\PATH_TO_GENERATED_SAS_PROGRAM\’ MOD;    length char_var $256;    char_var=’ind’||strip(indicator_name)||’=’;    put char_var;    char_var='(‘||strip(factor_variable_name)||’ EQ ‘||strip(indicator_name)||'”);’;    put char_var;  run;  data _NULL_;    file ‘C:\PATH_TO_GENERATED_SAS_PROGRAM\’ MOD;    put ‘run;’;  run;  %include ‘C:\PATH_TO_GENERATED_SAS_PROGRAM\’; Fetch the code above as SAS program file: GenerateIndicators.SAS

I is also possible to make a simple macro using the code above (ensure you have write access to C:\)

%macro factor2indicators(factor,dataset);

proc sql;

create table levels_data_set_name as 

  select distinct &factor as indicator_name

  from &dataset;


data _NULL_;

file ‘C:\’;

put ‘data temp_data;’;

put ‘set &dataset;’;


data _NULL_;

set levels_data_set_name;

file ‘C:\’ MOD;

length char_var $256;


put char_var;

char_var='(‘||strip(&factor)||’ EQ “‘||strip(indicator_name)||'”);’;

put char_var;


data _NULL_;

file ‘C:\’ MOD;

put ‘run;’;


%include ‘C:\’;

data &dataset;

set temp_data;



0 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 experience, I have a tendency to analyze programmatic problems before


bottom of page