1. 程式人生 > >1-SAS程式設計基礎

1-SAS程式設計基礎

1-SAS程式設計基礎

臨時sas資料集

data temp;
input x [email protected]@;
cards;
34 56 78 90 35 67 89 10 23 65 77 45
;
run;
data work.temp;
input x [email protected]@;
cards;
34 56 78 90 35 67 89 10 23 65 77 45
;
run;

在這裡插入圖片描述

永久sas資料集

Libname AA 'd:\SAS';         /*定義d盤SAS目錄的邏輯名為AA*/
Data AA.Females;            /*在上述目錄中建立名為FEMALES的永久型資料集*/
Input a b c; /*定義輸入變數a,b,c*/ Cards; /*直接輸入資料,資料塊開始*/ 3 1.3 0.5 /* 資料塊*/ 4 1.5 0.9 9 1.9 0.4 ; /* 資料塊結束*/ Run; /*結束SAS程式*/ Data h; /* 建立臨時資料集h*/ Set AA.Females; /* 該資料集中的資料來自永久資料集Females*/
Run; /*結束SAS程式*/

資料集的拆分

Data score;
input name $ score ;
cards;
john 70
hop 83
leseen 89
like 63
;
run;
Data high low;
Set score;
if score<80 then output low;
else output high;
run;
proc print data=high;
title 'high score';
run;
proc print data=low;
title 'low score';
run;

在這裡插入圖片描述

set命令

data male;
input id sex $ math English;
cards;
12 m 80 90
13 m 76 86
14 m 96 73
;
data female;
input id sex $ math English Chinese;
cards;
15 F 93 76 92
16 F 89 78 83
17 F 86 63 89
;
data score;
set male female;
proc print;
run;

在這裡插入圖片描述

update語句

data master;                                          /*建立主資料集*/
input id name $ sex age weight;
cards;
11 jones 1 46 117.2
14 milier 2 59 132.2
30 parker 1 29 111.3
49 smith 1 34 209.1
87 wilson 2 30 98.3
;
run;
data trans;                                            /*建立處理資料集*/
input id  age weight name [email protected]@;
cards;
11 47 . .
30 . 108.4 .
49 35 215.1 .
87 . . cameron
87 . 104.1 .
;
run;
data new;                                /*利用處理資料集修改主資料集*/
update master trans;by id;run;
proc print;run;                        /*顯示修改後的資料集*/

在這裡插入圖片描述

where語句

data a;
input id [email protected]@;
cards;
11 10 31 30 41 40
;
data b;
input id [email protected]@;
cards;
11 100 21 200 41 400
;
data whereab;
merge a b;
where id>30;
proc print data=whereab;
run;
data ifab;
merge a b;
if id>30;
proc print data=ifab;
run;

在這裡插入圖片描述

merge命令

data me;
input id sex $ math;
cards;
12 m 80
13 m 76
14 m 96
;
data mad;
input id sex $ English Chinese;
cards;
12 F 93 76
13 F 89 78
;
data score;
merge me mad;
run;
proc print;
run;

在這裡插入圖片描述

merge and by 命令

data identity;
input id sex $ age;
cards;
1001 f 20
1003 m 22
1002 m 20
1001 f 21
;
proc sort;
by id;
run;
data score;
input id language $ score;
cards;
1001 English 90
1002 Chinese 93
10001 France 83
;
proc sort;
by id;
run;
data result;
merge identity score;
by id;
run;
proc print data=result;
run;

在這裡插入圖片描述

sas函式

data b;
input hhid sex$ birthday$ intvday$;
bdm=substr(birthday,3,2)*1;
bdy=substr(birthday,1,2)*1;
bdd=substr(birthday,5,2)*1;
idm=substr(intvday,3,2)*1;
idy=substr(intvday,1,2)*1;
idd=substr(intvday,5,2)*1;
age=(mdy(idm,idd,idy)-mdy(bdm,bdd,bdy))/365.25;
cards;
4124316 2 750603 971201
4124420 1 691002 971205
4324320 2 490820 971208
4324415 1 790610 971215
4224210 2 291025 981220
4524215 1 870710 991225
;
proc print;
run;

在這裡插入圖片描述