楼主: crackman
7640 24

跟crackman读SAS程序(34-50) [推广有奖]

11
crackman 发表于 2010-8-31 17:55:50
跟crackman读SAS程序(44)--SQL里面嵌入宏块


%macro test;
data one;
array trx(24);
do i=1 to 1000;
do j=1 to 24;
trx[j]=ranuni(1);
end;
output;
end;
run;
proc sql;
select trx1 %do i=2 %to 24;
,trx&i
%end;
from one;
%mend test;
%test;


在这个程序里面,注意一个细节问题就是,select trx1 %do i=2 %to 24;
,trx&i 。如果把TRX1去掉,不行,出现错误。如果把 trx&i签名的逗号去掉也不行,也会有错误,其实这里面用%do循环产生的是 ,TRX2      ,TRX3...,TRX24,每一个都是包括了逗号的,SELECT选择COLUMN就是这样的。还有这里的SQL没有QUIT语句结尾,大家可以加一个QUIT出现什么样的结果


当然下面这个更简单,直接在SQL SELECT语句中运动宏

proc sql;
select trx1 %macro loop; %do i=2 %to 24; ,trx&i %end;%mend;%loop from one;
quit;


注意每一个细节,包括标点符号。

12
crackman 发表于 2010-8-31 17:56:10
跟crackman读SAS程序(45)--DATA STEP计算某一变量变化并输出


data one_week ;
input BP $ NAME $ nameI $ ABB $ DSET ;
attrib dset format= ddmmyy. informat= ddmmyy. ;
cards ;
10000001 Jack J. CAMPAIGN 160710
10000001 Jack J. CAMPAIGN 230710
10000001 Jack J. CAMPAIGN 300710
10000001 Jack J. CAMPAIGN 100810
10000001 Jack J. CAMPAIGN 130810
10000006 Karen K NORMAL 230710
10000006 Karen K NORMAL 300710
10000006 Karen K NORMAL 100810
10000006 Karen K NORMAL 130810
10000007 Albert A CAMPAIGN 160710
10000007 Albert A CAMPAIGN 230710
10000007 Albert A NORMAL 300710
10000008 Lisa L CAMPAIGN 230710
10000008 Lisa L CAMPAIGN 300710
10000008 Lisa L CAMPAIGN 100810
10000008 Lisa L CAMPAIGN 130810
10000009 Kal L CAMPAIGN 230710
10000009 Kal L NORMAL 300710
10000009 Kal L NORMAL 100810
10000009 Kal L CAMPAIGN 130810
10000009 Kal L SPECIAL 150910
;
proc sort ;
by bp dset ;
run ;
data changes ;
changes= 0;
do until(last.bp) ;      
set one_week ;     
by bp abb notsorted ;     
changes +(first.abb and not first.bp) ;
end ;
if changes ; /*这句话是排除没有变化的观测对象*/
keep bp name: changes abb dset;
rename abb = latest_abb dset=latest_dset ;
run;
这个里面注意几个地方:

1.DO UNTIL循环对ONE_WEEK的每一个观测进行计算CHANGES

2.黄色标注的地方,FIRST.ABB和NOT FIRST.BP,确定变化的ABB

3.IF CHANGES 就是CHANGES不为0或者缺失均为入选的观测对象

4.KEEP里面的 name:后面的冒号其实就是保留前缀为NAME的变量。

5.specifies that observations with the same BY value are grouped together but are not necessarily sorted in alphabetical or numeric order. 这里的NOTSORTED是起到分组的功能而不是排序的功能。

13
crackman 发表于 2010-8-31 17:56:40
跟crackman读SAS程序(46)--读入数据中DELETE的应用


data have;
if _n_ = 1 then do;
   input x1 $ x2 $;
   delete;
end;
retain x1 x2;
input x3 $ x4 $@@;
if (anyalpha(x3) or anyalpha(x4)) then do;

x1 = x3; x2 = x4;
delete;
end;
cards;
a b
1 2 3 4
5 6 2 3
c d
1 3 4 5

;


运行之后的结果如下:

                                                         SAS 系统               2010年08月31日 星期二 上午01时10分10秒   1

                                               Obs    x1    x2    x3    x4

                                                1     a     b     1     2
                                                2     a     b     3     4
                                                3     a     b     5     6
                                                4     a     b     2     3
                                                5     c     d     1     3
                                                6     c     d     4     5
这里面最关键的是DELETE

Use the DELETE statement when it is easier to specify a condition that excludes observations from the data set or when there is no need to continue processing the DATA step statements for the current observation.

Use the subsetting IF statement when it is easier to specify a condition for including observations.

Do not confuse the DROP statement with the DELETE statement. The DROP statement excludes variables from an output data set; the DELETE statement excludes observations.

14
crackman 发表于 2010-8-31 17:56:56
跟crackman读SAS程序(47)--ASCII码

data crackman;
input x $;
datalines;
a
;
run;
data crackman;
set crackman;
xx=rank(x);
run;
proc print;
run;

使用RANK函数

15
crackman 发表于 2010-8-31 17:57:18
跟crackman读SAS程序(48)--分组连乘


data crackman;
input code     day       rate    ;
cards;
1             1               1.2              
1             2               1.5              
1             3               1.6              
2             1               1.5               
2             2               1.7                          
2             3               1.8              
;run;

data crackman;
set crackman; by code notsorted;
retain result 1;
if first.code then result=rate ;else
result=result*rate ;
proc print;
run;

其实这里面学习几个:

1.NOTSORTED只需要分组,不需要排序

2.first.VAR的应用

16
crackman 发表于 2010-8-31 17:57:41
跟crackman读SAS程序(49)--同一变量下的差值计算

data crackman;
input name date $ egg;
datalines;
1 3-1 2
1 3-2 3
1 3-3 5
1 3-4 1
2 3-1 1
2 3-2 3
3 3-1 3
3 3-2 4
3 3-3 2
4 3-1 1
4 3-2 1
5 3-1 5
5 3-1 4
5 3-2 7
5 3-3 3
;

data crackman;
set crackman;
by name;
change=dif(egg);
if first.name then change=.;
run;


同一变量下的值的差值

DIF函数的应用

proc print; run;

17
crackman 发表于 2010-8-31 17:58:06
跟crackman读SAS程序(50)--SAS如何整个读入 yyyy-mm-dd hh:mm:ss数据

DATA _NULL_;

X = '2003-01-12 23:11:32';

T =INPUT(scan(X, 1, ' '), yymmdd10.)*3600*24+input(scan(x,2, ' '), time8.);

DATE = DATEPART(T);

TIME = TIMEPART(T);

PUT X = T= T=DATETIME. DATE=date11. TIME=time. ;

RUN;
已有 1 人评分学术水平 热心指数 收起 理由
peijiamei + 4 + 3 精彩帖子

总评分: 学术水平 + 4  热心指数 + 3   查看全部评分

18
rdzr 发表于 2010-8-31 18:04:43
斑竹,偶也算是您的忠实粉丝吧,顶起来!

19
zhentao 发表于 2010-10-20 12:37:31
期期都受教。

最近好像更新不频了。

辛苦了。

20
crackman 发表于 2010-10-20 15:03:14

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-31 01:21