Saturday, July 12, 2025

๐Ÿ”„ Mastering PROC TRANSPOSE in SAS: Convert Rows to Columns and Vice Versa

PROC TRANSPOSE is a powerful SAS procedure used to reshape data by converting rows into columns or columns into rows. Whether you're preparing datasets for reporting or statistical analysis, PROC TRANSPOSE can simplify your task with just a few lines of code.

Proc Transpose in SAS - Datahark

In this blog, you'll learn:

  • What is PROC TRANSPOSE?
  • When and why to use it
  • Syntax and options
  • Multiple real-world examples
  • Tips and tricks for efficient use


๐Ÿ” What is PROC TRANSPOSE in SAS?

PROC TRANSPOSE is used to pivot dataโ€”turning variables (columns) into observations (rows), or vice versa. It's especially useful for:

  • Summarizing repeated measures
  • Restructuring long or wide datasets
  • Preparing data for visualizations or modeling


๐Ÿ“š Basic Syntax of PROC TRANSPOSE

proc transpose data=input_data out=output_data <options>;
by variable(s); * Optional: groups data; id variable; * Optional: names for new columns; var variable(s); * Variables to transpose; run;

๐Ÿ“Œ Key Options Explained

OptionDescription
BYGroups data before transposing
VARSpecifies variables to transpose
IDUses values of a variable as new column names
NAME=Renames the default _NAME_ column
LABEL=Renames the default _LABEL_ column

โœ… Example 1: Transposing Without BY or ID

๐Ÿ”น Input Data

data sales;
input Quarter $ Sales; datalines; Q1 100 Q2 120 Q3 140 Q4 160 ;

๐Ÿ”น Transpose Code

proc transpose data=sales out=sales_transposed;
var Sales; run;

๐Ÿ”น Output

NAMECOL1COL2COL3COL4
Sales100120140160

โœ… Example 2: Transposing with ID to Use Column Names

proc transpose data=sales out=sales_wide;
id Quarter; var Sales; run;

๐Ÿ”น Output

NAMEQ1Q2Q3Q4
Sales100120140160

โœ… Example 3: Transpose with BY Grouping

๐Ÿ”น Input Data

data student_scores;
input Student $ Subject $ Score; datalines; John Math 85 John English 78 John Science 92 Anna Math 88 Anna English 91 Anna Science 84 ;

๐Ÿ”น Transpose Code

proc sort data=student_scores;
by Student; run; proc transpose data=student_scores out=scores_wide; by Student; id Subject; var Score; run;

๐Ÿ”น Output

StudentEnglishMathScience
John788592
Anna918884

โœ… Example 4: Transposing Multiple Variables

data patient_data;
input ID $ Visit $ Height Weight; datalines; P1 Visit1 170 65 P1 Visit2 171 66 P2 Visit1 160 60 P2 Visit2 161 61 ;

๐Ÿ”น Code

proc sort data=patient_data;
by ID; run; proc transpose data=patient_data out=trans_height prefix=Height_; by ID; id Visit; var Height; run; proc transpose data=patient_data out=trans_weight prefix=Weight_; by ID; id Visit; var Weight; run; data final_transposed; merge trans_height trans_weight; by ID; run;

๐Ÿ”น Output

IDHeight_Visit1Height_Visit2Weight_Visit1Weight_Visit2
P11701716566
P21601616061

๐Ÿง  Tips for Using PROC TRANSPOSE

  • Always SORT your data before using BY.
  • Use the PREFIX= option to create meaningful column names.
  • Combine multiple transpositions for complex reshaping.
  • Use NAME= and LABEL= to rename the default variables _NAME_ and _LABEL_.


๐Ÿ”Ž When to Use PROC TRANSPOSE

Use CasePROC TRANSPOSE?
Convert long to wide formatโœ… Yes
Convert wide to long formatโœ… Yes (reverse)
Reshape repeated measuresโœ… Yes
Change actual data valuesโŒ No
Merge multiple reshaped tablesโœ… Yes

๐Ÿ“ˆ Conclusion

PROC TRANSPOSE is an essential tool in any SAS programmerโ€™s toolkit. It simplifies the process of reshaping data for reporting, analysis, and modeling. With a good understanding of BY, ID, and VAR options, you can handle almost any data transformation challenge.

Labels: , , , , , , , , , ,

Sunday, December 20, 2020

๐Ÿ“ฅ PROC IMPORT in SAS: A Complete Guide to Importing External Data Files

One of the most common tasks in data analysis is importing external files into your SAS environment. Fortunately, SAS provides a powerful and flexible procedure called PROC IMPORT that allows you to bring in data from Excel, CSV, and text files with ease.

In this blog post, we'll explore everything you need to know about PROC IMPORT in SAS, including syntax, file types, options, practical examples, and visual aids to make your learning easier.


๐Ÿ” What is PROC IMPORT?

PROC IMPORT is a SAS procedure that allows you to read data from various file typesโ€”most commonly:
  • Microsoft Excel files (.xls, .xlsx)
  • Comma-separated values files (.csv)
  • Tab-delimited or plain text files (.txt)

The IMPORT procedure will, by default, get variable names from the first line in your data file. If you do not want this, then add the GETNAMES=NO statement after the PROC IMPORT statement. PROC IMPORT will assign the variables the names VAR1, VAR2, VAR3, and so on. Also if your data file is type DLM, PROC IMPORT assumes that the delimiter is a space. If you have a different delimiter, then specify it in the DELIMITER= statement. The following shows both these statements:ย 

Click here to Read more ยป

Labels: , , , , , , , , , , ,