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

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.

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中的)

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...)

<any 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;

 
时间: 2024-09-29 17:56:05

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

sas中的sql(6)创建表格、展现表格、插入行、删除行、规定限制条件(constriants)、处理输入错误(undo策略)、update表格、更改列

1:三种建表方式 建表只会在库中建立好表格并在日志中显示,并不会有输出. 1.1:自己定义列来建立一张空表 column-specification = column-define + column-constriants + MESSAGE=/MSGTYPE SAS中数据的存储方式只有两种,一种char(n)一种num.sas也支持sql原生的数据类型,但是最终都会统一转化为这两种 proc sql; create table work.departments (Dept varchar(20

sas中的sql(4) 多表操作,内连接,外连接(left | right | full/join),In-Line Views

Understanding Joins 1.Joins combine tables horizontally (side by side) by combining rows. The tables being joined are not required to have the same number of rows or columns. (被join的表不需要行或列与join表的相同) 2.When any type of join is processed, PROC SQL sta

sas中的sql(1) 基本语法

Sas proc sql与寻常sas语句的的不同之处 1:The PROC SQL step does not require a RUN statement. PROC SQL executes each query automatically 2:Unlike many other SAS procedures, PROC SQL continues to run after you submit a step. To end the procedure, you must submit a

sas中的sql(8)sql选项解析,数据字典

INOBS/OUTOBS= 这个选项意思在前面的随笔中已说过,就INOBS这里有个例子 这里的INOBS=5是针对于两张表分别读入5个,而不是一共读入五个 NUMBER/NONUMBER 效果如下 Double/NoDouble Double Spacing your output to make it easier to read. FLOW | NOFLOW | FLOW=n | FLOW=n m The FLOW option causes text to be flowed in its

sas中的sql(5) 纵向操作数据集 Except、Intersect、Union、OuterJoin

SQL进行纵向操作的基本语法 proc sql; select * from table1 set-operator <all> <corr> select * from table2 set-operator <all> <corr> select * from table3; 1:几种set操作符 Except.Intersect.Union.OuterJoin Except.Intersect.Union三种set符号是默认进行unique处理,当进行

sas中的sql(7)创建视图,更新视图,删除视图

什么是视图? 视图是一系列的查询语句,在使用时被执行,用来从其他的数据集或视图中获取想要的子集(subset)或者超集(superset). The view contains only the logic for accessing the data, not the data itself 视图能用在哪些地方? 几乎在sas程序中任何真实表用的地方(不能用的地方暂未列出). 使用视图的好处? 1:节约空间,视图往往比真实表要小很多. 2:防止用户经常进行表查询而忽略默写列,视图写好后每次调用

sas中的sql(3) 标签,格式化输出,子查询,大于两张表的联合查询(暂缺)

1.1:Specifying Column Formats and Labels (SAS enhancements. ) proc sql outobs=15; title 'Current Bonus Information'; title2 'Employees with Salaries > $75,000'; /*title可以放在sql之前或sql与select之间*/ select empid label='Employee ID', /*label=放在变量之后*/ jobcod

sql多行多列重复

在sql的查询中我们会遇到查询的结果比如这样的: 查询这张表的sql语句: select r.ROLE_NAME,u.USERID,u.USERNAME,u.TrueName from BASE_USERINFOR u left join BASE_USERROLE ur on u.USERID=ur.USER_ID left join BASE_ROLEINFOR r on r.ROLE_ID=ur.ROLE_ID 就拿钱三行来说就一个ROLE_NAME不一样其他的列的数值都是一样的难道我们就

sas中sql基本语法

sas中的sql过程可以整理数据,数据合并,以及数据的选取功能等. sql过程可以拼接两个数据集,创建表格,删除表格中的行和列,以及进行简单的计算各个变量值. 例如: proc sql;  create view work.body as                                        //从ad表格中选取变量id,de,age,sex,并增加一个变量height并创建一个新表格bodyselect id, de ,age,sex,id**2\age as heig