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

how to write sql query from sas based on if then statements

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

data convert_code;

format date1 date 2 mmddyy8. code .;

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;

此处给出的示例使用 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 = .,
        /* 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 的。

  • data 步的角度对 proc sql 的简要探索可以找到 here
  • case表达式here
  • ifn()ifc() 函数 here
  • format =列修饰符here