如何根据if语句从sas编写sql查询

时间:2015-02-27 19:33:09

标签: sql sas oracle-sqldeveloper

我真的没有任何关于如何在sql中表达if / then语句(例如是否使用case)和/或在同一查询中格式化变量的高级sql知识,所以我想知道是否有人可以帮助这个sas代码并将其以正确的格式写入sql:

data convert_code;

format date1 date 2 mmddyy8. code $4.;

set userid.code; (this table is pulled from oracle)

if ID='X' then P='A'; else P='B';

If CAT in ('1','2','3') then CAT_group='ONE'; else CAT_GROUP='TWO';

if CAT_2 > '0' and CAT_2A >='1' then d=1; else d=0;

date1=datepart(date1);

date2=datepart(date2);

if code='3' and type_code in ('A','B','C') THEN DO;

if P_CODE in ('1','2','3') then P='1';

if P_CODE in ('4','5','6') then P='2';

end;

if code='4' and e_code in ('A') then DO;

if B_CODE in ('11','12','13') then P='3';

if B_CODE in ('14','15','16') then P='4';

end;

run;

1 个答案:

答案 0 :(得分:1)

此处给出的示例使用SAS'proc sql语言。如果您使用的是其他SQL实现,则语法可能会有所不同。但是,case表达式示例应该很容易适应任何SQL实现,因为它是SQL标准的一部分。

proc sql;
    /* Name your output table */
    create table convert_code as
    select
        /* 
            Unlike the data step variables from the input data are not
            selected by default, you can request them individually or
            with "select *" 
        */
        /* Use "format =" after the column definition to set its format */
        code format = $4.,
        /* Use SAS functions as normal, name the output variable with "as" */
        datepart(date1) as date1 format = mmddyy8.,
        /* Comma separate each variable you want in your output */
        datepart(date2) as date2 format = mmddyy8.,
        /* A "case" expression can conditionally set a variable to a value */
        case 
            when CAT in ('1', '2', '3') then 'ONE'
            else 'TWO'
        /* Close the "case" statement with "end" */
        end as CAT_group,
        /* You can nest case statements to emulate your "then do" */
        case 
            when code = '3' and e_code in ('A', 'B', 'C') then 
                case 
                    /* Use multiple "when then"s to emulate "else if" */
                    when P_CODE in ('1', '2', '3') then '1'
                    when P_CODE in ('4', '5', '6') then '2'
                    else ''
                end
            when code = '4' and e_code in ('A') then 
                case 
                    when P_CODE in ('11', '12', '13') then '3'
                    when P_CODE in ('14', '15', '16') then '4'
                    else ''
                end
            when ID = 'X' then 'A'
            else 'B'
        end as P,
        /* An alternative to case is to use the "ifn()" or "ifc()" functions */
        ifn(CAT_2 > 0 and CAT_2A >= 1, 1, 2) as d
    /* Choose the input data and end the query with a ";" */
    from userid.code;
    /* 
        Additional processing can be done here, some examples include:
            "where": limit the input
            "left join", "right join", "inner join", "outer join", ",":
                combine with additional data sets
            "group by": group based on column values for summary functions
            "order by": specify which columns to sort the output by
    */
/* End the "proc sql" processing */
quit;

使用case表达式而不是SAS特定的ifn()ifc()函数将是我的建议,因为它们是有条件地分配值的标准SQL方法,并且(可能)是在其他SQL实现中也是如此。请注意,format =datepart()功能是SAS特定的。

  • proc sql步骤角度对data进行简要探讨here
  • case表达式here
  • ifn()ifc()函数here
  • format =列修饰符here