02-SAS Data Reading Practice (SASPy Magic 模式)

Author

Simon Zhou

Published

January 10, 2026

1 SAS Data Reading Practice

1.1 使用说明

本 notebook 使用 Python Kernel + SASPy Magic 模式:

  1. 确保使用 Python kernel (work311)
  2. 运行第一个代码单元格 (Setup Cell) 初始化 SAS 会话
  3. 在后续单元格使用 %%SAS magic 运行 SAS 代码

1.1.1 前提条件

  • ✅ Python 环境已激活 (conda activate work311)
  • ✅ 已安装: pip install saspy
  • ✅ sspiauth.dll 路径已添加到 PATH
  • ✅ SAS Workspace Server 运行在 localhost:8591

1.1.2 如遇问题

如果看到 Java/IOM 错误,检查: - Workspace Server 是否运行: netstat -an | findstr "8591" - PATH 是否包含 sspiauth.dll - 或考虑使用 SAS Kernel: pip install sas_kernel && python -m sas_kernel.install --user


1.2 Exercise 1: Reading Built-in Datasets

In this exercise, you’ll practice reading and exploring SAS built-in datasets.

Code
# ========================================
# SASPy Magic 模式配置
# ========================================

# 1. 设置 SASPy Magic 模式
# 在 Jupyter Notebook 中使用 SASPy Magic 模式,可以通过以下命令启用
%load_ext saspy.sas_magic
The saspy.sas_magic extension is already loaded. To reload it, use:
  %reload_ext saspy.sas_magic

1.2.1 Task 1.1: Display sashelp.class

Use PROC PRINT to display the first 10 observations from sashelp.class

Code
%%SAS
/* Your code here */
proc print data=sashelp.class (obs=10);
run;
Using SAS Config named: winlocal
SAS Connection established. Subprocess id is 32384
SAS Output

The SAS System

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

1.2.2 Task 1.2: Get Dataset Information

Use PROC CONTENTS to see the structure of sashelp.cars

Code
%%SAS
/* Your code here */
proc contents data=sashelp.cars;
run;
SAS Output

The SAS System

The CONTENTS Procedure

Data Set Name SASHELP.CARS Observations 428
Member Type DATA Variables 15
Engine V9 Indexes 0
Created 11/08/2018 11:48:08 Observation Length 152
Last Modified 11/08/2018 11:48:08 Deleted Observations 0
Protection   Compressed NO
Data Set Type   Sorted YES
Label 2004 Car Data    
Data Representation WINDOWS_64    
Encoding us-ascii ASCII (ANSI)    
Engine/Host Dependent Information
Data Set Page Size 65536
Number of Data Set Pages 2
First Data Page 1
Max Obs per Page 430
Obs in First Data Page 413
Number of Data Set Repairs 0
ExtendObsCounter YES
Filename C:\Program Files\SASHome\SASFoundation\9.4\core\sashelp\cars.sas7bdat
Release Created 9.0401M6
Host Created X64_SR12R2
Owner Name BUILTIN\Administrators
File Size 192KB
File Size (bytes) 196608
Alphabetic List of Variables and Attributes
# Variable Type Len Format Label
9 Cylinders Num 8    
5 DriveTrain Char 5    
8 EngineSize Num 8   Engine Size (L)
10 Horsepower Num 8    
7 Invoice Num 8 DOLLAR8.  
15 Length Num 8   Length (IN)
11 MPG_City Num 8   MPG (City)
12 MPG_Highway Num 8   MPG (Highway)
6 MSRP Num 8 DOLLAR8.  
1 Make Char 13    
2 Model Char 40    
4 Origin Char 6    
3 Type Char 8    
13 Weight Num 8   Weight (LBS)
14 Wheelbase Num 8   Wheelbase (IN)
Sort Information
Sortedby Make Type
Validated YES
Character Set ANSI

1.3 Exercise 2: Creating Datasets

1.3.1 Task 2.1: Create a Simple Dataset

Create a dataset called ‘students’ with columns: id, name, age, grade

Code
%%SAS
/* Your code here */
data students;
    input id name $ age grade;
    datalines;
1 Alice 20 85
2 Bob 21 78
3 Carol 19 92
4 David 22 88
5 Eve 20 95
;
run;

proc print data=students;
run;
SAS Output

The SAS System

Obs id name age grade
1 1 Alice 20 85
2 2 Bob 21 78
3 3 Carol 19 92
4 4 David 22 88
5 5 Eve 20 95

1.4 Exercise 3: Basic Statistics

1.4.1 Task 3.1: Summary Statistics

Calculate mean, min, max, and std for Age, Height, and Weight from sashelp.class

Code
%%SAS
/* Your code here */
proc means data=sashelp.class mean min max std;
    var age height weight;
run;
SAS Output

The SAS System

The MEANS Procedure

Variable Mean Minimum Maximum Std Dev
Age
Height
Weight
13.3157895
62.3368421
100.0263158
11.0000000
51.3000000
50.5000000
16.0000000
72.0000000
150.0000000
1.4926722
5.1270752
22.7739335

1.4.2 Task 3.2: Frequency Distribution

Create a frequency table for the Sex variable in sashelp.class

Code
%%SAS
/* Your code here */
proc freq data=sashelp.class;
    tables sex;
run;
SAS Output

The SAS System

The FREQ Procedure

Sex Frequency Percent Cumulative
Frequency
Cumulative
Percent
F 9 47.37 9 47.37
M 10 52.63 19 100.00

1.5 Challenge Exercise

1.5.1 Task: Data Subset and New Variable

  1. Create a new dataset from sashelp.class
  2. Keep only students aged 13-15
  3. Create a new variable BMI = weight / (height^2) * 703
  4. Display the results
Code
%%SAS
/* Your code here */
data class_subset;
    set sashelp.class;
    where age between 13 and 15;
    bmi = weight / (height**2) * 703;
    format bmi 5.2;
run;

proc print data=class_subset;
run;

1.6 Notes and Observations

Write your observations here: - What did you learn? - What was challenging? - Questions for review?