data tempx1;
input ID;
cards;
1
2
3
4
;
run;
data tempx2;
set tempx1;
array diag{4} d1 d2 d3 d4 (1,2,3,4);
do i = 1 to 4;
if diag[i] = ID then diag[i] = 1; else diag[i] = 0;
end;
drop i;
run;
I want 1’s to be in the diagonal of the array, what did i do wrong here?
When you initialise an array with a set of values, the values you give it are only set once. They don’t get re-set for each implicit loop of the dataset.
You therefore must do this explicitly. The following code will work for you:
Here is a demo which should make it easier to understand why this works: