last first kiss.variable与last.variable是干什么用的

BY Statement :: SAS(R) 9.3 Statements: Reference
BY Statement
Controls the operation of a SET, MERGE, MODIFY,
or UPDATE statement in the DATA step and sets up special grouping
variables.
DATA step or PROC step
File-handling
Declarative
DESCENDING
specifies that the
data sets are sorted in descending order by the variable that is specified.
DESCENDING means largest to smallest numerically, or reverse alphabetical
for character variables.
Restriction:You cannot use the DESCENDING option with data sets that
are indexed because indexes are always stored in ascending order.
GROUPFORMAT
uses the formatted
values, instead of the internal values, of the BY variables to determine
where BY groups begin and end, and therefore how FIRST.variable and LAST.variable are assigned. Although the GROUPFORMAT option can appear anywhere
in the BY statement, the option applies to all variables in the BY statement.
Restrictions:You must sort the observations in a data set based on
the value of the BY variables before using the GROUPFORMAT option
in the BY statement.
You can use the GROUPFORMAT option in a BY statement
only in a DATA step.
Interaction:If you also use the NOTSORTED option, you can
group the observations in a data set by the formatted value of the
BY variables without requiring that the data set be sorted or indexed.
Note:BY-group processing in the DATA step using the GROUPFORMAT
option is the same as BY-group processing with formatted values in
SAS procedures.
Tips:Using the GROUPFORMAT option is useful when you define
your own formats to display data that is grouped.
Using the GROUPFORMAT option in the DATA step ensures
that BY groups that you use to create a data set match the BY groups
in PROC steps that report grouped, formatted data.
See: in SAS Language Reference: Concepts
names each variable
by which the data set is sorted or indexed. These variables are referred
to as BY variables for the current DATA or PROC step.
Requirement:If you designate a name literal as the BY variable in
BY-group processing and you want to refer to the corresponding FIRST.
or LAST. temporary variables, you must include the FIRST. or LAST.
portion of the two-level variable name within single quotation marks.
For example:data sedanT
by 'Sedan Types'n;
if 'first.Sedan Types'n then type=1;
Tip:The data set can be sorted or indexed by more than one
specifies that observations
with the same BY value are grouped together but are not necessarily
sorted in alphabetical or numeric order.
Restriction:You cannot use the NOTSORTED option with the MERGE and
UPDATE statements.
Tips:The NOTSORTED option can appear anywhere in the BY statement.
Using the NOTSORTED option is useful if you have data
that falls into other logical groupings such as chronological order
or categories.
How SAS Identifies the Beginning and End of a BY Group
SAS identifies the beginning
and end of a BY group by creating two temporary variables for each
BY variable: FIRST.variable and LAST.variable. The value
of these variables is either 0 or 1. SAS sets the value of FIRST.variable to 1 when it reads the first observation
in a BY group, and sets the value of LAST.variable to 1 when it reads the last observation in a BY group. These temporary
variables are available for DATA step programming but are not added
to the output data set.
For a complete explanation
of how SAS processes grouped data and of how to prepare your data,
in SAS Language Reference: Concepts.
In a DATA Step
The BY statement applies
only to the SET, MERGE, MODIFY, or UPDATE statement that precedes
it in the DATA step, and only one BY statement can accompany each
of these statements in a DATA step.
The data sets that are
listed in the SET, MERGE, or UPDATE statements must be sorted by the
values of the variables that are listed in the BY statement or have
an appropriate index.
As a default, SAS expects the data sets to
be arranged in ascending numeric order or in alphabetical order.
The observations can be arranged by one of the following methods:
sort the data set
create an index for the variables
input the observations in order.
Note: MODIFY does not require sorted
data, but sorting can improve performance.
Note: The BY statement honors the
linguistic collation of data that is sorted by using the SORT
with the SORTSEQ=LINGUISTIC option.
For more information,
in SAS Language Reference: Concepts.
In a PROC Step
You can specify the BY statement with
some SAS procedures to modify their action.
Refer to the individual
procedure in the
for a discussion of how the BY statement affects
processing for SAS procedures.
With SAS Views
If you create a DATA step view by reading from a DBMS
and the SET, MERGE, UPDATE, or MODIFY statement is followed by a BY
statement, the BY statement might cause the DBMS to sort the data
in order to return the data in sorted order. Sorting the data could
increase execution time.
Processing BY Groups
SAS assigns the following values to FIRST.variable and LAST.variable:
FIRST.variable has a value of 1 under the following conditions:
when the current observation is
the first observation that is read from the data set.
when you do not use the GROUPFORMAT
option and the internal value of the variable in the current observation
differs from the internal value in the previous observation.
If you use the GROUPFORMAT
option, FIRST.variable has
a value of 1 when the formatted value of the variable in the current
observation differs from the formatted value in the previous observation.
FIRST.variable has a value of 1 for any preceding variable in the BY statement.
In all other cases,
FIRST.variable has a value
LAST.variable has a value of 1 under the following conditions:
when the current observation is
the last observation that is read from the data set.
when you use the GROUPFORMAT option
and the internal value of the variable in the current observation
differs from the internal value in the next observation.
If you use the GROUPFORMAT
option, LAST.variable has a
value of 1 when the formatted value of the variable in the current
observation differs from the formatted value in the next observation.
LAST.variable has a value of 1 for any preceding variable in the BY statement.
In all other cases,
LAST.variable has a value of
Example 1: Specifying One or More BY Variables
Observations
are in ascending order of the variable DEPT:
Observations are in alphabetical
(ascending) order by CITY and, within each value of CITY, in ascending
order by ZIPCODE:
Example 2: Specifying Sort Order
Observations are in ascending order
of SALESREP and, within each SALESREP value, in descending order of
the values of JANSALES:
by salesrep
Observations are in descending
order of BEDROOMS, and, within each value of BEDROOMS, in descending
order of PRICE:by descending bedro
Example 3: BY-Group Processing with Nonsorted Data
Observations are ordered
by the name of the month in which the expenses were accrued:
Example 4: Grouping Observations by Using Formatted Values
The following example
illustrates the use of the GROUPFORMAT option.
value range
low -55 = 'Under 55'
= '55 to 60'
= '60 to 65'
= '65 to 70'
= 'Over 70';
proc sort data=sashelp.class out=sorted_
data _null_;
format height range.;
set sorted_
if first.height then
put 'Shortest in ' height 'measures ' height:best12.;
SAS writes the following
output to the log:
Shortest in Under 55 measures 51.3
Shortest in 55 to 60 measures 56.3
Shortest in 60 to 65 measures 62.5
Shortest in 65 to 70 measures 65.3
Shortest in Over 70 measures 72
Example 5: Combining Multiple Observations and Grouping Them Based on
One BY Value
The following example
shows how to use FIRST.variable and LAST.variable with BY-group
processing.
length RecordID 8 Invoice $ 30 ItemLine $ 50;
input RecordID Invoice ItemLine &;
drop RecordID;
Highlighters
Lot # 7603
Yellow Blue Green
24 per box
Paper Clips
Lot # 7423
Small Medium Large
Lot # 4422
Rubber bands
Lot # 7892
Wide width, Narrow width
array Line{4} $ 60 ;
retain Line1-Line4;
keep Invoice Line1-Line4;
if first.I
call missing(of Line1-Line4);
records = 0;
records + 1;
Line[records]=ItemL
proc print data=
title 'Office Supply Inventory';
Output from Combining Multiple Observations
Statements:您所在位置: &
&nbsp&&nbsp&nbsp&&nbsp
第2章:SAS与股票市场汇总.doc 33页
本文档一共被下载:
次 ,您可全文免费在线阅读后下载本文档。
下载提示
1.本站不保证该用户上传的文档完整性,不预览、不比对内容而直接下载产生的反悔问题本站不予受理。
2.该文档所得收入(下载+内容+预览三)归上传者、原创者。
3.登录后可充值,立即自动返金币,充值渠道很便利
需要金币:350 &&
你可能关注的文档:
··········
··········
第2章:SAS与股票市场分析
马柯维茨的均值-方差决策理论表明,在股票市场上,收益与风险是理性投资者在决策时所考虑的两个重要因素,这是因为理性投资者追求的最优投资组合是:在收益一定的条件下风险最小的投资组合,在风险一定的条件下收益最大的投资组合。收益与风险的这个关系,不仅决定了投资者的最优组合,而且决定了包括股票在内的风险资产的定价模型——资本资产定价模型(CAPM)。因此,对股票市场进行分析,首先需要对股票市场的收益与风险进行分析。
本章介绍在SAS中如何进行股票市场的收益风险分析,如何进行单只股票的CAPM拟合与检验。通过本章学习读者将会了解:①股票收益的定义及收益计算的SAS实现;②不同假设条件下期望收益计算的SAS实现;③股市风险的度量方法及SAS实现;④如何利用SAS对单只股票进行CAPM拟合与检验;⑤本章有关的SAS基础知识。
股票市场的收益与风险
股票市场的收益率与期望收益率
股票的收益率
1.单期收益率
度量单个时期的股票收益率,需要考虑两个部分:从股息(红利)中得到的收入、以及从股票的买卖价差中得到的资本收入或损失。如果直接采用红利分配复权后的股票价格数据,一只股票在时刻t的价格为,则该股票的百分比收益率和对数收益率的定义为:
单期的百分比收益率
单期的对数收益率,又称为单期的连续复利收益率
根据泰劳展开式
因此当很小时有,即当很小时单期的对数收益率约等于单期的百分比收益率,然而当较大时,两者之间差异也会较大。
2.K个时期的收益率
k个时期的收益率是指,连续持有股票k个时期,按照复利方式计算所得的投资收益率。k个时期的百分比收益率和对数收益率的定义为:
k个时期的百分比收益率
k个时期的对数收益率
从k个时期的对数收益率的定义可以看到,对数收益率具有一个优良性质:高频数据与低频数据之间有着简单的加总关系,即有:
对数收益率的另外一个优良性质为:在有效市场中,通常假设股票的价格服从对数正态分布,那么股票的对数收益率就可以视为服从正态分布。
3.K个时期的平均收益率
k个时期的平均收益率的度量方法有两种:k个时期的算术平均收益率和k个时期的几何平均收益率。算术平均收益率(AM)等于k个时期中各个单期收益率之和除以时期数k,AM度量了k个时期内各个单期的平均收益率
几何平均收益率(GM)表示一个股票在k个时期内复合的、累计的收益率。一个股票在k个时期的几何平均收益率用以下公式表示
4.财富指数
财富指数(WEALTH)度量投资产生的收益或损失。如果投资者在期初投资1元,则财富指数就代表期末时投资者的资本值。如果财富指数大于1,投资者在这个时期获得了正的投资收益,如果财富指数小于1,投资者这个时期只获得了负的投资收益,或者说投资者遭受到了投资损失。
K个时期的财富指数等于投资者最终的资本值与投资者最初投入的资本值之比,即有
K个时期的财富指数也可以按照以下公式计算
另外,K个时期的几何平均收益率与K个时期的财富指数的关系为
收益率计算的SAS实现
在SAS中,上述定义的各种收益率的计算,可以通过DATA步语句编程来实现。这里通过一个例子来说明。
【例2-1】Excel数据集sjk2-1给出了年期间各个交易日的上证指数数据,其中变量date、close分别表示交易日期与上证指数的收盘价。试计算期间各个年度上证指数的单期收益率,上证指数的10年收益率,10年平均收益率,以及10年的财富指数。
将Excel数据集sjk2-1引入转换为SAS数据集,转换后的SAS数据集命名为sjk2_1。所需要的各种收益率的计算结果,这里通过以下几个步骤和SAS程序而得到。
①创建计算年度收益率的SAS数据集sjk2_1a。
一个年度的单期收益率,就是取本年度最后一个交易日的收盘价,取上个年度最后一个交易日的收盘价,计算得到的收益率。由于数据集sjk2_1中变量close给出的是上证指数的每日收盘价数据,为了计算年度收益率,首先需要创建一个只包含每年最后一个交易日的上证指数收盘价的SAS数据集sjk2_1a。这个数据集的创建,可以通过以下SAS程序来实现。(cx2-1a)
set sjk2_1;
year=year(date);
/*提取每个交易日的年度数据*/
proc sort data=a;
data sjk2_1a;
/*对所有观测按变量year分组*/
if last.year;
/*如果观测是一个by组的最后一个观测*/
在这个程序中,后面这个子
正在加载中,请稍后...您所在位置: &
&nbsp&&nbsp&nbsp&&nbsp
SAS课程.doc 46页
本文档一共被下载:
次 ,您可全文免费在线阅读后下载本文档。
下载提示
1.本站不保证该用户上传的文档完整性,不预览、不比对内容而直接下载产生的反悔问题本站不予受理。
2.该文档所得收入(下载+内容+预览三)归上传者、原创者。
3.登录后可充值,立即自动返金币,充值渠道很便利
需要金币:200 &&
你可能关注的文档:
··········
··········
SAS系统简介
SAS功能模块
SAS系统是一个组合软件系统,它由多个功能模块组合而成,常用模块包括:
1 Base SAS
2 SAS/STAT
二、SAS工作界面和工作窗口
三、数据库操作
在SAS系统中,有四个预定义的数据库,分别是Maps,Sashelp,Sasuser,Work。其中Work数据库叫做临时库,存放在其中的是临时文件,推出系统时,这些文件将会被自动删除。Sasuser库保存的是与用户个人设置有关的文件,它是永久的。Sashelp库保存与SAS帮助系统、例子有关的文件,是永久的。
建立一个新的数据库的操作步骤如下:
1 在“资源管理器”窗口双击逻辑库图标
2 选“文件”-“新建”
3 输入新建数据库的名称,引擎选“默认”,选择“启动时启用”
4 单击“浏览”,选择这个数据库的目录
5 单击确定。
四、数据管理
在SAS系统中,数据是以数据集的形式来存储和管理的,SAS数据集可以看作由若干行和若干列做成的表格,类似于一个矩阵,但各列可以取不同的类型(例如,整数值、字符串等)。数据集的每一行叫做一个观测,每一列叫做一个变量。SAS数据集有时也称为一张表。
SAS对名字(数据集名,变量名,数据库名)有约定:SAS名字由英文字母、数字、下划线组成,第一个字符必须是字母或下滑线,名字最多用32个字符,大写字母和小写字母不区分。
SAS数据集以两级水平来命名:第一级名字是SAS逻辑数据库的库名;第二级是文件的名字。例如Fdata.Class。
SAS数据集的大小只受计算机内存的限制。
五、导出数据与导入数据
仅以导出数据为例,将Fdata.Class导出到一个Excel电子表格中。
六、SAS语言中最重要的是数据步(Data)和过程步(Proc)。
创建SAS数据集
变量分为数值变量和字符变量。
数值变量的值只能是数值。字符变量的值可以是字符、字母、特殊字符和数值。字符变量后跟一个$,表示该变量是字符型而不是数值型。
SAS变量的属性包括:长度、输入输出格式和标签。未设定属性的变量在它们第一次出现时由系统给定。
变量的长度是指存储变量值的字节数。默认长度是8个字节。
变量的输入格式是指SAS系统读入变量值的方式。
变量的输出方式是指SAS系统呈现变量的方式。
变量的标签是指变量名的描述性标识,它至多可用40个字符。
SAS系统中还有一部分很重要的自动变量,可参见(朱74)。
一个DATA步构成的SAS程序。
data A600001(label=”邯郸钢铁”);
input date yymmdd10. oppr
name $ hstocd $;
date yymmdd10. ;
/*设定变量的输出格式 */
Oppr 8.2 ;
Hipr 8.2 ;
Lopr 8.2 ;
Clpr 8.2 ;
Vol 12.2 ;
format sums 16.2 ;
label Date=”日期|Date”;
/*给变量加标签*/
label Oppr=”开盘价|Open Price”;
label Hipr=”最高价|High Price”;
label Lopr=”最低价|Low Price”;
label Clpr=”收盘价|Close Price”;
label Vol=”交易量| Volume Traded “;
label sums=”交易金额|sums”;
label name=”股票简称|stock name”;
label hstocd=”股票代码|Stock Code”;
/*以下是数据表*/
8.68 8.7 8.45 8.49 77 邯郸钢铁 600001
8.52 8.6 8.4 8.47 45
邯郸钢铁 600001
8.47 8.47 8.08 8.12 45 邯郸钢铁 600001
8.08 8.4 8.05 8.35 64 邯郸钢铁 600001
8.39 8.5 8.28 8.29 48 邯郸钢铁 600001
8.32 8.4 8.21 8.26 34 邯郸钢铁 600001
例中,DATA语句告诉SAS创建名为A60
正在加载中,请稍后...sas first.variable什么意思_百度知道
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。
sas first.variable什么意思
我有更好的答案
运算符,PROC,INPUT,CARDS,BY)开头,包含SAS名字、特殊字符。数据步用来生成数据集、计算、报告。SAS语言的基本单位是语句,每个SAS语句一 般由一个关键字(如DATA、整理数据,过程步用来对数据进行分析SAS 语言程序由数据步和过程步组成
采纳率:89%
来自团队:
在已按变量variable分组的数据集中,标记每组的第一行
就是第一个变量的意思啊
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包您所在位置: &
&nbsp&&nbsp&nbsp&&nbsp
第2章:SAS与股票市场课稿.doc 33页
本文档一共被下载:
次 ,您可全文免费在线阅读后下载本文档。
下载提示
1.本站不保证该用户上传的文档完整性,不预览、不比对内容而直接下载产生的反悔问题本站不予受理。
2.该文档所得收入(下载+内容+预览三)归上传者、原创者。
3.登录后可充值,立即自动返金币,充值渠道很便利
需要金币:350 &&
你可能关注的文档:
··········
··········
第2章:SAS与股票市场分析
马柯维茨的均值-方差决策理论表明,在股票市场上,收益与风险是理性投资者在决策时所考虑的两个重要因素,这是因为理性投资者追求的最优投资组合是:在收益一定的条件下风险最小的投资组合,在风险一定的条件下收益最大的投资组合。收益与风险的这个关系,不仅决定了投资者的最优组合,而且决定了包括股票在内的风险资产的定价模型——资本资产定价模型(CAPM)。因此,对股票市场进行分析,首先需要对股票市场的收益与风险进行分析。
本章介绍在SAS中如何进行股票市场的收益风险分析,如何进行单只股票的CAPM拟合与检验。通过本章学习读者将会了解:①股票收益的定义及收益计算的SAS实现;②不同假设条件下期望收益计算的SAS实现;③股市风险的度量方法及SAS实现;④如何利用SAS对单只股票进行CAPM拟合与检验;⑤本章有关的SAS基础知识。
股票市场的收益与风险
股票市场的收益率与期望收益率
股票的收益率
1.单期收益率
度量单个时期的股票收益率,需要考虑两个部分:从股息(红利)中得到的收入、以及从股票的买卖价差中得到的资本收入或损失。如果直接采用红利分配复权后的股票价格数据,一只股票在时刻t的价格为,则该股票的百分比收益率和对数收益率的定义为:
单期的百分比收益率
单期的对数收益率,又称为单期的连续复利收益率
根据泰劳展开式
因此当很小时有,即当很小时单期的对数收益率约等于单期的百分比收益率,然而当较大时,两者之间差异也会较大。
2.K个时期的收益率
k个时期的收益率是指,连续持有股票k个时期,按照复利方式计算所得的投资收益率。k个时期的百分比收益率和对数收益率的定义为:
k个时期的百分比收益率
k个时期的对数收益率
从k个时期的对数收益率的定义可以看到,对数收益率具有一个优良性质:高频数据与低频数据之间有着简单的加总关系,即有:
对数收益率的另外一个优良性质为:在有效市场中,通常假设股票的价格服从对数正态分布,那么股票的对数收益率就可以视为服从正态分布。
3.K个时期的平均收益率
k个时期的平均收益率的度量方法有两种:k个时期的算术平均收益率和k个时期的几何平均收益率。算术平均收益率(AM)等于k个时期中各个单期收益率之和除以时期数k,AM度量了k个时期内各个单期的平均收益率
几何平均收益率(GM)表示一个股票在k个时期内复合的、累计的收益率。一个股票在k个时期的几何平均收益率用以下公式表示
4.财富指数
财富指数(WEALTH)度量投资产生的收益或损失。如果投资者在期初投资1元,则财富指数就代表期末时投资者的资本值。如果财富指数大于1,投资者在这个时期获得了正的投资收益,如果财富指数小于1,投资者这个时期只获得了负的投资收益,或者说投资者遭受到了投资损失。
K个时期的财富指数等于投资者最终的资本值与投资者最初投入的资本值之比,即有
K个时期的财富指数也可以按照以下公式计算
另外,K个时期的几何平均收益率与K个时期的财富指数的关系为
收益率计算的SAS实现
在SAS中,上述定义的各种收益率的计算,可以通过DATA步语句编程来实现。这里通过一个例子来说明。
【例2-1】Excel数据集sjk2-1给出了年期间各个交易日的上证指数数据,其中变量date、close分别表示交易日期与上证指数的收盘价。试计算期间各个年度上证指数的单期收益率,上证指数的10年收益率,10年平均收益率,以及10年的财富指数。
将Excel数据集sjk2-1引入转换为SAS数据集,转换后的SAS数据集命名为sjk2_1。所需要的各种收益率的计算结果,这里通过以下几个步骤和SAS程序而得到。
①创建计算年度收益率的SAS数据集sjk2_1a。
一个年度的单期收益率,就是取本年度最后一个交易日的收盘价,取上个年度最后一个交易日的收盘价,计算得到的收益率。由于数据集sjk2_1中变量close给出的是上证指数的每日收盘价数据,为了计算年度收益率,首先需要创建一个只包含每年最后一个交易日的上证指数收盘价的SAS数据集sjk2_1a。这个数据集的创建,可以通过以下SAS程序来实现。(cx2-1a)
set sjk2_1;
year=year(date);
/*提取每个交易日的年度数据*/
proc sort data=a;
data sjk2_1a;
/*对所有观测按变量year分组*/
if last.year;
/*如果观测是一个by组的最后一个观测*/
在这个程序中,后面这个子
正在加载中,请稍后...

我要回帖

更多关于 first last 的文章

 

随机推荐