天天看点

SAS编程基础(2):常用 数据步与过程步

data students (keep=name sex class address);

length address $30;

input id name $ sex $ class address;

cards;

1001 高溪红 F 1 衡阳神龙大酒店

1002 张明明 M 2 北京市东城区

;

run;

libname jx “d:\jx”;

%let dir=“d:\jx\cust.bat”;

filename fil “(&dir)”;

data jx.open_bj jx.other;

infile fil;

input zone $ open_count city $;

if city in (“北京”, “青岛”) then output jx.open_bj;

else output jx.other;

run;

libname jx “d:\jx”;

%let dir=“d:\jx\cust.bat”;

filename fil “(&dir)”;

data cust;

infile fil;

input zone $ open_count city $;

run;

data jx.bj

set cust (where=(city in (“北京”, “青岛”);

run;

%let dir=“d:\jx\custer.bat”;

filename fil “(&dir)”;

libname jx “d:\jx”;

data jx.card138;

infile fil dsd missover;

input @20 card_type $3. @;

if card_type=“138” then do;

input @1 qh $3.

@4 card_nm $16.

@23 name $8.

@31 address :$20.

;

ouput jx.card138;

end;

run;

%let dir=“d:\jx\custer.bat”;

filename fil “(&dir)”;

libname jx “d:\jx”;

data jx.card138;

infile fil dsd missover;

input @20 card_type $3. @;

if card_type=“138” then delete;

if card_type^=“138” then do;

input @1 qh $3.

@4 card_nm $16.

@23 name $8.

@31 address :$20.

;

ouput jx.card138;

end;

run;

%let dir=“d:\jx\inf_cust.dat”;

filename sj “(&dir)”;

libname csj “d:\jx”;

%macro loadfile(v_lib);

data &v_lib.inf_custer;

infile sj firstobs=2 end=final length=length;

input @1 qh $3.

@4 card_nm $16.

@20 card_type $3.

@23 name $8.

@31 address :$20.

;

run;

%mend;

%%loadfile(csj.);

%do %while(%quote(&dsname) ne %quote());

%if &name>2 %then %do;

or

%end;

index(%scan(&syspbuff, 1), “&dsname”)>0

%let num=%eval(&num+1);

%let dsname=%scan(&syspbuff, &num);

%end;

%mend;

data city;

set city_inf;

where %indexes(city, 北京, 上海, 山东);

run;

proc print data=city;

run;

%macro callpro(v_param);

data null;

x=“we”;

z=&v_param;

call symput(“v_var”, x);

%mend callpro;

%callpro(1);

run;

data temp;

y="&v_var";

run;

proc print data=temp;

run;

/* 关系型数据库数据处理 /

/ 链接关系型数据库的两种方式

  1. 创建永久逻辑库 读取
  2. 通过 pass through 的方式,其就是在 proc sql 里面 连接操作mysql数据库

    /

    /

    libname test mysql user=root password=sas123 database=mydb

    server=localhost port=3306;

    /

    /

    proc sql;

    connect to mysql

    (user=root password=sas123 server=localhost

    database=world port=3306);

    create table b as select * from connection to mysql

    (select * from city);

    execute(create table cc as select * from aa
               

    )by mysql;

    disconnect from mysql;

    quit;

    */

data score;

input id name $ class math english chinese;

cards;

1001 高明 1 89 78 89

1002 中海 1 76 99 78

1003 刘海洋 1 88 56 66

1004 杨小帅 1 99 89 98

1005 赵晓红 1 87 86 83

1006 马西瑞 1 89 58 43

;

run;

proc means data=score sum min max;

var math english chinese;

run;

data score;

input id name $ class math english chinese;

cards;

1001 高明 1 89 78 89

1002 中海 1 76 99 78

1003 刘海洋 1 88 56 66

1004 杨小帅 1 99 89 98

1005 赵晓红 1 87 86 83

1006 马西瑞 1 89 58 43

;

run;

proc print data=score noobs;

where math>80 and english>80 and chinese>80;

titile “各科成绩大于80分的学生信息”;

run;

data students;

input group age height weight sex $;

cards;

2 35 162 42 f

1 31 173 43 m

2 42 156 56 f

1 53 152 39 f

1 42 173 63 m

1 28 165 55 f

2 33 157 66 f

2 17 162 46 f

1 16 173 45 m

1 25 180 66 m

;

run;

proc sort data=students;

by group;

proc summary data=students mean std n max min range stderr cv;

var age height weight;

class sex;

by group;

output out=stu_analy;

proc print data=stu_analy;

run;