Solved
New Contributor
Posts: 4

# Stacking columns

I know that you can stack variables Using the DATA Step, SET statement for different data sets. But how do you stack specific columns in the same data set?

I have variables x1 to x10 (with multiple observations) 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

Please note that I am not trying to make a "wide" format dataset into a "long" format. Just trying to stack the columns one after the other ...

Can't seem to figure this one out.

Accepted Solutions
Solution
‎12-03-2012 03:13 PM
Super User
Posts: 23,776

## Re: Stacking columns

Posted in reply to sandeep249

Then the first one will work.

All Replies
Super User
Posts: 23,776

## Re: Stacking columns

Posted in reply to sandeep249

It is long to wide, just with two different sets of variables.

Does the order matter for the observations?

One long way...

data want;

set have (keep= x1 x2 rename=(x1=col1 x2=col2))

have (keep= x3 x4 rename=(x3=col1 x4=col2));

run;

or...

data want;

set have;

array one(5) x1 x3 x5 x7 x9;

array two(5) x2 x4 x6 x8 x10;

do i=1 to 5;

col1=one(i);

col2=two(i);

output;

end;

drop i x:;

run;

New Contributor
Posts: 4

## Re: Stacking columns

Thank you. But I tried this method and it does not work.

The ARRAY method you proposed takes the first observation from x1, x3, x5, x7, x9 and puts them in col1. Then it does the same for observation 2, etc. But this not stacking the columns. The order is important, and I want col1 to have all x1 values first, then all x3 values, x5, x7, x9

And the same for col 2: stacked values for x2 x4 x6 x8 x10

Solution
‎12-03-2012 03:13 PM
Super User
Posts: 23,776

## Re: Stacking columns

Posted in reply to sandeep249

Then the first one will work.

New Contributor
Posts: 4

## Re: Stacking columns

Yes, it does! Thank you!

🔒 This topic is solved and locked.

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

Discussion stats
• 4 replies
• 328 views
• 3 likes
• 2 in conversation