top of page
Search

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\indicators.sas’;        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\indicators.sas’ 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\indicators.sas’ MOD;    put ‘run;’;  run;  %include ‘C:\PATH_TO_GENERATED_SAS_PROGRAM\indicators.sas’; 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;

quit;

data _NULL_;

file ‘C:\indicators.sas’;

put ‘data temp_data;’;

put ‘set &dataset;’;

run;

data _NULL_;

set levels_data_set_name;

file ‘C:\indicators.sas’ MOD;

length char_var $256;

char_var=’ind’||strip(indicator_name)||’=’;

put char_var;

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

put char_var;

run;

data _NULL_;

file ‘C:\indicators.sas’ MOD;

put ‘run;’;

run;


%include ‘C:\indicators.sas’;


data &dataset;

set temp_data;

run;

%mend;

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...

Comments


bottom of page