Sample 44637: Double PROC TRANSPOSE method for reshaping your data set with multiple BY variables
You might have the need to reshape your data set to get one unique row for each unique value of a BY variable. When a second BY variable is involved, you can use a double PROC TRANSPOSE method to reshape your data set.
For example, you might have data that is structured like the following:
myID time s1 s2 s3
1 T1 85 85 86
1 T2 80 79 70
1 T3 78 77 87
2 T1 79 79 79
2 T2 83 83 85
You want the resulting data set to have one unique record for each value of MyID, like this:
myID s1_T1 s2_T1 s3_T1 s1_T2 s2_T2 s3_T2 s1_T3 s2_T3 s3_T3
1 85 85 86 80 79 70 78 77 87
2 79 79 79 83 83 85 . . .
To obtain this result, you can use a double PROC TRANSPOSE method like the one shown on the
Full Code tab. Note that prior to SAS® 9.2, an interim DATA step is required before the second PROC TRANSPOSE step.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
You might have the need to reshape your data set to get one unique row for each unique value of a BY variable. When a second BY variable is involved, you can use a double PROC TRANSPOSE method to reshape your data set.
data sample;
input myID time $ s1-s3;
datalines;
1 T1 85 85 86
1 T2 80 79 70
1 T3 78 77 87
2 T1 79 79 79
2 T2 83 83 85
;
run;
proc sort data=sample;
by myID time;
run;
proc print data=sample;
run;
proc transpose data=sample out=out1;
by myID time;
var s1-s3;
run;
/* Prior to SAS® 9.2, an interim DATA step is required to create an ID variable */
data out1;
set out1;
idvar=catx('_',_name_,time);
run;
proc print data=out1;
run;
proc transpose data=out1 out=new(drop=_name_);
by myID;
var col1;
id idvar;
run;
proc print data=new;
run;
/* Beginning with SAS® 9.2, multiple variables can be included in the ID statement */
proc transpose data=out1 delimiter=_
out=new2(drop=_name_);
by myID;
var col1;
id _name_ time;
run;
proc print data=new2 ;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
This sample illustrates how to use a double PROC TRANSPOSE method for reshaping your data set with multiple BY variables.
Date Modified: | 2020-05-28 11:38:54 |
Date Created: | 2011-10-12 16:25:07 |
Operating System and Release Information
SAS System | Base SAS | z/OS | | |
Z64 | | |
OpenVMS VAX | | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | | |
Microsoft Windows Server 2003 Datacenter 64-bit Edition | | |
Microsoft Windows Server 2003 Enterprise 64-bit Edition | | |
Microsoft Windows XP 64-bit Edition | | |
Microsoft® Windows® for x64 | | |
OS/2 | | |
Microsoft Windows 95/98 | | |
Microsoft Windows 2000 Advanced Server | | |
Microsoft Windows 2000 Datacenter Server | | |
Microsoft Windows 2000 Server | | |
Microsoft Windows 2000 Professional | | |
Microsoft Windows NT Workstation | | |
Microsoft Windows Server 2003 Datacenter Edition | | |
Microsoft Windows Server 2003 Enterprise Edition | | |
Microsoft Windows Server 2003 Standard Edition | | |
Microsoft Windows Server 2003 for x64 | | |
Microsoft Windows Server 2008 | | |
Microsoft Windows Server 2008 for x64 | | |
Microsoft Windows XP Professional | | |
Windows 7 Enterprise 32 bit | | |
Windows 7 Enterprise x64 | | |
Windows 7 Home Premium 32 bit | | |
Windows 7 Home Premium x64 | | |
Windows 7 Professional 32 bit | | |
Windows 7 Professional x64 | | |
Windows 7 Ultimate 32 bit | | |
Windows 7 Ultimate x64 | | |
Windows Millennium Edition (Me) | | |
Windows Vista | | |
Windows Vista for x64 | | |
64-bit Enabled AIX | | |
64-bit Enabled HP-UX | | |
64-bit Enabled Solaris | | |
ABI+ for Intel Architecture | | |
AIX | | |
HP-UX | | |
HP-UX IPF | | |
IRIX | | |
Linux | | |
Linux for x64 | | |
Linux on Itanium | | |
OpenVMS Alpha | | |
OpenVMS on HP Integrity | | |
Solaris | | |
Solaris for x64 | | |
Tru64 UNIX | | |