Hi all,
I have a question about arrays.
I have a data set that looks similar to this only much bigger (1000x) I need to calculate the combine scores of [a, b ,c ....n] based on a set equation [ ((n)x + (n)y) - Fixed]. I have created an array for it but I failed to obtain the answer:
The dummy data and the ideal outcome:
Data have | Ideal | |||||||||
Fixed | ax | ay | bx | by | cx | cy | a | b | c | |
6 | 13 | 21 | 12 | 16 | 11 | 10 | 28 | 22 | 15 | |
13 | 14 | 21 | 15 | 10 | 15 | 10 | 22 | 12 | 12 | |
1 | 16 | 11 | 20 | 19 | 15 | 15 | 26 | 38 | 29 | |
10 | 14 | 14 | 18 | 17 | 17 | 17 | 18 | 25 | 24 | |
16 | 10 | 14 | 10 | 13 | 12 | 21 | 8 | 7 | 17 | |
17 | 19 | 16 | 17 | 11 | 13 | 20 | 18 | 11 | 16 | |
7 | 11 | 12 | 15 | 19 | 21 | 20 | 16 | 27 | 34 | |
2 | 16 | 21 | 17 | 11 | 15 | 14 | 35 | 26 | 27 | |
11 | 20 | 17 | 17 | 14 | 12 | 20 | 26 | 20 | 21 | |
19 | 12 | 11 | 10 | 17 | 11 | 21 | 4 | 8 | 13 | |
6 | 12 | 20 | 10 | 12 | 17 | 13 | 26 | 16 | 24 |
The code:
data have;
input Fixed ax ay bx by cx cy;
datalines;
6 13 21 12 16 11 10
13 14 21 15 10 15 10
1 16 11 20 19 15 15
10 14 14 18 17 17 17
16 10 14 10 13 12 21
17 19 16 17 11 13 20
7 11 12 15 19 21 20
2 16 21 17 11 15 14
11 20 17 17 14 12 20
19 12 11 10 17 11 21
6 12 20 10 12 17 13
;
run;
data want;
set have;
array xcal (*) ax bx cx;
array ycal (*) ay by cy;
array wnt (*) a b c;
do i=1 to dim(xcal);
do j=1 to dim(ycal);
do k=1 to dim(wnt);
wnt(k) = (xcal(i) + ycal(j)) - fixed ;
end;end;end;
drop i j k;
run;
and the wrong outcome:
Fixed | ax | ay | bx | by | cx | cy | a | b | c |
6 | 13 | 21 | 12 | 16 | 11 | 10 | 15 | 15 | 15 |
13 | 14 | 21 | 15 | 10 | 15 | 10 | 12 | 12 | 12 |
1 | 16 | 11 | 20 | 19 | 15 | 15 | 29 | 29 | 29 |
10 | 14 | 14 | 18 | 17 | 17 | 17 | 24 | 24 | 24 |
16 | 10 | 14 | 10 | 13 | 12 | 21 | 17 | 17 | 17 |
17 | 19 | 16 | 17 | 11 | 13 | 20 | 16 | 16 | 16 |
7 | 11 | 12 | 15 | 19 | 21 | 20 | 34 | 34 | 34 |
2 | 16 | 21 | 17 | 11 | 15 | 14 | 27 | 27 | 27 |
11 | 20 | 17 | 17 | 14 | 12 | 20 | 21 | 21 | 21 |
19 | 12 | 11 | 10 | 17 | 11 | 21 | 13 | 13 | 13 |
6 | 12 | 20 | 10 | 12 | 17 | 13 | 24 | 24 | 24 |
Could anyone advice me the right method or find out what is wrong with my code please?
Many thanks in advance!
Will
You just have too many DOs.
You just have too many DOs.
Hi,
I think only one do loop will do the job.
data want;
set have;
array xcal (*) ax bx cx;
array ycal (*) ay by cy;
array wnt (*) a b c;
do i=1 to dim(xcal);
wnt(i) = (xcal(i) + ycal(i)) - fixed ;
end;
drop i;
run;
proc print data=want;
run;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.