Solved
Contributor
Posts: 23

# Year dummy variables

Hi,

I am tring to create year dummy variables.

this is my code.

I am keep getting an error message below. Do you know what is wrong with my code?

ERROR: Array subscript out of range at line 284 column 10.

Date X;

set X;

array yrdummy(i) 8 dy2003 - dy2011;

do i=2003 to 2011;

if datayear = i then yrdummy = 1;

else yrdummy = 0;

end;

run;

Accepted Solutions
Solution
‎10-14-2014 05:37 PM
Super User
Posts: 23,689

## Re: Year dummy variables

Since you can initialize an array to 0 at the beginning something like this may work as well:

Data X;

set X;

array dy(2003:2011) dy2003 - dy2011 (9*0);

dy(datayear)=1;

run;

All Replies
Posts: 1,270

## Re: Year dummy variables

Data X;

set X;

array yrdummy(*) dy2003 - dy2011;

do i=2003 to 2011;

if datayear = i then yrdummy{i} = 1;

else yrdummy{i} = 0;

end;

run;

Contributor
Posts: 23

## Re: Year dummy variables

I am still getting the same error message

ERROR: Array subscript out of range at line 300 column 10.

data regressionsample_fixedeffects;

array yrdummy(*) dy2003 - dy2011;

do i=2003 to 2011;

if datayear = i then yrdummy{i} = 1;

else yrdummy{i} = 0;

end;

run;

Posts: 1,270

## Re: Year dummy variables

This is happening becasue yrdummy{i} will become yrdummy{2003} after first iteration. Try this modified code

Data X;

set X;

array yrdummy(*) dy2003 - dy2011;

do i=1 to dim(yrdummy);

if datayear = i+2002 then yrdummy{i} = 1;

else yrdummy{i} = 0;

end;

run;

Contributor
Posts: 23

## Re: Year dummy variables

Thank you so much!!

Solution
‎10-14-2014 05:37 PM
Super User
Posts: 23,689

## Re: Year dummy variables

Since you can initialize an array to 0 at the beginning something like this may work as well:

Data X;

set X;

array dy(2003:2011) dy2003 - dy2011 (9*0);

dy(datayear)=1;

run;

🔒 This topic is solved and locked.