博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【SAS ADVANCE】Performing Queries Using PROC SQL
阅读量:4658 次
发布时间:2019-06-09

本文共 3230 字,大约阅读时间需要 10 分钟。

SQL: Structured Query Language

一、Objectives in this chapter:

  • invoke the SQL procedure
  • select columns
  • define new columns
  • specify the table(s) to read
  • specify subsetting criteria
  • order rows by values of one or more columns
  • group results by values of one or more columns
  • end the SQL procedure
  • summarize data 
  • generate a report as the output of a query
  • create a table of the output of a query

二、PROC SQL有何不同?

  • 与其他PROC步骤不同的是,PROC SQL由很多个子句构成。最常见的情形是PROC SQL代码中包含两个语句:第一季是PROC SQL;第二季是SELECT语句,而SELECT语句中就包含多了子句,例如:SELECT ...,.. FROM ...,....WEHER和ORDER BY;
  • PROC SQL过程不需要RUN语句,PROC SQL自动执行每一个查询;
  • 建议在末尾加上QUIT语句来结束运行;

三、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        
/*subsets the data based on a condition*/6
> /*classifies the data into groups based on the specifyed column*/7
>; /*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 bonus3      from sasuser.payrollmaster4      where salary<320005      order by 2;

 

 

4)在order by语句中,通过逗号列出变量,进行多变量排序

1  proc sql;2       select empid,jobcode,salary,salary*.06 as bonus3       from sasuser.payrollmaster4       where salary<320005       order by jobcode,empid;/order by 2,empid;

 

 

六、join two or more tables 

  • 若需要join两个或多个tables,list the columns that you want to select from both tables in the SELECT clause.
  • Seperate all column names with commas.
  • 若用户需要引用的变量名出现在多个表中,则需要在该变量前加上表明的前缀,来指定其表名;
  • 若用户需要连接多个表,则需在from子句中加上多了表名,并用逗号隔开;
    1 proc sql;2     select salcomp.empid, lastname,3                newsals.salary,mewsalay4     from sasuser.salcomps, sasuser.newsals5     where salcomps.empid=newsals.empid6     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 TotalMiles4      from sasuser.frequentflyer5     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 Avg3          from sasuser.payrollmaster4          group by jobcode5          having avy(salary)>400006          order by jobcode;

 

转载于:https://www.cnblogs.com/chenyn68/p/3896142.html

你可能感兴趣的文章
Linux下常用的压缩与解压命令
查看>>
第一周的博客作业
查看>>
linux的awk命令解读
查看>>
JavaScript内部原理实践——真的懂JavaScript吗?(转)
查看>>
android5.0之toolBar
查看>>
POJ--2689-C++
查看>>
better-scroll的使用方法,动态创建dom使用better-scroll
查看>>
PHP中的面向对象魔术方法大全
查看>>
数据库常用函数详解
查看>>
jquery 监听不起效果的小问题汇总
查看>>
eclipse构建及运行maven web项目
查看>>
Photoshop 图文并茂常用快捷键
查看>>
linux基础命令2(ls,cd)
查看>>
面向对象初识
查看>>
Word 2010中查找和替换功能高级技巧(转)
查看>>
优先队列
查看>>
一起学wp7 XNA游戏开发
查看>>
堆内存破坏检测实战--附完整调试过程
查看>>
【knockoutjs】 Computed VS Pure Computed 区别
查看>>
JS向数组中添加/删除元素
查看>>