BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hugo_1_
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
DBailey
Lapis Lazuli | Level 10

I removed what may be an extra underscore in cat&i....but have you tried this:

 

%MACRO time_categorical_to_binary(time_max=, number_cat=);
%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; 
%END;
%END;
%MEND time_categorical_to_binary;


data tmp;
set data1;
%time_categorical_to_binary(time_max=4, number_cat=5);
run;

 

View solution in original post

3 REPLIES 3
DBailey
Lapis Lazuli | Level 10

I removed what may be an extra underscore in cat&i....but have you tried this:

 

%MACRO time_categorical_to_binary(time_max=, number_cat=);
%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; 
%END;
%END;
%MEND time_categorical_to_binary;


data tmp;
set data1;
%time_categorical_to_binary(time_max=4, number_cat=5);
run;

 

Hugo_1_
Calcite | Level 5

It worked! Thank you!

I also noticed that I had missed to have

if y_&i.=&j. then cat&j._time_&i.=1; else cat&j._time_&i.=0;

instead of

if y_&I.=1 then cat&j._time_&i.=1; else cat&j._time_&i.=0;

But that was easily adjusted. Now everything works perfectly!

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1035 views
  • 0 likes
  • 3 in conversation