www.OASUS.ca
“Come out of the desert of ignorance to the OASUS of knowledge”
Proc du Jour:PROC TRANSPOSETom Kari
Tom Kari Consulting
OASUS, November 20
www.OASUS.ca
Tom Kari, Tom Kari Consulting 2
Simplest case
data SimpleTransposeIn;input A B C;cards;1 2 310 20 30100 200 300run;proc transpose data=SimpleTransposeIn out=SimpleTransposeOut;run;
November 20, 2014
www.OASUS.ca
Tom Kari, Tom Kari Consulting 3
ID Variabledata FinanceData;length Region $9;input Region NetSales;cards;EMNA 3253EME 749EMIO 430EMSA 403Corporate 4.8run;proc transpose data=FinanceData out=Transposed;ID Region;run;
November 20, 2014
• Specifies one or more variables in the input data set whose formatted values name the transposed variables in the output data set.
• Can use with prefix, suffix, delimiter options.
• IDLabel statement provides a variable that contains the labels for the new variables.
www.OASUS.ca
Tom Kari, Tom Kari Consulting 4
BY Variabledata QuarterlyData;length Region $9;input Region Quarter NetSales;cards;EMNA 1 3253EME 1 749...Corporate 4 6.3run;proc sort data=QuarterlyData;by Region;proc transpose data=QuarterlyData out=TransposedQuarterly prefix=Q;by Region;ID Quarter;var NetSales;run;
November 20, 2014
• Excludes one or more variables from being transposed
• Data must be sorted by the BY variable(s)
www.OASUS.ca
Tom Kari, Tom Kari Consulting 5
BY Variable (cont’d)
November 20, 2014
www.OASUS.ca
Tom Kari, Tom Kari Consulting 6
Additional comments
November 20, 2014
• Like PROC SORT, this procedure is intended purely for data processing
• PROC TRANSPOSE has no provisions for ODS
• PROC TRANSPOSE will frequently convert numeric variables to character
• Enterprise Guide provides a good Transpose task
www.OASUS.ca
Tom Kari, Tom Kari Consulting 7
Use case 1:“Datapoint” files
November 20, 2014
Person Name Gender Age Height
1Alfred M 14 69
2Alice F 13 56.5
3Barbara F 13 65.3
4Carol F 14 62.8
5Henry M 14 63.5
6James M 12 57.3
7Jane F 12 59.8
8Janet F 15 62.5
9Jeffrey M 13 62.5
10John M 12 59
11Joyce F 11 51.3
12Judy F 14 64.3
13Louise F 12 56.3
14Mary F 15 66.5
15Philip M 16 72
16Robert M 12 64.8
17Ronald M 15 67
18Thomas M 11 57.5
19William M 15 66.5
Person Variable Value
1 Name Alfred
1 Gender M
1 Age 14
1 Height 69
2 Name Alice
2 Gender F
2 Age 13
2 Height 56.5
3 Name Barbara
3 Gender F
3 Age 13
3 Height 65.3
…
76 rows
www.OASUS.ca
Tom Kari, Tom Kari Consulting 8
Convert from relational to “Datapoint” files
November 20, 2014
www.OASUS.ca
Tom Kari, Tom Kari Consulting 9
Convert from “Datapoint” to relational
November 20, 2014
www.OASUS.ca
Tom Kari, Tom Kari Consulting 10
Use case 2:Edit Spreadsheets
November 20, 2014
Spreadsheets are a terrible way to transfer important data from one organization to another:• They are prone to ad-hoc changes;• There’s always the risk of transferring the wrong month,
version, location;• “Glitches” frequently pop up.
Yet everybody does it!
www.OASUS.ca
Tom Kari, Tom Kari Consulting 11
What could go wrong?
November 20, 2014
www.OASUS.ca
Tom Kari, Tom Kari Consulting 12
What could go wrong?
November 20, 2014
www.OASUS.ca
Tom Kari, Tom Kari Consulting 13
What could go wrong?
November 20, 2014
www.OASUS.ca
Tom Kari, Tom Kari Consulting 14
What could go wrong?
November 20, 2014
www.OASUS.ca
Tom Kari, Tom Kari Consulting 15
Spaghetti code!
November 20, 2014
www.OASUS.ca
Tom Kari, Tom Kari Consulting 16
A different approach
November 20, 2014
• Transpose one of your spreadsheets (turn rows and columns into rows identified by cell id, e.g. B17)
• Separate your cells into “static” (the contents should always be the same), and “fixed” (contains data that we want to capture). Set this table up as a reference table.
• For your production processing:– Pull your spreadsheet into SAS (PROC IMPORT);– Transpose the cells same way, into rows with a cell id;– Match to the reference table by cell id:
• If static and values don’t match, we have a problem.• If a reference cell is empty, and a test cell isn’t, or vice versa, do we have a problem?
– Pull out your “fixed” cell ids (we know what they are from the reference table), and transpose them. You’ll get one record with your data!
www.OASUS.ca
Tom Kari, Tom Kari Consulting 17November 20, 2014