SAS学习笔记(1):Basic Concepts

 未分类  SAS学习笔记(1):Basic Concepts已关闭评论
11月 301999

SAS OnlineTutor®: Basic and Intermediate SAS®的learning path见《SAS学习笔记:开篇》

SAS Programs

A SAS program can consist of a DATA step or a PROC step or any combination of DATA and PROC steps.

DATA steps typically create or modify SAS data sets. They can also be used to produce custom-designed reports.

PROC (procedure) steps are pre-written routines that enable you to analyze and process the data in a SAS data set and to present the data in the form of a report. They sometimes create new SAS data sets that contain the results of the procedure. PROC steps can list, sort, and summarize data.

SAS programs consist of SAS statements. A SAS statement has two important characteristics:

  • It usually begins with a SAS keyword.
  • It always ends with a semicolon.

A DATA step begins with a DATA statement, which begins with the keyword DATA. A PROC step begins with a PROC statement, which begins with the keyword PROC.

SAS statements are free-format. This means that

  • they can begin and end anywhere on a line
  • one statement can continue over several lines
  • several statements can be on a line.

Blanks or special characters separate "words" in a SAS statement.

DATA and PROC statements signal the beginning of a new step. When SAS encounters a subsequent DATA, PROC, or RUN statement (for DATA steps and most procedures) or a QUIT statement (for some procedures), SAS stops reading statements and executes the previous step in the program. In our sample program, each step ends with a RUN statement.

The beginning of a new step (DATA or PROC) implies the end of the previous step. Though the RUN statement is not always required between steps in a SAS program, using it can make the SAS program easier to read and debug, and it makes the SAS log easier to read.

SAS Libraries

Every SAS file is stored in a SAS library, which is a collection of SAS files. A SAS data library is the highest level of organization for information within SAS.

SAS libraries have different implementations depending on your operating environment, but a library generally corresponds to the level of organization that your host operating system uses to access and store files. In some operating environments, a library is a physical collection of files. In others, the files are only logically related.

Windows, UNIX, OpenVMS, OS/2(directory based-systems)

a group of SAS files that are stored in the same directory. Other files can be stored in the directory, but only the files that have SAS file extensions are recognized as part of the SAS data library.

CMS: a group of SAS files that have the same file type.

z/OS (OS/390): a specially formatted host data set in which only SAS files are stored.

Depending on the library name that you use when you create a file, you can store SAS files temporarily or permanently.

Storing files temporarily:  If you don’t specify a library name when you create a file (or if you specify the library name Work), the file is stored in the temporary SAS data library. When you end the session, the temporary library and all of its files are deleted.

Storing files permanently:  To store files permanently in a SAS data library, you specify a library name other than the default library name Work.

Referencing SAS Files

To reference a permanent SAS data set in your SAS programs, you use a two-level name: libref.filename. In the two-level name, libref is the name of the SAS data library that contains the file, and filename is the name of the file itself. A period separates the libref and filename.

To reference temporary SAS files, you can specify the default libref Work, a period, and the filename. For example, the two-level name Work.Test references the SAS data set named Test that is stored in the temporary SAS library Work. Alternatively, you can simply use a one-level name (the filename only) to reference a file in a temporary SAS library. When you specify a one-level name, the default libref Work is assumed.

Rules for SAS Names: SAS data set names

  • can be 1 to 32 characters long
  • must begin with a letter (A-Z, either uppercase or lowercase) or an underscore (_)
  • can continue with any combination of numbers, letters, or underscores.

SAS Data Sets

A SAS data set is a file that consists of two parts: a descriptor portion and a data portion.

The descriptor portion of a SAS data set contains information about the data set, including

  • the name of the data set
  • the date and time that the data set was created
  • the number of observations
  • the number of variables.

The data portion of a SAS data set is a collection of data values that are arranged in a rectangular table.

Rows (called observations) in the data set are collections of data values that usually relate to a single object.

Columns (called variables) in the data set are collections of values that describe a particular characteristic.

Variable Attributes

Name: Each variable has a name that conforms to SAS naming conventions. Variable names follow exactly the same rules as SAS data set names. Like data set names, variable names

  • can be 1 to 32 characters long
  • must begin with a letter (A-Z, either uppercase or lowercase) or an underscore (_)
  • can continue with any combination of numbers, letters, or underscores.

Type: A variable’s type is either character or numeric.

  • Character variables, such as Name (shown below), can contain any values.
  • Numeric variables, such as Policy and Total (shown below), can contain only numeric values (the digits 0 through 9, +, -, ., and E for scientific notation).

A variable’s type determines how missing values for a variable are displayed. In the following data set, Name and Sex are character variables, and Age and Weight are numeric variables.

  • For character variables such as Name, a blank represents a missing value.
  • For numeric variables such as Age, a period represents a missing value.

Length: A variable’s length (the number of bytes used to store it) is related to its type.

  • Character variables can be up to 32K long. In the exa
    mple below, Name has a length of 20 characters and uses 20 bytes of storage.
  • All numeric variables have a default length of 8. Numeric values (no matter how many digits they contain) are stored as floating-point numbers in 8 bytes of storage, unless you specify a different length.

Format: Formats are variable attributes that affect the way data values are written. SAS software offers a variety of character, numeric, and date and time formats. You can also create and store your own formats. To write values out using some particular form, you select the appropriate format.

SAS format

For example, to display the value 1234 as $1234.00 in a report, you can use the DOLLAR8.2 format.

Informat: Whereas formats write values out using some particular form, informats read data values in certain forms into standard SAS values. Informats determine how data values are read into a SAS data set. You must use informats to read numeric values that contain letters or other special characters.

SAS informat

For example, the numeric value $12,345.00 contains two special characters, a dollar sign ($) and a comma (,). You can use an informat to read the value while removing the dollar sign and comma, and then store the resulting value as a standard numeric value.

Label: A variable can have a label, which consists of descriptive text up to 256 characters long.

Technorati Tags: ,


 未分类  通告已关闭评论
11月 301999






SAS Macro的面对对象解释-一个附会

 未分类  SAS Macro的面对对象解释-一个附会已关闭评论
11月 301999


Technorati Tags: , , ,


 未分类  SAS和蒙特卡罗模拟(1):开篇已关闭评论
11月 301999

SAS for Monte Carlo Simulations (1): Introduction


为什么要用SAS做蒙卡?首先,对我来说,我只会用SAS,而且打算用SAS完成我所有的工作。当然,其他一些通用的理由有(Fan, etc.,2002):

  1. 蒙卡是个计算密集的活,而SAS Base、SAS Macro、SAS/IML强大而灵活的编程能力能满足这一要求;
  2. 做蒙卡时要用到大量的统计/数学技术,而SAS就内置了大量的统计/数学函数(在 SAS/Stat和SAS/ETS);用Fortran或C++当然也是非常好的主意,只是他们缺少内置的统计函数,代码就要冗长复杂很多。


好,开始,什么是蒙卡?了解它背景知识的最好办法当然是wiki-Monte_Carlo_method。蒙特卡罗是位于摩洛哥的一家赌场,二战时,美国Los Alamos国家实验室把它作为核裂变计算机模拟的代码名称。作为模拟方法,蒙卡以前就叫统计抽样(statistical sampling),我们感兴趣的结果因为输入变量的不确定而不可知,但如果能依概率产生输入变量的样本,我们就可以估计到结果变量的分布。跟蒙卡对应的,还有一种模拟技术叫系统模拟,包括排队、库存等模型,这些模型都跟随时间推移而出现的事件序列有关。下面举个蒙卡的例子,来自Evans, etc.( 2001)的超级简化版。


  1. 投骰子;
  2. 根据骰子的点数确定需求量;
  3. 根据需求量,求利润。


重复次数 骰子点数 需求量 利润
1 5 50 100
2 3 30 60
3 3 30 60
4 6 60 120
5 1 10 20
6 3 30 60
7 4 40 40
8 5 50 100
9 2 20 20
10 5 50 50


 金融计算  计算股票的贝塔值β已关闭评论
11月 301999






下载yahoo finance和雅虎财经数据

 金融计算  下载yahoo finance和雅虎财经数据已关闭评论
11月 301999

找股票、基金、期货等实时或历史数据,yahoo finance是一个不错的地方(最好的地方?),更新快,免费,输出格式通用。如果你只想要比如ibm股票的数据,那直接下载就是;但如果想用爬虫程序同时下载ibm、google等数十家股票的数据——气人的是——所有的下载过来的数据都以table.csv命名,想想还是用迅雷加手动省事:

  • 打开yahoo finance主页,左上角有个“Enter Symbol(s)”的提示框,填入你想要股票代码,比如ibm。如果没有直接对应的股票,比如你输入google,它就会转到一个搜索界面,再选中goog便是;
  • 前面我们填的是ibm,那么我们就进入了这个页面。在页面的左栏,“Quotes”栏目的第三个,点击"Historical Prices"就进入了ibm股票的历史数据页面
  • 在这个数据页面,你还可以设定你需要数据的时间刻度(日交易数据还是周、月交易数据)和时间范围(在事件研究项目上有用),默认的时间刻度是天,范围是最早到最近。拉到页面的最低端,找到“Download To Spreadsheet”这个按钮。如果你只需要这个数据,点击保存就是。要注意的是所有下载下来的数据都以table.csv命名,所以一定记得要重命名;
  • 如果想利用迅雷同是下载更多只股票的信息,那么我们就要看看以上链接的信息。比如在ibm的页面,在那个“Download To Spreadsheet”上,点右键,找到“Properties性质”,我们看到这个数据文件的URL是,同样查到google股票数据的URL是

  • 以上差别就是加黑部分股票代码的不同。比如说我们还知道其他感兴趣的股票代码,比如ag等,现在就可以打开迅雷,点“文件”—“新建”,在弹出来的对话框中,“网址(URL)”就填入并命名为ibm.csv;对ag等股票,把上面的URL中的IBM改为AG就是,接下来重复以上动作,填上所有你感兴趣的股票代码,剩下的就是让机器慢慢下了。

当然,以上手工活还挺多。听说有个专门下yahoo finance的工具,不知其可。中国雅虎的财经频道跟yahoo finance类似的结构,一样可行。


 未分类  一组常用的SAS快捷键和命令(一):入门阶段已关闭评论
11月 301999

腾出手来动鼠标,对习惯在Linux下编程的程序员来说都非常麻烦:双手最好都时刻停在键盘上。在SAS for Windows下,还是有几个快捷键能让我们的工作更轻松,一下都是都是我平时常用的,而且大多每次开机都要用上:

  1. 第一个当然是F9,它能显示所有的SAS快捷键;一个可以替代的SAS 命令栏命令是keys。这个命令栏就是SAS 视窗左上角那个小框,在菜单栏下面那个,不是SAS的程序编辑器Editor;
  2. 提到命令框,第2个快捷键就是F11。比如说你在Editor下写程序,想要显示每条程序的行号,摁下F11就可以跳到命令框,敲入nums,回车,就回到你当前的编辑器里;
  3. 当然你还可以在程序编辑器Editor里加一个命令框:Tools——Options——Preferences——View,找到Command line钩上就是。这样在你的Editor上面就会出现一个Command ===>,这里你可以像在命令框一样敲入一些小命令,比如bye就退出系统。对这个Command line,一个类似命令框的F11的快捷键是home,它可以在Command line和Editor之间来回切换。比较一下,命令框的好处就是它能自动记下你敲入的每条命令,以后你敲入一个字母它就能够自动补齐,Command line没有记忆功能,它的好处就是,它就在你的编辑器里;
  4. 返回键backspace:比如说你在Libraries一路点开,backspace可以一路返回;
  5. 接下来是一组连贯的快捷键(F3到F7):
    • F5回到你的程序编辑器Editor;
    • 写好程序后,摁F3提交运行;
    • F6查看Log;
    • F7查看结果Output;
    • 如果你在编辑器里删了些东西,F4可以追回整段的代码(recall)。   
  6. 一些组合键可以在第二阶段再熟悉,但一些在Windows下通用的快捷键还是很有价值,如home回到一行的开始,end回到一行的末尾,Ctrl+Home回到整篇的开始,Ctrl+end回到整篇的末尾,摁下Shift不方再用上下左右键选中你感兴趣的内容。


  • F9, keys
  • F11, nums, bye; home (for Command line)
  • backspace
  • F5, F3, F6, F7, F4
  • home, end, Ctrl+Home, Ctrl+end, Shift+上下左右键

Paul Wilmott’s Books on Quantitative Finance

 金融计算  Paul Wilmott’s Books on Quantitative Finance已关闭评论
11月 301999

Paul Wilmott在数量金融(Quantitative Finance)方面的书,跟Fabozzi(发包子)在固定收益(Fixed Income)方面的书一样有名,一样充满重复,不过捡来的声誉要高得多。现在有朋友问,他哪些书值得买(或者复印、打印),其实这个问题Paul写书之前——我看——就给出了答案。他最有名的书,Paul Wilmott on Quantitative Finance,以前是两卷本(2000,1064页),现在是三卷(2006,1500页),手头备一份就可以了。如果还嫌贵,Paul还准备了一册Paul Wilmott Introduces Quantitative Finance(2007,722页),是上面三卷本的精简版

是已经够了。不过在国内,还偶尔能看到Paul的另一本Derivatives: the Theory and Practice of Financial Engineering(1998,768页),或者一册更薄的,The Mathematics of Financial Derivatives: A Student Introduction(1995,330页),这本小书又是他的Option Pricing: mathematical Methods and Computation(1994,457页)的缩写。


Paul Wilmott on Quantitative Finance三卷本(2006,1500页)


1. Products and Markets
2. Derivatives
3. The Random Behavior of Assets
4. Elementary Stochastic Calculus
5. The Black-Scholes Model
6. Partial Differential Equations
7. The Black-Scholes Formulae and the ‘Greeks’
8. Simple Generalizations of the Black-Scholes World
9. Early Exercise and American Options
10. Probability Density Functions and First Exit Times
11. Multi-asset Options
12. How to Delta Hedge
13. Fixed-income Products and Analysis: Yield, Duration and Convexity
14. Swaps
15. The Binomial Model
16. How Accurate is the Normal Approximation?
17. Investment Lessons from Blackjack and Gambling
18. Portfolio Management
19. Value at Risk
20. Forecasting the Markets?
21. A Trading Game


22. An Introduction to Exotic and Path-dependent Options
23. Barrier Options
24. Strongly Path-dependent Options
25. Asian Options
26. Lookback Options
27. Derivatives and Stochastic Control
28. Miscellaneous Exotics
29. Equity and FX Term Sheets
30. One-factor Interest Rate Modeling
31. Yield Curve Fitting
32. Interest Rate Derivatives
33. Convertible Bonds
34. Mortgage-backed Securities
35. Multi-factor Interest Rate Modeling
36. Empirical Behavior of the Spot Interest Rate
37. The Heath, Jarrow & Morton and Brace, Gatarek & Musiela Models
38. Fixed Income Term Sheets
39. Value of the Firm and the Risk of Default
40. Credit Risk
41. Credit Derivatives
42. RiskMetrics and CreditMetrics
43. CrashMetrics
44. Derivatives **** Ups


45. Financial Modeling
46. Defects in the Black-Scholes Model
47. Discrete Hedging
48. Transaction Costs
49. Overview of Volatility Modeling
50. Volatility Smiles and Surfaces
51. Stochastic Volatility
52. Uncertain Parameters
53. Empirical Analysis of Volatility
54. Stochastic Volatility and Mean-variance Analysis
55. Asymptotic Analysis of Volatility
56. Volatility Case Study: The Cliquet Option
57. Jump Diffusion
58. Crash Modeling
59. Speculating with Options
60. Static Hedging
61. The Feedback Effect of Hedging in Illiquid Markets
62. Utility Theory
63. More About American Options and Related Matters
64. Advanced Dividend Modeling
65. Serial Autocorrelation in Returns
66. Asset Allocation in Continuous Time
67. Asset Allocation Under Threat Of A Crash
68. Interest-rate Modeling Without Probabilities
69. Pricing and Optimal Hedging of Derivatives, the Non-probabilistic Model Cont’d
70. Extensions to the Non-probabilistic Interest-rate Model
71. Modeling Inflation
72. Energy Derivatives
73. Real Options
74. Life Settlements and Viaticals
75. Bonus Time
76. Overview of Numerical Methods
77. Finite-difference Methods for One-factor Models
78. Further Finite-difference Methods for One-factor Models
79. Finite-difference Methods for Two-factor Models
80. Monte Carlo Simulation and Related Methods
81. Numerical Integration and Simulation Methods
82. Finite-difference Programs
83. Monte Carlo Programs
A. All the Math You Need… and No More (An Executive Summary)

Paul Wilmott Introduces Quantitative Finance

1 Products and Markets: Equities, Commodities, Exchange Rates, Forwards and Futures

2 Derivatives

3 The Binomial Model

4 The Random Behavior of Assets

5 Elementary Stochastic Calculus 

6 The Black-Scholes Model

7 Partial Differential Equations

8 The Black-Scholes Formula and the ‘Greeks’

9 Overview of Volatility Modeling

10 How to Delta Hedge

11 An Introduction to Exotic and Path-dependent Options

12 Multi-asset Options

13 Barrier Options

14 Fixed-income Products and Analysis: Yield, Duration and Convexity

15 Swaps

16 One-factor Interest Rate Modeling

17 Yield Curve Fitting

18 Interest Rate Derivatives

19 The Heath, Jarrow & Morton and Brace, Gatarek & Musiela Models

20 Investment Lessons from Blackjack and Gambling

21 Portfolio Management

22 Value at Risk

23 Credit Risk

24 RiskMetrics and CreditMetrics

25 CrashMetrics

26 Derivatives **** Ups

27 Overview of Numerical Methods

28 Finite-difference Methods for One-factor Models

29 Monte Carlo Simulation

30 Numerical Integration

A All the Math You Need. . . and No More (An Executive Summary)

B Forecasting the Markets? A Small Digression

C A Trading Game

D Contents of CD accompanying Paul Wilmott Introduces Quantitative Finance, second edition

E What you get if (when) you upgrade to PWOQF2




The Mathematics of Financial Derivatives: A Student Introduction

PART I: Basic Option Theory
1. An Introduction to Options and Markets
2. Asset Price Random Walks
3. The Black-Scholes Model
4. Partial Differential Equations
5. The Black-Scholes Formulae
6. Variations on the Black-Scholes Model
7. American Options
PART II: Numerical Methods
8. Finite-Difference Methods
9. Methods for American Options
10. Binomial Methods
PART III: Further Option Theory
11. Exotic and Path-Dependent Options
12. Barrier Options
13. A Unifying Framework for Path-Dependent Options
14. Asian Options
15. Lookback Options
16. Options with Transaction Costs
PART IV: Interest Rate Derivative Products
17. Interest rate Derivatives
18. Convertible Bonds
Hints to Selected Exercises