Year dummy variables

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

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
Grand Advisor
Posts: 17,462

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;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,203

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;

set regressionsample_addvariables;

array yrdummy(*) dy2003 - dy2011;

do i=2003 to 2011;

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

else yrdummy{i} = 0;

end;

run;

Trusted Advisor
Posts: 1,203

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
Grand Advisor
Posts: 17,462

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 352 views
  • 3 likes
  • 3 in conversation