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;
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.
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.
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.
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.
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.
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;
@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
Hello @liyongkai800,
Your question requires more details before experts can help. Can you revise your question to include more information?
Review this checklist:
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
.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;
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.