Hello,
I am having a lot of trouble creating several dummy-variables with two loops, and I have no idea how to do it.
The goal is to make dummy-variables from categorical variables recorded at specific times.
As of now (for 5 years with 5 categories), to do it manually it would be like:
data tmp;
set data1;
if y_1=0 then cat0_time_0=1; else cat0_time_0=0;
if y_2=0 then cat0_time_1=1; else cat0_time_1=0;
if y_3=0 then cat0_time_2=1; else cat0_time_2=0;
if y_4=0 then cat0_time_3=1; else cat0_time_3=0;
if y_5=0 then cat0_time_4=1; else cat0_time_4=0;
if y_1=1 then cat1_time_0=1; else cat1_time_0=0;
if y_2=1 then cat1_time_1=1; else cat1_time_1=0;
if y_3=1 then cat1_time_2=1; else cat1_time_2=0;
if y_4=1 then cat1_time_3=1; else cat1_time_3=0;
if y_5=1 then cat1_time_4=1; else cat1_time_4=0;
if y_1=2 then cat2_time_0=1; else cat2_time_0=0;
if y_2=2 then cat2_time_1=1; else cat2_time_1=0;
if y_3=2 then cat2_time_2=1; else cat2_time_2=0;
if y_4=2 then cat2_time_3=1; else cat2_time_3=0;
if y_5=2 then cat2_time_4=1; else cat2_time_4=0;
if y_1=3 then cat3_time_0=1; else cat3_time_0=0;
if y_2=3 then cat3_time_1=1; else cat3_time_1=0;
if y_3=3 then cat3_time_2=1; else cat3_time_2=0;
if y_4=3 then cat3_time_3=1; else cat3_time_3=0;
if y_5=3 then cat3_time_4=1; else cat3_time_4=0;
if y_1=4 then cat4_time_0=1; else cat4_time_0=0;
if y_2=4 then cat4_time_1=1; else cat4_time_1=0;
if y_3=4 then cat4_time_2=1; else cat4_time_2=0;
if y_4=4 then cat4_time_3=1; else cat4_time_3=0;
if y_5=4 then cat4_time_4=1; else cat4_time_4=0;
run;
y_3=4 means "Year 3 for category 4" and so on.
But I need to have it more macro-based, so that one only need to specify the maximum amount of categories and years. However, I just can't get it to work.
The "closest" I have gotten is through this attempt:
%MACRO time_categorical_to_binary(time_max=, number_cat=);
data tmp;
set data1;
%DO i = 0 %to &time_max;
%DO j = 0 %to &number_cat;
if y_&i.=1 then cat_&j._time_&i.=1; else cat_&j._time_&i.=0;
run;
%END;
%END;
%MEND time_categorical_to_binary;
%time_categorical_to_binary(time_max=4, number_cat=5);
But it seems like SAS doesn't like it when trying to create two indicators to the same variable.
Does anyone have any ideas how to make this doable?
I use SAS 9.4