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

 Hi guys,

 

I have a little problem. I have the following dataset:

filippo_kow_0-1655995106866.png

and in the results I would like to obtain duplicated record for x1/x2 and y1/y2 variables, it means something like this:

filippo_kow_1-1655995288699.png

 

If I will have only one variable, then I would simply use:

proc transpose data=test out=tr;
by trans1 trans2;
var x1 x2;
run;

 

But the problem is with multiple variables.

 

 

Can you please help me how can I obtain the result (using proc transpose or something else) ?

 

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Do them separately and combine.

proc transpose data=test out=X;
by trans1 trans2;
var x1 x2;
run;
proc transpose data=test out=X(rename=(_name_=_name2_ col1=col2));
by trans1 trans2;
var x1 x2;
run;
data want;
  merge x y;
  by trans1 trans2;
run;

Or just code it yourself.

data want;
  set test ;
  array _x x1-x2;
  array _y y1 y2;
  do index=1 to dim(_x) ;
     x=_x[index];
     y=_y[index];
     output;
   end;
  drop x1-x2 y1-y2;
run;

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

Do them separately and combine.

proc transpose data=test out=X;
by trans1 trans2;
var x1 x2;
run;
proc transpose data=test out=X(rename=(_name_=_name2_ col1=col2));
by trans1 trans2;
var x1 x2;
run;
data want;
  merge x y;
  by trans1 trans2;
run;

Or just code it yourself.

data want;
  set test ;
  array _x x1-x2;
  array _y y1 y2;
  do index=1 to dim(_x) ;
     x=_x[index];
     y=_y[index];
     output;
   end;
  drop x1-x2 y1-y2;
run;
filippo_kow
Obsidian | Level 7

 Hi,

 

Thanks for the info!

 

That's deffinetely good solution, but the problem occurs when you have many "col_n" and variables - then I need to make many single transposes and merge by many columns. I just thought that maybe there is "one-step" solution of this problem 🙂

filippo_kow
Obsidian | Level 7

 Hi again Tom,

 

I missed your second data step - it is exactly what I meant - thanks a lot!!!

 

 Cheers,

 Filip

Tom
Super User Tom
Super User

@filippo_kow wrote:

 Hi,

 

Thanks for the info!

 

That's deffinetely good solution, but the problem occurs when you have many "col_n" and variables - then I need to make many single transposes and merge by many columns. I just thought that maybe there is "one-step" solution of this problem 🙂


Perhaps you can make a better example?

You can also transform to TALL format.  Parse out the NAME and the NUMBER from the name and reorder and transform back.

proc transpose data=test out=tall ;
  by id1 id2 ;
  var x1 x2 y1 y2 z1 z2 ;
run;

data step2;
  set tall;
  name=substr(_name_,1,length(_name_)-1);
  number = char(_name_,length(_name_));
run;

proc sort data=step2;
  by id1 id2 number name ;
run;

proc transpose data=step2 out=want;
  by id1 id2 number ;
  id name;
  var col1;
run;

Parsing your real variable names might be harder.

Ksharp
Super User
data have;
input  (t1 t2 x1 x2 x3 y1 y2 y3 z1 z2 z3) ($);
cards;
xyz xyz 111 222 333 888 999 777 1 2 3
;

proc transpose data=have out=temp;
by t1 t2;
var x1--z3;
run;
data temp;
 set temp;
 id=scan(_name_,1,,'d');
run;
proc sql noprint;
select distinct catt('temp(where=(id="',id,'") rename=(_name_=name_',id,' col1=col_',id,'))') into :merge separated by ' ' from temp;
quit;

data want;
 merge &merge.;
 by t1 t2;
 output;
 call missing(of _all_);
drop id;
run;
PaigeMiller
Diamond | Level 26

You can use the %UNTRANSPOSE macro

 

ABSTRACT
PROC TRANSPOSE is an extremely powerful tool for making long files wide, and wide files less wide or
long, but getting it to do what you need often involves a lot of time, effort, and a substantial knowledge of
SAS® functions and data step processing. This is especially true when you have to untranspose a wide
file that contains both character and numeric variables. And, while the procedure usually seamlessly
handles variable types, lengths and formats, it doesn’t always do that and only creates a system variable
(i.e., _label_) to capture variable labels. The present paper introduces a macro that simplifies the process,
significantly reduces the amount of coding and programming skills needed (thus reducing the likelihood of
producing the wrong result), runs up to 50 or more times faster than the multiple PROC TRANSPOSE
and data steps that would otherwise be needed, and either creates untransposed variables that inherit all
of the original variables’ characteristics or creates a file that contains all of the relevant metadata.

 

https://support.sas.com/content/dam/SAS/support/en/sas-global-forum-proceedings/2018/2419-2018.pdf

--
Paige Miller

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 874 views
  • 0 likes
  • 4 in conversation