Stack columns (in same dataset) in SAS

#1
I know that you can concatenate Using the DATA Step for different data sets. But how do you stack specific columns in the same data set?

I have variables x1 to x10 in a data set. And I want to stack them into two columns: (col 1): x1, x3, x5, x7, and x9 and (Col 2): x2, x4, x6, x8, and x10

Can't seem to figure this one out.
 

hlsmith

Less is more. Stay pure. Stay poor.
#2
Sounds like you want to convert data from wide to long. Look this concept up and see if it would work, and if so feel free to post subsequent questions about doing it.
 
#6
Using the array method converts the data from wide to long. But it takes values from each of the variables and does not stack the columns.
But that's not what I want. I want to stack the variables. Exactly how you would if there were variables in two data sets using the SET statement.
 

hlsmith

Less is more. Stay pure. Stay poor.
#7
Is there only one observation per variable, so it would look just like your last post but numbers inserted into the x1-x10?
 

hlsmith

Less is more. Stay pure. Stay poor.
#9
Seems like you could perform the change via the methods on the listed link, just need to create some varibles, then delete them out at the end. I am no smooth operator when it comes to clean/concise SAS code, so if you are looking for more direct coding - hopefully someone else reads and posts on this thread.
 
#10
Thank you for helping.

I am familiar with the procedures listed on that link. It tells you how to convert your data from wide to long format. But that is not what I am trying to do. I am simply trying to stack the columns as is.
 

Mean Joe

TS Contributor
#11
Code:
data stacked;
  set old_data;
  column_A=x1;
  column_B=x2;
  output;

  column_A=x3;
  column_B=x4;
  output;

  column_A=x5;
  column_B=x6;
  output;

  column_A=x7;
  column_B=x8;
  output;

  column_A=x9;
  column_B=x10;
  output;
run;