Proc transpose
%******************************************************************************; %** http://www.info-net.nl **; %******************************************************************************; %* Topic : SAS Procedures *; %* Program : Proc transpose *; %* Author : Raymond Ebben *; %* Location : http://www.info-net.nl/index/sas-coding/sas-procedures *; %* Date : January 2006 *; %* Version : 1.0 *; %* Description : This program demonstrates some examples of the proc *; %* transport procedure. *; %******************************************************************************; %* In datasets : None *; %* In macrovars : None *; %* In files : None *; %* Out datasets : None *; %* Out macrovars : None *; %* Out files : None *; %******************************************************************************; %** Version control **; %******************************************************************************; %* Mod * Ver. * Date * Description *; %******************************************************************************; %******************************************************************************; %******************************************************************************; %* Proc transpose is a SAS procedure that can restructure the data in a *; %* dataset. This program will describe different usage examples, *; %* as well as demonstrating the some of the most common pitfalls *; %******************************************************************************; %******************************************************************************; %* Proc transpose examples *; %* Given the following dataset from sashelp : sashelp.class *; %******************************************************************************; %* Obs NAME SEX AGE HEIGHT WEIGHT *; %* *; %* 1 Alfred M 14 69.0 112.5 *; %* 2 Alice F 13 56.5 84.0 *; %* 3 Barbara F 13 65.3 98.0 *; %* 4 Carol F 14 62.8 102.5 *; %* 5 Henry M 14 63.5 102.5 *; %* 6 James M 12 57.3 83.0 *; %* 7 Jane F 12 59.8 84.5 *; %* 8 Janet F 15 62.5 112.5 *; %* 9 Jeffrey M 13 62.5 84.0 *; %* 10 John M 12 59.0 99.5 *; %* 11 Joyce F 11 51.3 50.5 *; %* 12 Judy F 14 64.3 90.0 *; %* 13 Louise F 12 56.3 77.0 *; %* 14 Mary F 15 66.5 112.0 *; %* 15 Philip M 16 72.0 150.0 *; %* 16 Robert M 12 64.8 128.0 *; %* 17 Ronald M 15 67.0 133.0 *; %* 18 Thomas M 11 57.5 85.0 *; %* 19 William M 15 66.5 112.0 *; %* *; %******************************************************************************; %******************************************************************************; %* Example 1 transposing the data to get Sex, name, type, value,Where value is*; %* value of age, height or weight and where type holds the name of the value *; %******************************************************************************; %******************************************************************************; %* Sort the data for the proc transpose and create a copy in the work *; %******************************************************************************; proc sort data=sashelp.class out=class; by sex name; run; %******************************************************************************; %* Transpose the dataset and rename the automatic generated column col1 to *; %* value and _name_ to type on the out statement, otherwise the variable does *; %* not yet exist *; %******************************************************************************; proc transpose data=class out=class(rename=(col1=value _name_=type)); var age height weight; by sex name; run; %******************************************************************************; %* Partial listing of the result of the transposed dataset *; %* *; %* Obs SEX NAME TYPE VALUE *; %* 1 F Alice AGE 13.0 *; %* 2 F Alice HEIGHT 56.5 *; %* 3 F Alice WEIGHT 84.0 *; %* 4 F Barbara AGE 13.0 *; %* 5 F Barbara HEIGHT 65.3 *; %* 6 F Barbara WEIGHT 98.0 *; %* 7 F Carol AGE 14.0 *; %* 8 F Carol HEIGHT 62.8 *; %* 9 F Carol WEIGHT 102.5 *; %* 10 F Jane AGE 12.0 *; %* 11 F Jane HEIGHT 59.8 *; %* 12 F Jane WEIGHT 84.5 *; %* 13 F Janet AGE 15.0 *; %* 14 F Janet HEIGHT 62.5 *; %* 15 F Janet WEIGHT 112.5 *; %******************************************************************************; %******************************************************************************; %* Example 2 transposing the data to get Sex, name, type, value Where value *; %* is value of age, height or weight and where type holds the name of the *; %* value *; %******************************************************************************; proc transpose data=sashelp.class out=class; var sex age weight height; id name; idlabel name; run;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
© 2010 Raymond Ebben, expert SAS consultant and programmer, Netherlands All Rights Reserved.