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

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

1:获取数据集前几行观测

proc sql outobs=5; *outobs选项只限制显示的行数,并不限制读入的行数. inobs=选项可以限制读入的行数;select * from sashelp.class;
quit;data res;set sashelp.class (obs=5); 
run;

2:Eliminating Duplicate Rows from Output  

DISTINCT : applies to all columns, and only those columns, that are listed in the SELECT clause.

注意这里一个细节,distinct的变量会默认排序

proc sql;    
select distinct flightnumber, destination     /*distinct只能跟在select后*/
from sasuser.internationalflights; 
quit;

 

3:条件运算符

 To create a negative condition, you can precede any of these conditional operators, except for ANY and ALL, with the NOT operator. 

3.1:BETWEEN value-1 AND value-2  ( between or equal to 两端的value是被包括进去的)

To select rows based on a range of numeric or character values(value可以使数字也可以是字符),When specifying the limits for the range of values, it is not necessary to specify the smaller value first. (value-1/2的大小无要求

 

3.2:Using the CONTAINS or Question Mark (?) Operator to Select a String  

sql-expression CONTAINS/? sql-expression 

where sql-expression is a character column, string (character constant), or expression(contain某些东西的列是字符型)

  proc sql outobs=10;   select name        from sasuser.frequentflyers       where name contains 'ER'; 
quit;

 

3.3:IN Operator to Select Values from a List  

column IN (constant-1 <, . . . constant-n>) 

constant-1 and constant-n represent a list that contains one or more specific values.(括号中的常量个数大于等于1)

 

 

3.4: IS MISSING or IS NULL Operator to Select Missing Values  

To select rows that contain missing values, both character and numeric, use the IS MISSING or IS NULL operator. These operators are interchangeable.  

字符型和数值型缺失都可检验,这两个符号是等价的)

where column = ' '; where column = .;分别只能检验字符型和数值型缺失。

 

3.5: LIKE Operator to Select a Pattern 

column LIKE 'pattern'  

underscore ( _ )  any single character 

percent sign (%)  any sequence of zero or more characters

proc sql;    select ffid, name, address     from sasuser.frequentflyers        where address like '% P%PLACE'; *空格也包含在字符串中;
quit;

 

3.6:Using the Sounds-Like (=*) Operator to Select a Spelling Variation 

The sounds-like (=*) operator uses the SOUNDEX algorithm to compare each value of a column (or other sql-expression) with the word or words (or other sql-expression) that you specify.

 

3.7:Subsetting Rows by Using Calculated Values (sas特有的,不是标准sql中的)

sas编译时,先执行where,如果不用calculated那么就会报错说没有total这个变量,加上后会在新生成的变量中查找。

proc sql outobs=10;    select flightnumber, date, destination,      boarded + transferred + nonrevenue as Total,

      calculated Total/2 as half
from sasuser.marchflights where calculated total < 100; /*想要使用新生成的列的时候,需要加上calculated关键字,having要加 order by 不用加*/

 

3.8:Using the ANY Operator 

where dateofbirth < any   (subquery...)              

equal to max()    比如,子查询返回20 、30、 40,那么,外查询选择所有<40的记录

>any equal to min()   比如,子查询返回20 、30、 40,那么,外查询选择所有>20的记录

=any equal to in

 

3.9:Using the ALL Operator 

all和any相反

 

3.10:exsits 、not exsits

对于exsits,为真的话就输出,假的就不输出。

对于not exsits相反。

/*需求,选择是员工又是经常单独飞行的人姓名*/

proc
sql; title 'Frequent Flyers Who Are Employees';

select name

from sasuser.frequentflyers

where exists

(select * from sasuser.staffmaster

where name=trim(lastname)||', '||firstname)

order by name;

quit;

 

4、NOEXEC 、VALIDATE;

相同点:这两个关键字都有使程序不执行,只进行语法检查的效果!

不同点:validate只对紧跟其后的select语句有效,noexec对真个sql过程有效

proc sql noexec;       
select empid, jobcode, salary   from sasuser.payrollmaster     where jobcode contains 'NA'       order by salary;
quit;



proc sql;

validate

select empid, jobcode, salary

  from sasuser.payrollmaster

  where jobcode contains 'NA'

  order by salary;

quit;

 

 4.1 feedback,在列比较多,我用来查看列名。。。。然后复制粘贴我想要的名字。。。

*options fullstimer=on;*run;
proc sql feedback;
select * from sashelp.class;
NOTE: 语句变换为:select CLASS.Name, CLASS.Sex, CLASS.Age, CLASS.Height, CLASS.Weightfrom SASHELP.CLASS;quit;

 4.2Count

只计算非缺失值

转载于:https://www.cnblogs.com/yican/p/4079283.html

更多相关:

  • select {appearance: none;/*隐藏原生select下拉框的向下箭头▼*/ }...

  • 一、基础1、说明:创建数据库CREATE DATABASE database-name2、说明:删除数据库drop database dbname3、说明:备份sql server--- 创建 备份数据的 deviceUSE masterEXEC sp_addumpdevice 'disk', 'testBack', 'c:mssq...

  • 一、介绍   GreenPlum分布式数据仓库,大规模并行计算技术。  无共享/MPP核心架构  Greenplum数据库软件将数据平均分布到系统的所有节点服务器上,所以节点存储每张表或表分区的部分行,所有数据加载和查询都是自动在各个节点服务器上并行运行,并且该架构支持扩展到上万个节点。 混合的存储和执行(按列或按行)  Greenp...

  • from selenium.webdriver.support.ui import Select Select(d.find_element_by_id(u'key_开户行')).first_selected_option.text 转载于:https://www.cnblogs.com/paisen/p/3669272.html...

  • 我们都知道面试的时候通常都会考数据库部分的知识,所以在此整理了下我们常用的面试中常用的一些查询语句SQL,本人测试数据库为SQL Server2008 首先在数据库中建立一个测试库,此处用DavidTest代替 数据表与相应测试数据 USE [DavidTest] GO /****** Object: Table [dbo].[T...

  • 数据分析过程中,我们经常可以看到提数的SQL语句,了解SQL常用的基础查询语句,是检验提数逻辑是否正确的途径之一,并且也能更方便使用SMART BI数据分析工具。今天就让小编带大家走进SQL基础查询的世界吧~1、查询单个字段:语法:SELECT 字段名 FROM 表名举例:SELECT first_name FROM employ...

  •   SELECT * FROM tableSELECT * FROM table WHERE name = '强哥'SELECT * FROM table ORDER BY updateTime DESC...

  • 使用 OpenRowSet 和 OpenDataSource 访问 Excel 97-2007 测试文件:D:97-2003.xls和D:2007.xlsx,两个文件的内容是一模一样的。 测试环境:SQL Server 2000 / 2005。 -------------------------------------------...

  • exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'Ad Hoc Distributed Queries',1 reconfigure SELECT * INTO tmp_asset FROM OPENROWSET('Microsof...

  • select b.*,(select count(a.id) from td_product a where a.protypeid=b.id) num from td_protype b 转载于:https://www.cnblogs.com/shanlin/archive/2011/09/27/2192725.html...

  • We have ZZIPlib installed.My command configure line looks like :./configure ?with-apxs ?with-curl ?with-curl-dir=/usr/local/lib ?with-gd ?with-gd-dir=/usr/local ?with-g...

  • asar Whether to package the application’s source code into an archive, using Electron’s archive format. Defaults to true. Node modules, that must be unpacked, will be d...

  • 1.      今天遇到一问题,在sles11/vxworks下编译通过,但是在hpux下失败 2.      编译错误: /usr/ccs/bin/ld:DP relative code in file /projects/xxx/DERIVED/tfa_pa32-hpux.a(tfa02_pa32-hpux.o) -share...

  •         最近买个了小本lenovo x100e,结果发现这小本没有大小写指示灯,在windows用也无妨,不过我常常用这本在ubuntu中调试linux代码,vi 常用的编辑器,熟悉的都知道,大小写很关键的,用google搜了一下,发现可以用如下方法解决:        1.  “sudo apt-get install l...

  •   修改Ubuntu的启动logo 原文链接: https://my.oschina.net/jmjoy/blog/380262     内容:   Plymouth splash screen is the initial splash screen at boot-up.Ubuntu 10.04 uses Plym...