BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
VVill
Calcite | Level 5

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 haveIdeal
Fixedaxaybxbycxcyabc
6132112161110282215
13142115101510221212
1161120191515263829
10141418171717182524
161014101312218717
17191617111320181116
7111215192120162734
2162117111514352627
11201717141220262021
191211101711214813
6122010121713261624

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:

Fixedaxaybxbycxcyabc
6132112161110151515
13142115101510121212
1161120191515292929
10141418171717242424
16101410131221171717
17191617111320161616
7111215192120343434
2162117111514272727
11201717141220212121
19121110171121131313
6122010121713242424

Could anyone advice me the right method or find out what is wrong with my code please?

Many thanks in advance!

Will

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

You just have too many DOs.

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 = (xcal + ycal) - fixed ;
         
    end;
      
    drop i;
       run;

    View solution in original post

    2 REPLIES 2
    data_null__
    Jade | Level 19

    You just have too many DOs.

    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 = (xcal + ycal) - fixed ;
         
    end;
      
    drop i;
       run;
    stat_sas
    Ammonite | Level 13

    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;

    sas-innovate-2024.png

    Don't miss out on SAS Innovate - Register now for the FREE Livestream!

    Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

     

    Register now!

    How to Concatenate Values

    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.

    Click image to register for webinarClick image to register for webinar

    Classroom Training Available!

    Select SAS Training centers are offering in-person courses. View upcoming courses for:

    View all other training opportunities.

    Discussion stats
    • 2 replies
    • 371 views
    • 3 likes
    • 3 in conversation