sas中的sql(2) 行选择 、限制重复、条件运算符、运行前语法检查、feedback、count
2014-11-11 14:40
141 查看
1:获取数据集前几行观测
[b]2:EliminatingDuplicateRowsfromOutput[/b]
DISTINCT:appliestoallcolumns,andonlythosecolumns,thatarelistedintheSELECTclause.
注意这里一个细节,distinct的变量会默认排序
[b]3:条件运算符[/b]
Tocreateanegativecondition,youcanprecedeanyoftheseconditionaloperators,exceptforANYandALL,withtheNOToperator.
3.1:BETWEENvalue-1ANDvalue-2(betweenorequalto两端的value是被包括进去的)
Toselectrowsbasedonarangeofnumericorcharactervalues(value可以使数字也可以是字符),Whenspecifyingthelimitsfortherangeofvalues,itisnotnecessarytospecifythesmallervaluefirst.(value-1/2的大小无要求)
3.2:UsingtheCONTAINSorQuestionMark(?)OperatortoSelectaString
sql-expressionCONTAINS/?sql-expression
wheresql-expressionisacharactercolumn,string(characterconstant),orexpression(contain某些东西的列是字符型)
3.3:INOperatortoSelectValuesfromaList
columnIN(constant-1<,...constant-n>)
constant-1andconstant-nrepresentalistthatcontainsoneormorespecificvalues.(括号中的常量个数大于等于1)
3.4:ISMISSINGorISNULLOperatortoSelectMissingValues
Toselectrowsthatcontainmissingvalues,bothcharacterandnumeric,usetheISMISSINGorISNULLoperator.Theseoperatorsareinterchangeable.
(字符型和数值型缺失都可检验,这两个符号是等价的)
wherecolumn='';wherecolumn=.;分别只能检验字符型和数值型缺失。
3.5:LIKEOperatortoSelectaPattern
columnLIKE'pattern'
underscore(_)anysinglecharacter
percentsign(%)anysequenceofzeroormorecharacters
3.6:UsingtheSounds-Like(=*)OperatortoSelectaSpellingVariation
Thesounds-like(=*)operatorusestheSOUNDEXalgorithmtocompareeachvalueofacolumn(orothersql-expression)withthewordorwords(orothersql-expression)thatyouspecify.
3.7:SubsettingRowsbyUsingCalculatedValues(sas特有的,不是标准sql中的)
sas编译时,先执行where,如果不用calculated那么就会报错说没有total这个变量,加上后会在新生成的变量中查找。
3.8:UsingtheANYOperator
wheredateofbirth<any(subquery...)
<anyequaltomax()比如,子查询返回20、30、40,那么,外查询选择所有<40的记录
>anyequaltomin() 比如,子查询返回20、30、40,那么,外查询选择所有>20的记录
=anyequaltoin
3.9:UsingtheALLOperator
all和any相反
3.10:exsits、notexsits
对于exsits,为真的话就输出,假的就不输出。
对于notexsits相反。
4、NOEXEC、VALIDATE;
相同点:这两个关键字都有使程序不执行,只进行语法检查的效果!
不同点:validate只对紧跟其后的select语句有效,noexec对真个sql过程有效
procsql;
validate
selectempid,jobcode,salary
fromsasuser.payrollmaster
wherejobcodecontains'NA'
orderbysalary;
quit;
4.1[b]feedback,在列比较多,我用来查看列名。。。。然后复制粘贴我想要的名字。。。[/b]
4.2Count
只计算非缺失值
procsqloutobs=5;*outobs选项只限制显示的行数,并不限制读入的行数.inobs=选项可以限制读入的行数; select* fromsashelp.class; quit; datares; setsashelp.class(obs=5); run;
[b]2:EliminatingDuplicateRowsfromOutput[/b]
DISTINCT:appliestoallcolumns,andonlythosecolumns,thatarelistedintheSELECTclause.
注意这里一个细节,distinct的变量会默认排序
procsql; selectdistinctflightnumber,destination/*distinct只能跟在select后*/ fromsasuser.internationalflights; quit;
[b]3:条件运算符[/b]
Tocreateanegativecondition,youcanprecedeanyoftheseconditionaloperators,exceptforANYandALL,withtheNOToperator.
3.1:BETWEENvalue-1ANDvalue-2(betweenorequalto两端的value是被包括进去的)
Toselectrowsbasedonarangeofnumericorcharactervalues(value可以使数字也可以是字符),Whenspecifyingthelimitsfortherangeofvalues,itisnotnecessarytospecifythesmallervaluefirst.(value-1/2的大小无要求)
3.2:UsingtheCONTAINSorQuestionMark(?)OperatortoSelectaString
sql-expressionCONTAINS/?sql-expression
wheresql-expressionisacharactercolumn,string(characterconstant),orexpression(contain某些东西的列是字符型)
procsqloutobs=10; selectname fromsasuser.frequentflyers wherenamecontains'ER'; quit;
3.3:INOperatortoSelectValuesfromaList
columnIN(constant-1<,...constant-n>)
constant-1andconstant-nrepresentalistthatcontainsoneormorespecificvalues.(括号中的常量个数大于等于1)
3.4:ISMISSINGorISNULLOperatortoSelectMissingValues
Toselectrowsthatcontainmissingvalues,bothcharacterandnumeric,usetheISMISSINGorISNULLoperator.Theseoperatorsareinterchangeable.
(字符型和数值型缺失都可检验,这两个符号是等价的)
wherecolumn='';wherecolumn=.;分别只能检验字符型和数值型缺失。
3.5:LIKEOperatortoSelectaPattern
columnLIKE'pattern'
underscore(_)anysinglecharacter
percentsign(%)anysequenceofzeroormorecharacters
procsql; selectffid,name,address fromsasuser.frequentflyers whereaddresslike'%P%PLACE';*空格也包含在字符串中; quit;
3.6:UsingtheSounds-Like(=*)OperatortoSelectaSpellingVariation
Thesounds-like(=*)operatorusestheSOUNDEXalgorithmtocompareeachvalueofacolumn(orothersql-expression)withthewordorwords(orothersql-expression)thatyouspecify.
3.7:SubsettingRowsbyUsingCalculatedValues(sas特有的,不是标准sql中的)
sas编译时,先执行where,如果不用calculated那么就会报错说没有total这个变量,加上后会在新生成的变量中查找。
procsqloutobs=10; selectflightnumber,date,destination, boarded+transferred+nonrevenueasTotal, calculatedTotal/2ashalf fromsasuser.marchflights wherecalculatedtotal<100;/*想要使用新生成的列的时候,需要加上calculated关键字,having要加orderby不用加*/
3.8:UsingtheANYOperator
wheredateofbirth<any(subquery...)
<anyequaltomax()比如,子查询返回20、30、40,那么,外查询选择所有<40的记录
>anyequaltomin() 比如,子查询返回20、30、40,那么,外查询选择所有>20的记录
=anyequaltoin
3.9:UsingtheALLOperator
all和any相反
3.10:exsits、notexsits
对于exsits,为真的话就输出,假的就不输出。
对于notexsits相反。
/*需求,选择是员工又是经常单独飞行的人姓名*/ procsql;title'FrequentFlyersWhoAreEmployees'; selectname fromsasuser.frequentflyers whereexists (select*fromsasuser.staffmaster wherename=trim(lastname)||','||firstname) orderbyname; quit;
4、NOEXEC、VALIDATE;
相同点:这两个关键字都有使程序不执行,只进行语法检查的效果!
不同点:validate只对紧跟其后的select语句有效,noexec对真个sql过程有效
procsqlnoexec; selectempid,jobcode,salary fromsasuser.payrollmaster wherejobcodecontains'NA' orderbysalary; quit;
procsql;
validate
selectempid,jobcode,salary
fromsasuser.payrollmaster
wherejobcodecontains'NA'
orderbysalary;
quit;
4.1[b]feedback,在列比较多,我用来查看列名。。。。然后复制粘贴我想要的名字。。。[/b]
*optionsfullstimer=on;*run;
procsqlfeedback;
select*fromsashelp.class;
NOTE:语句变换为:
selectCLASS.Name,CLASS.Sex,CLASS.Age,CLASS.Height,CLASS.Weight
fromSASHELP.CLASS;
quit;
4.2Count
只计算非缺失值
相关文章推荐
- 在SQL中删除重复记录(多种方法) 选择自 55015182 的 Blog
- T-SQL无重复选择记录
- remove_constans() ——检查共享池总的sql运行情况。
- 【SAS proc sql】选择字段可添加的参数
- 检查重复语句的sql二中比较
- ORACLE 常用的SQL语法和数据对象 选择自 i_like_database 的 Blog
- Oracle EBS-SQL (MRP-7):检查MRP计划运行报错原因之超大数据查询2.sql
- 摇滚21 选择标签至文本域效果,可多选/可过滤重复/可限制个数(已封装)
- SQL – 5.数据分组 + 6.限制结果集行数 + 7.抑制重复数据
- 检查数据库当前运行的SQL
- mysql可以运行在不同sql mode模式下面,sql mode模式定义了mysql应该支持的sql语法,数据校验等
- Sql 语法检查
- 基于jquery的选择标签至文本域效果,可多选/可过滤重复/可限制个数的实现代码
- 在SQL中删除重复记录(多种方法) 选择自 55015182 的 Blog
- Set noexec on 与 Set parseonly on的区别及SQL语法检查
- .Net 中用查找窗体标题的方法检查WinForm应用是否重复运行
- sql语句中选取限制字段的不同可能会对运行效率产生质的影响
- SQL选择不重复记录
- 用Semisynchronous Replication半同步,mk-table-checksum数据同步一致性检查,监控主从同步运行状态的脚本等方式加强SQL性能和数据审核
- Oracle EBS-SQL (MRP-6):检查MRP计划运行报错原因之超大数据查询1.sql