Corey
Corey

Reputation: 781

Use Variables Across Data Sets

Let's say I have the following data set in SAS:

DATA example_1;
   INFILE 'data.csv' dsd;
   INPUT name $ test1-test5;
   ARRAY test{5} test1-test5;
   ARRAY maximum{5} _TEMPORARY_ (50, 75, 50, 100, 75);
RUN;
PROC PRINT DATA=example_1;
RUN;

I want to create another data set which uses the test array and the maximum array to calculate the score on each test.

DATA example_2;
   SET example_1;
   ARRAY percent{5}; /* This can originally be all 0s it doesn't really matter */
   DO i = 1 TO 5;
       percent{i} = (test{i} / maximum{i}) * 100;
   END;
   DROP i;
RUN;
PROC PRINT DATA=example_2;
RUN;

How can I use those two arrays from the example_1? The way I am doing it now with the set example_1 isn't working..

Thanks!

Upvotes: 2

Views: 124

Answers (1)

itzy
itzy

Reputation: 11755

Arrays aren't stored as part of the data set, just variables. So when you refer to test{i} in the second data step, it doesn't exist. (The variables test1-test5 exist, but they're not in an array.)

Just add the same lines

ARRAY test{5} test1-test5;
ARRAY maximum{5} _TEMPORARY_ (50, 75, 50, 100, 75);

to the second data step and it should work.

Upvotes: 3

Related Questions