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.
Then the first one will work.
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;
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
Then the first one will work.
Yes, it does! Thank you!
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 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.