Solved
Senior User
Posts: 1

# How to make arrays from multiple datasets. using array (Create two arrays that can be used to access

task > Create two arrays that can be used to access the number of students in grades 7

through 12 from each of your two input data sets. Create a third array that will

i have two data sets ok_mid and ok_high, now i want to make two arrays to access Grade7 - Grade12 from both data sets to add them. So far i have only seen one array - one data sets in a scope examples. how can i achieve above task

thanks.

```proc sort data=data.ok_mid out=mid_sorted(rename=(Grade7-Grade12=MS_Grade7-MS_Grade12)); by MapCity School; run;

data Both Midonly (keep=School MapCity MailCity County) HSonly (keep=School MapCity MailCity County);
merge mid_sorted (in=A)
high_sorted (in=B);
by MapCity School;
if A=1 and B=1 then output Both;
/*(b) create a temporary data set of high schools with no matching record in ok_mid*/
else if A=0 and B=1 then output HSonly;
/*(c) create a temporary data set of middle schools with no matching record in ok_high*/
else if A=1 and B=0 then output Midonly;
run;

data;
set high_sorted;
set mid_sorted;
do i =1 to 6;
new_array[i] = hs_array[i] + ms_array[i];
end;
run;```

Accepted Solutions
Solution
‎07-14-2017 08:10 AM
Super User
Posts: 9,866

## Re: How to make arrays from multiple datasets. using array (Create two arrays that can be used to ac

In your last step you access high_sorted, its likely you want to access the both dataset - i.e. the dataset you merge before hand to get all those variables into one dataset.  Maybe something like this (and updated the formatting so its more readable - also note that a and b are bolean 1 or 0, so you don't need the a=1, just a);

```proc sort data=data.ok_mid out=mid_sorted (rename=(grade7-grade12=ms_grade7-ms_grade12));
by mapcity school;
run;

by mapcity school;
run;

data both midonly (keep=school mapcity mailcity county) hsonly (keep=school mapcity mailcity county);
merge  mid_sorted (in=a)
high_sorted (in=b);
by mapcity school;
if a and b then output both;
else if not(a) and b then output hsonly;
else output midonly;
run;

data;
set both;
do i=1 to 6;
new_array{i}=hs_array{i} + ms_array{i};
end;
run;```

All Replies
Solution
‎07-14-2017 08:10 AM
Super User
Posts: 9,866

## Re: How to make arrays from multiple datasets. using array (Create two arrays that can be used to ac

In your last step you access high_sorted, its likely you want to access the both dataset - i.e. the dataset you merge before hand to get all those variables into one dataset.  Maybe something like this (and updated the formatting so its more readable - also note that a and b are bolean 1 or 0, so you don't need the a=1, just a);

```proc sort data=data.ok_mid out=mid_sorted (rename=(grade7-grade12=ms_grade7-ms_grade12));
by mapcity school;
run;

by mapcity school;
run;

data both midonly (keep=school mapcity mailcity county) hsonly (keep=school mapcity mailcity county);
merge  mid_sorted (in=a)
high_sorted (in=b);
by mapcity school;
if a and b then output both;
else if not(a) and b then output hsonly;
else output midonly;
run;

data;
set both;
do i=1 to 6;
new_array{i}=hs_array{i} + ms_array{i};
end;
run;```

Super User
Posts: 24,028

## Re: How to make arrays from multiple datasets. using array (Create two arrays that can be used to ac

First, get your data in the form you need it. Are the multiple SET statements in the last step creating a dataset in the right format.

If no, figure that out first and then you can declare as many arrays as you want. I suspect you should be using the BOTH dataset.  Or a version with all of the data.

Arrays in SAS are only a short cut reference to the variables. They don't store data per se, just make it easier to reference a variable with an index.

☑ This topic is solved.