标签:des style blog color io strong for ar
SQL: Structured Query Language
一、Objectives in this chapter:
二、PROC SQL有何不同?
三、PROC SQL的基本格式
LIBNAME libref ‘SAS-data-library‘;
1 PROC SQL; /*invoke the SQL procedure*/
2 CREATE TABLE table-name AS
3 SELECT column-1<,...,column-n> /*specifies the column(s) to be selectd*/ 4 from table-1|view-1<,...,table-n|view-n> /*specifies the table(s) to be queried*/ 5 <WHERE expression> /*subsets the data based on a condition*/ 6 <GROUP BY column-1<,...column-n>> /*classifies the data into groups based on the specifyed column*/ 7 <ORDER BY column-1<,...column-n>>; /*sorts the rows that the query returns by the values of the specified columns*/
8 QUIT;
【注意事项】:
SELECT语句包含多个子句,以分号结束
四、PROC SQL输出
PROC SQL语句 | TYPE Of Output |
SELECT | report |
CREATE TABLE and SELECT | table |
CREATE VIEW AND SELECT | PROC SQL VIEW |
五、Order by语句中的编程注意事项
1) 常常放在select语句的最后;
2) 若用户需要逆序排序,则需将关键词DESC放在order by语句中需要逆序排列的变量后面,例如:
1 order by jobcode desc;
3) 在order by子句中,用户可通过指定select语句中变量的位置来代替其变量名,例如:
1 proc sql; 2 select empid,jobcode,salary,salary*.06 as bonus 3 from sasuser.payrollmaster 4 where salary<32000 5 order by 2;
4)在order by语句中,通过逗号列出变量,进行多变量排序
1 proc sql; 2 select empid,jobcode,salary,salary*.06 as bonus 3 from sasuser.payrollmaster 4 where salary<32000 5 order by jobcode,empid;/order by 2,empid;
六、join two or more tables
1 proc sql; 2 select salcomp.empid, lastname, 3 newsals.salary,mewsalay 4 from sasuser.salcomps, sasuser.newsals 5 where salcomps.empid=newsals.empid 6 order by lastname;
七、Summarizing Groups of Data
If you specify a GROUP BY clause in a query that does not contain a summary function, your clause is changed to an ORDER BY clause, and a message to the effect is written to the SAS log.
1 proc sql; 2 select membertype 3 sum(milestraveled) as TotalMiles 4 from sasuser.frequentflyer 5 group by membertype; 6 /*输出每个membertype的milestraveled的和*/
八、HAVING语句:在含有GROUP BY子句的PROC SQL中筛选行
A HAVING clause works with the GROUP BY clause to restrict the groups that are displayed in the output,based one or more specified conditions.
1 proc sql; 2 select jobcode, avg(salary) as Avg 3 from sasuser.payrollmaster 4 group by jobcode 5 having avy(salary)>40000 6 order by jobcode;
【SAS ADVANCE】Performing Queries Using PROC SQL,布布扣,bubuko.com
【SAS ADVANCE】Performing Queries Using PROC SQL
标签:des style blog color io strong for ar
原文地址:http://www.cnblogs.com/chenyn68/p/3896142.html