## How to combine two arrays

Solved
Occasional Contributor
Posts: 12

# How to combine two arrays

I have created two arrays in sas:

data one;
array x_array{5} x1-x5;
do i=1 to 5;
x_array[i]=i;
end;
run;

data two;
array y_array{5} y1-y5;
do i=1 to 5;
y_array[i]=i+1;
end;
run;

namely x_array=(1,2,3,4,5), y_array=(2,3,4,5,6).

Then how to create a 2 by 5 table as:

1,2,3,4,5

2,3,4,5,6

The following code creates a 2 by 10 table, which is not what i want. How to fix it? Thanks a lot.

data three;

set one two;

run;

Accepted Solutions
Solution
‎06-11-2018 08:38 AM
Super User
Posts: 9,599

## Re: How to combine two arrays

Posted in reply to liyongkai800

You do not have "hundreds of arrays".  Please read the manuals on what arrays are.  They are temporary arrays used for reference and only present in the datastep execution, they are not like arrays from other languages.  What you have is lots a variables.  So, in each of the datasets you set into your final dataset, you need to rename these to be consistent.  There are numerous ways to do this, simplest of which:

Assumes:
DS1 = x1 to x5

DS2 = y1 to y5

DS3 = z1 to z5

```data want;
set ds1
ds2 (rename=(y1-y5=x1-x5))
ds3 (rename=(z1-z5=x1-x5));
run;```

I suppose the underlying question here is why you have ended up with lots of datasets with the same data (as implied by your want), but with different variable names.  It sounds like your whole process has gone wrong earlier than this step and you would save yourself a lot of time and effort to fix it earlier on before the variables get created with different names.

All Replies
Super User
Posts: 9,599

## Re: How to combine two arrays

Posted in reply to liyongkai800

You have defined 10 variables, x1-x5, and y1-y5.  Therefore when you set the two together, you get 10 variables in the output dataset.  If you only want 5 variables, then either the x1-5 should be y1-5 or the y should be x:

```data one;
array x_array{5} x1-x5;
do i=1 to 5;
x_array[i]=i;
end;
run;

data two;
array y_array{5} x1-x5;
do i=1 to 5;
y_array[i]=i+1;
end;
run;

data want;
set one two;
run;```

You will then get five, x1-5, in the output.

Occasional Contributor
Posts: 12

## Re: How to combine two arrays

I was thinking to rename all the variables as your suggestion, say change all the x's to y's. But I got hundreds of arrays, and all of them have different names for the variables.  Is there easier way that I don't have to change the name one by one? Thanks.

Super User
Posts: 23,776

## Re: How to combine two arrays

Posted in reply to liyongkai800

Explain your use case? There are many other ways, but this is a contrived example that doesn't make a lot of sense.

SQL Insert INTO with a macro to handle the multiple tables is one option.

Solution
‎06-11-2018 08:38 AM
Super User
Posts: 9,599

## Re: How to combine two arrays

Posted in reply to liyongkai800

You do not have "hundreds of arrays".  Please read the manuals on what arrays are.  They are temporary arrays used for reference and only present in the datastep execution, they are not like arrays from other languages.  What you have is lots a variables.  So, in each of the datasets you set into your final dataset, you need to rename these to be consistent.  There are numerous ways to do this, simplest of which:

Assumes:
DS1 = x1 to x5

DS2 = y1 to y5

DS3 = z1 to z5

```data want;
set ds1
ds2 (rename=(y1-y5=x1-x5))
ds3 (rename=(z1-z5=x1-x5));
run;```

I suppose the underlying question here is why you have ended up with lots of datasets with the same data (as implied by your want), but with different variable names.  It sounds like your whole process has gone wrong earlier than this step and you would save yourself a lot of time and effort to fix it earlier on before the variables get created with different names.

PROC Star
Posts: 1,836

## Re: How to combine two arrays

Posted in reply to liyongkai800

are you after this?

``````
data combined;
array x_array{5} x1-x5;
do i=1 to 5;
x_array[i]=i;
end;
output;
do i=1 to 5;
x_array[i]=i+1;
end;
output;
drop i;
run;``````
PROC Star
Posts: 1,836

## Re: How to combine two arrays

Posted in reply to novinosrin

@liyongkai800 You wrote "Is there easier way that I don't have to change the name one by one? Thanks."

If you had a chance to look at my previous response. I believe that does exactly what you are looking for. Please correct me if i am wrong

SAS Moderator
Posts: 62

## Re: How to combine two arrays [how to improve your question]

Posted in reply to liyongkai800

Hello @liyongkai800,

Your question requires more details before experts can help. Can you revise your question to include more information?

Review this checklist:

• Specify a meaningful subject line for your topic.  Avoid generic subjects like "need help," "SAS query," or "urgent."
• When appropriate, provide sample data in text or DATA step format.  See this article for one method you can use.
• If you're encountering an error in SAS, include the SAS log or a screenshot of the error condition. Use the Photos button to include the image in your message.
• It also helps to include an example (table or picture) of the result that you're trying to achieve.

To edit your original message, select the "blue gear" icon at the top of the message and select Edit Message.  From there you can adjust the title and add more details to the body of the message.  Or, simply reply to this message with any additional information you can supply.

SAS experts are eager to help -- help them by providing as much detail as you can.

This prewritten response was triggered for you by fellow SAS Support Communities member @ballardw

.
Super User
Posts: 13,583

## Re: How to combine two arrays

Posted in reply to liyongkai800

Arrays are not part of a data set, they are a way of temporarily grouping variables for processing in a data step. The variables may remain but the "array" does not persist.

Also all elements of an array appear on a single observation or row of data, there would be no 2 by 5 appearance.

If you want to define and use an array that is logically 2 by 5 the approach would look something like:

```array xy{2,5}  x1-x5 y1-y5;
do i=1 to 2;
do j= 1 to 5;
xy[i,j]= <whatever>;
end;
end;```
Super User
Posts: 23,776

## Re: How to combine two arrays

Posted in reply to liyongkai800
Arrays and matrixes in SAS are not the same as other languages. If you want to work with arrays/matrix type logic, you can use PROC IML instead. It's best to understand how SAS works and use the base functionality to get the most efficiency.
Super User
Posts: 8,127

## Re: How to combine two arrays

Posted in reply to liyongkai800

What do arrays have to do with your question? If we remove them we can see easier what you are doing.

``````data one;
input x1-x5 ;
cards;
1 2 3 4 5
;

data two;
input y1-y5 ;
cards;
1 2 3 4 5
;

data three ;
set one two;
run;``````

If you want rename the variables in TWO to match the variables in ONE then use a rename statement or rename= dataset optoin.

``````data four ;
set one two(rename=(y1-y5=x1-x5));
run;``````

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
• 10 replies
• 186 views
• 2 likes
• 7 in conversation