《SAS编程与数据挖掘商业案例》学习笔记之十九

前端之家收集整理的这篇文章主要介绍了《SAS编程与数据挖掘商业案例》学习笔记之十九前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

继续《SAS编程与数据挖掘商业案例》学习笔记,本文侧重数据处理实践,包括HASH对象、自定义format、以及功能强大的正则表达式

一:HASH对象

Hash对象又称散列表,是根据关键码值而直接进行访问的数据结构,是根据关键码值而直接进行访问的数据结构,

sas提供了两个类来处理哈希表,用于存储数据的hash和用于遍历的hiter,hash类提供了查找、添加修改删除方法hiter提供了用于定位和遍历的firstnext方法

优点:键值的查找是在内存中进行的,有利于提高性能

hash表可以在数据步运行时,动态的添加更新或删除观测;

hash表中可以很快的定位数据,减少查找次数

常用方法

definekey:定义键

Definedata:定义值

definedone:定义完成,可以载入数据

add添加键值,如在hash表中已存在,则忽略;

replace:如果健在hash表中存在,则替换,如果不存在则添加键值

remove:清除键值对

find:查找健值,如果存在则将值写入对应变量

check:查找键值,如果存在则返回rc=0,不修改当前变量的值;

output:将hash输出到数据集

clear:清空hash表,但并不删除对象

equal:判断两个hash类是否相等

find方法的示例:

libname chapt12 'f:\data_model\book_data\chapt12';

data results;

if _n_=0 then set chapt12.participants;

if _n_ = 1 then do;

declare hash h(dataset:'chapt12.participants');

h.definekey('name');

h.definedata('gender','treatment');

h.definedone();

end;

set chapt12.weight;

if h.find() = 0 then

output;

run;

hiter对象的引例:

data patients;

length patient_id $ 16 discharge 8;

input patient_id discharge:date9.;

datalines;

smith-4123 15mar2004

hagen-2834 23apr2004

smith-2437 15jan2004

flinn-2940 12feb2004

;

data _null_;

if _n_=0 then set patients;

declare hash ht(dataset:"patients",ordered:"ascending");

ht.definekey("patient_id");

ht.definedata("patient_id","discharge");

ht.definedone();

declare hiter iter("ht");

rc = iter.first();

do while (rc=0);

put patient_id discharge:date9.;

rc = iter.next();

run;

declare hiter iter("ht");hashht定义了一个遍历器iter,之后调用first方法将遍历器定位到hash表的第一条观测,然后使用next方法遍历hash表中的所有记录并输出

商业实战-两个数据集的合并:

data both1(drop=rc);

declare hash plan ();

rc = plan.definekey ('plan_id');

rc = plan.definedata ('plan_desc');

rc = plan.definedone ();

do until (eof1) ;

set chapt12.plans end = eof1;

rc = plan.add ();

do until (eof2) ;

set chapt12.members end = eof2;

call missing(plan_desc);

rc = plan.find ();

output;

stop;

上述程序可以简化为:

data both2;

length plan_id $3 plan_desc $20;

if _n_ = 1 then do;

declare hash h(dataset:'chapt12.plans');

h.definekey('plan_id');

h.definedata('plan_desc');

h.definedone();

call missing(plan_desc);

end;

set chapt12.members;

rc=h.find();

二:format

自定义format

Proc Format;

Value $ Sex_Fmt

'F'=''

'M'=''

Other = '未知';

Value Age_Dur

Low-10="10岁以下"

11-13="11-13"

14-<15="14-15"

15-High="15岁以上";

Run;

应用:

Datatest;

Setsashelp.class(keep=sex age);

x=put(sex,$sex_fmt);y=put(age,age_dur.);

三:正则表达式:

/.../一个正则表达式的起止;

|数项之间的选择,“或”运算;

()匹配组,标记一个子表达式的开始和结束位置;

.除换行符以外的任意字符;

\w任一单词字符,数字大小写字母以及下划线

\W任一非单词字符

\s任一空白字符,包括空格、制表符、换行符、回车符、中文全角空格等;

\S任一非空白字符,

\d0-9任一数字

\D任一非数字字符

[...]

[^...]

[a-z]az

[^a-z]不在从az范围内的任意字符

^匹配输入字符串的开始位置

$匹配输入字符串的结尾位置

\b描述单词的前或后边界

\B表示非单词边界

*匹配0次或多次

+匹配一次或多次

?匹配零次或一次

{n}匹配n

{n,}匹配n次以上

匹配nm

常用函数

Prxparse定义一个正则表达式

Prxmatch返回匹配模式的首次匹配位置

Call prxsubstr返回匹配模式在目标字符串的开始位置和长度

Prxposn返回正则表达式子表达式对应的匹配模式值

Callprxposn返回正则表达式子表达式对应的匹配模式和长度

Cal lprxnext返回匹配模式在目标字符串中的多个匹配位置和长度

Prxchange替代匹配模式的值

Call prxchange替代匹配模式的值

eg1

if _n_ = 1 then pattern_num = rxparse("/cat/");

retain pattern_num;

input string $30.;

position = rxmatch(pattern_num,string);

file print;

put pattern_num= string= position=;

there is a cat in this line.

does not match cat

cat in the beginning

at the end,a cat

cat

eg2:数据验证

data match_phone;

set chapt12.phone_numbers;

if _n_ = 1 then pattern = prxparse("/\(\d\d\d\) ?\d\d\d-\d{4}/");

retain pattern;

if prxmatch(pattern,phone) gt 0 then output;

找出不匹配的手机号码

data unmatch_phone;

where not prxmatch("/\(\d\d\d\) ?\d\d\d-\d{4}/",phone);

Eg3:提取匹配某种模式的字符串

data extract;

pattern = prxparse("/\(\d\d\d\) ?\d\d\d-\d{4}/");

if missing(pattern) then do;

put "error in compiling regular expression";

stop;

end;

length number $ 15;

input string $char80.;

call prxsubstr(pattern,string,start,length);

if start gt 0 then do;

number = substr (string,length);

number = compress(number," ");

output;

keep number;

this line does not have any phone numbers on it

this line does: (123)345-4567 la di la di la

also valid (123) 999-9999

two numbers here (333)444-5555 and (800)123-4567

eg4提取名字

data ReversedNames;

input name & $32.;

datalines;

Jones,Fred

Kavich,Kate

Turley,Ron

Dulix,Yolanda

data FirstLastNames;

length first last $ 16;

keep first last;

retain re;

if _N_ = 1 then

re = prxparse('/(\w+),(\w+)/');

set ReversedNames;

if prxmatch(re,name) then

do;

last = prxposn(re,1,name);

first = prxposn(re,2,255)">注:1,2分别代表正则表达式中的两个组

eg5提取符合规定的名字

data old;

input name $60.;

Judith S Reaveley

Ralph F. Morgan

Jess Ennis

Carol Echols

Kelly Hansen Huff

Judith

Nick

Jones

data new;

length first middle last $ 40;

re1 = prxparse('/(\S+)\s+([^\s]+\s+)?(\S+)/o');

re2 = prxparse('/(\S+)(\s+)([^\s]+\s+)(?)(\S+)/o');

set old;

id1=prxmatch(re1,255)">id2=prxmatch(re2,255)">if id1 then

first = prxposn(re1,255)">middle = prxposn(re1,255)">last = prxposn(re1,3,255)">if id2 then test=prxposn(re1,4,255)">put test=;

Eg6:返回匹配模式的多个位置

expressionid = prxparse('/[crb]at/');

text = 'the woods have a bat,cat,and a rat!';

start = 1;

stop = length(text);

call prxnext(expressionid,stop,text,position,255)">do while (position > 0);

found = substr(text,255)">put found= position= length=;

call prxnext(expressionid,255)">注:首次执行call prxnext返回一个position,然后进入循环,在抽取满足条件的子串中,再次执行all prxnext,此时会返回下一个匹配的position

Eg7:替换文本

data cat_and_mouse;

input text $char40.;

length new_text $ 80;

if _n_ = 1 then match = prxparse("s/[Cc]at/mouse/");

retain match;

call prxchange(match,-1,new_text,len,trunc,num);

if trunc then put "note: new_text was truncated";

the Cat in the hat

there are two cat cats in this line

here is no replacement

run;

原文链接:https://www.f2er.com/regex/361222.html

猜你在找的正则表达式相关文章