当前位置:网站首页>Learning notes of SAS programming and data mining business case 19
Learning notes of SAS programming and data mining business case 19
2022-07-05 20:51:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack , I've prepared for you today Idea Registration code .
continue 《SAS Programming with data Mining business Case study 》 Study note , This paper focuses on data processing practice . contain :HASH object 、 Define your own format、 And powerful regular expressions
One :HASH object
Hash Objects are also called hash tables , It is a data structure that can be accessed directly according to the key value . It is a data structure that can be accessed directly according to the key value .
sas Two classes are provided to handle hash tables . Used to store data hash And for traversal hiter,hash Class provides a way to find 、 Join in 、 changes 、 Delete and so on ,hiter Provides for positioning and traversal first、next Other methods .
Strengths : The key value is searched in memory , It's good for improving performance ;
hash Tables can be executed in data steps , Dynamically add, update or delete observations .
hash The table can locate data very quickly , Reduce the number of searches .
Often used :
definekey: Define key
Definedata: Definition value
definedone: That's it . Able to load data
add: Add key value . If in hash Table already exists , It ignores ;
replace: Suppose you are alive hash Presence in table , The replacement . If it does not exist, add the key value
remove: Clear key value pair
find: Find health value , If it exists, write the value to the corresponding variable
check: Find key values , If it exists, it returns rc=0, Do not change the value of the current variable ;
output: take hash Table output to dataset
clear: Empty hash surface , But the object is not deleted
equal: Infer two hash Are classes equal
find Demonstration examples of methods :
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 An example of an object :
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();
end;
run;
use declare hiter iter(“ht”); to hash surface ht Defines a traverser iter, Then call first Method locates the iterator to hash The first observation of the table , And then use next Methods through hash All records in the table and output .
Business practice – Merging of two data sets :
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 ();
end;
do until (eof2) ;
set chapt12.members end = eof2;
call missing(plan_desc);
rc = plan.find ();
output;
end;
stop;
run;
The above procedure can be simplified to :
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();
run;
Two :format
Define your own format:
Proc Format;
Value $ Sex_Fmt
‘F’=’ Woman ‘
‘M’=’ male ‘
Other = ‘ Unknown ‘;
Value Age_Dur
Low-10=”10 Under the age of “
11-13=”11-13 year “
14-<15=”14-15″
15-High=”15 Years of age or older “;
Run;
application :
Data test;
Set sashelp.class(keep=sex age);
x=put(sex,$sex_fmt);y=put(age,age_dur.);
Run;
3、 ... and : Regular expressions :
/…/ The beginning and end of a regular expression .
| Choice between several items ,“ or ” operation ;
() Match group , Mark the beginning and end of a subexpression .
. Random characters other than line breaks .
\w Any word character , Numbers, upper and lower case letters, and underscores
\W Any non word character
\s Any white space character , Include spaces 、 tabs 、 A newline 、 A carriage return 、 Chinese full corner space, etc ;
\S Any non white space character ,
\d 0-9 Any number
\D Any non numeric character
[…]
[^…]
[a-z] from a To z
[^a-z] Not from a To z Random characters in the range
^ Match the starting position of the input string
$ Matches the end of the input string
\b Describe the front or back boundaries of narrative words
\B Indicates a non word boundary
* matching 0 Times or times
+ Match once or more
? Match zero times or once
{n} matching n Time
{n,} matching n More than once
{n,m} matching n To m Time
Functions are often used :
Prxparse Define a regular expression
Prxmatch Return the first matching position of the matching pattern
Call prxsubstr Returns the starting position and length of the matching pattern in the target string
Prxposn Returns the matching pattern value corresponding to the regular expression sub expression
Call prxposn Returns the corresponding matching pattern and length of the regular expression sub expression
Cal l prxnext Returns multiple matching positions and lengths of the matching pattern in the target string
Prxchange Replace the value of the matching pattern
Call prxchange Replace the value of the matching pattern
eg1:
data _null_;
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=;
datalines;
there is a cat in this line.
does not match cat
cat in the beginning
at the end, a cat
cat
;
run;
eg2: data validation
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;
run;
Find out the mismatched mobile phone number
data unmatch_phone;
set chapt12.phone_numbers;
where not prxmatch(“/\(\d\d\d\) ?
\d\d\d-\d{4}/”,phone);
run;
Eg3: Extract a string that matches a pattern
data extract;
if _n_ = 1 then do;
pattern = prxparse(“/\(\d\d\d\) ?
\d\d\d-\d{4}/”);
if missing(pattern) then do;
put “error in compiling regular expression”;
stop;
end;
end;
retain pattern;
length number $ 15;
input string $char80.;
call prxsubstr(pattern,string,start,length);
if start gt 0 then do;
number = substr (string,start,length);
number = compress(number,” “);
output;
end;
keep number;
datalines;
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
;
run;
eg4: Pick up the name
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, name);
end;
run;
notes :1,2 Each represents two groups in the regular expression
eg5: Extract qualified names
data old;
input name $60.;
datalines;
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, name);
id2=prxmatch(re2, name);
if id1 then
do;
first = prxposn(re1, 1, name);
middle = prxposn(re1, 2, name);
last = prxposn(re1, 3, name);
end;
if id2 then test=prxposn(re1, 4, name);
put test=;
run;
Eg6: Return multiple locations of the matching pattern
data _null_;
expressionid = prxparse(‘/[crb]at/’);
text = ‘the woods have a bat, cat, and a rat!’;
start = 1;
stop = length(text);
call prxnext(expressionid, start, stop, text, position, length);
do while (position > 0);
found = substr(text, position, length);
put found= position= length=;
call prxnext(expressionid, start, stop, text, position, length);
end;
run;
notes : First run call prxnext Return to one position, Then enter the cycle , In extracting substrings that meet the conditions . Run again all prxnext, The next matching position;
Eg7: replace text
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,text,new_text,len,trunc,num);
if trunc then put “note: new_text was truncated”;
datalines;
the Cat in the hat
there are two cat cats in this line
here is no replacement
;
run;
Copyright notice : This article is an original blog article . Blog , Without consent , Shall not be reproduced .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117664.html Link to the original text :https://javaforall.cn
边栏推荐
- XML建模
- How to renew NPDP? Here comes the operation guide!
- 基于AVFoundation实现视频录制的两种方式
- Make Jar, Not War
- 从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
- Which securities is better for securities account opening? Is online account opening safe?
- ts 之 属性的修饰符public、private、protect
- [record of question brushing] 1 Sum of two numbers
- 《SAS编程和数据挖掘商业案例》学习笔记# 19
- 实现浏览页面时校验用户是否已经完成登录的功能
猜你喜欢
Abnova DNA marker high quality control test program
当Steam教育进入个性化信息技术课程
IC popular science article: those things about Eco
Duchefa丨低熔点琼脂糖 PPC中英文说明书
Abnova丨培养细胞总 RNA 纯化试剂盒中英文说明书
Duchefa low melting point agarose PPC Chinese and English instructions
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
Which is the best online collaboration product? Microsoft loop, notion, flowus
PHP反序列化+MD5碰撞
leetcode:1139. 最大的以 1 为边界的正方形
随机推荐
leetcode:1755. 最接近目标值的子序列和
Where is a good stock account? Is online account manager safe to open an account
解读协作型机器人的日常应用功能
3.3 project evaluation
Duchefa丨MS培养基含维生素说明书
研学旅游实践教育的开展助力文旅产业发展
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
Selenium element information
bazel是否有学习的必要
Chemical properties and application instructions of prosci Lag3 antibody
ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
Is it safe to open a stock account by mobile phone? My home is relatively remote. Is there a better way to open an account?
wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
Duchefa MS medium contains vitamin instructions
Abnova 环孢素A单克隆抗体,及其研究工具
珍爱网微服务底层框架演进从开源组件封装到自研
haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践
Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
NPDP如何续证?操作指南来了!