Trying to create dummy variables using arrays and I keep getting an 'array subscript out of range' error message. Can anyone help with what i'm doing wrong?
DATA WR16; SET final16;
Array race {10} R1_1-R1_10; Do i=1-10;
If race {i} = 'R1' then AIAN =1;
If race {i} = 'R2' then Asian =1;
If race {i} = 'R3' then Black =1;
If race {i} = 'R4' then NHPI =1;
If race {i} = 'R5' then White =1;
If race {i} = 'R9' then Other =1;
end;
run;
Correction: Do i=1 to 10;
DATA WR16; SET final16;
Array race {10} R1_1-R1_10;
Do i=1 to 10;
If race {i} = 'R1' then AIAN =1;
If race {i} = 'R2' then Asian =1;
If race {i} = 'R3' then Black =1;
If race {i} = 'R4' then NHPI =1;
If race {i} = 'R5' then White =1;
If race {i} = 'R9' then Other =1;
end;
run;
Correction: Do i=1 to 10;
DATA WR16; SET final16;
Array race {10} R1_1-R1_10;
Do i=1 to 10;
If race {i} = 'R1' then AIAN =1;
If race {i} = 'R2' then Asian =1;
If race {i} = 'R3' then Black =1;
If race {i} = 'R4' then NHPI =1;
If race {i} = 'R5' then White =1;
If race {i} = 'R9' then Other =1;
end;
run;
Thank you @novinosrin ,its amazing how such a tiny mistake in coding could matter a lot.
You might consider instead of
If race {i} = 'R1' then AIAN =1;
using
AIAN = (race [I] = 'R1');
which will have 0 values when not true.
Then MEAN of AIAN will be the decimal percentage of AIAN in the data set and you may find other uses as well.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.