“Come out of the desert of ignorance to the OASUS of knowledge” Proc du Jour: PROC TRANSPOSE Tom Kari Tom Kari Consulting OASUS, November 20
Simplest case data SimpleTransposeIn; input A B C; cards; run; proc transpose data=SimpleTransposeIn out=SimpleTransposeOut; run; November 20, 2014Tom Kari, Tom Kari Consulting2
ID Variable data FinanceData; length Region $9; input Region NetSales; cards; EMNA 3253 EME 749 EMIO 430 EMSA 403 Corporate 4.8 run; proc transpose data=FinanceData out=Transposed; ID Region; run; November 20, 2014Tom Kari, Tom Kari Consulting3 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.
BY Variable data QuarterlyData; length Region $9; input Region Quarter NetSales; cards; EMNA EME Corporate run; proc sort data=QuarterlyData; by Region; proc transpose data=QuarterlyData out=TransposedQuarterly prefix=Q; by Region; ID Quarter; var NetSales; run; November 20, 2014Tom Kari, Tom Kari Consulting4 Excludes one or more variables from being transposed Data must be sorted by the BY variable(s)
BY Variable (cont’d) November 20, 2014Tom Kari, Tom Kari Consulting5
Additional comments November 20, 2014Tom Kari, Tom Kari Consulting6 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
Use case 1: “Datapoint” files November 20, 2014Tom Kari, Tom Kari Consulting7 PersonNameGenderAgeHeight 1AlfredM1469 2AliceF BarbaraF CarolF HenryM JamesM JaneF JanetF JeffreyM JohnM JoyceF JudyF LouiseF MaryF PhilipM RobertM RonaldM ThomasM WilliamM PersonVariableValue 1NameAlfred 1GenderM 1Age14 1Height69 2NameAlice 2GenderF 2Age13 2Height56.5 3NameBarbara 3GenderF 3Age13 3Height65.3 … 76 rows
Convert from relational to “Datapoint” files November 20, 2014Tom Kari, Tom Kari Consulting8
Convert from “Datapoint” to relational November 20, 2014Tom Kari, Tom Kari Consulting9
Use case 2: Edit Spreadsheets November 20, 2014Tom Kari, Tom Kari Consulting10 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!
What could go wrong? November 20, 2014Tom Kari, Tom Kari Consulting11
What could go wrong? November 20, 2014Tom Kari, Tom Kari Consulting12
What could go wrong? November 20, 2014Tom Kari, Tom Kari Consulting13
What could go wrong? November 20, 2014Tom Kari, Tom Kari Consulting14
Spaghetti code! November 20, 2014Tom Kari, Tom Kari Consulting15
A different approach November 20, 2014Tom Kari, Tom Kari Consulting16 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!
November 20, 2014Tom Kari, Tom Kari Consulting17