您的位置:首页 > Web前端

sas中的sql(2) 行选择 、限制重复、条件运算符、运行前语法检查、feedback、count

2014-11-11 14:40 141 查看
1:获取数据集前几行观测

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

只计算非缺失值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: