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

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;

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

 

View solution in original post

10 REPLIES 10
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

liyongkai800
Obsidian | Level 7

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.

Reeza
Super User

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. 

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

 

novinosrin
Tourmaline | Level 20

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;
novinosrin
Tourmaline | Level 20

@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

Community_Guide
SAS Moderator

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.
    use_buttons.png
  • 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.

 

edit_post.png

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

.
ballardw
Super User

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;
Reeza
Super User
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.
Tom
Super User Tom
Super User

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;

image.png

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;

image.png

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 10 replies
  • 4402 views
  • 2 likes
  • 7 in conversation