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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Then the first one will work.

View solution in original post

4 REPLIES 4
Reeza
Super User

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;

sandeep249
Calcite | Level 5

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

Reeza
Super User

Then the first one will work.

sandeep249
Calcite | Level 5

Yes, it does! Thank you!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 1217 views
  • 3 likes
  • 2 in conversation