用SAS宏实现Oracle中的decode函数
DECODE函数是ORACLE PL/SQL的功能强大的函数之一,目前还只有ORACLE公司的SQL提供了此函数,其它数据库厂商的SQL实现还没有此功能
函数介绍
DECODE函数是Oracle PL/SQL的功能强大的函数之一,,目前还只有ORACLE公司的SQL提供了此函数,其它数据库厂商的SQL实现还没有此功能。DECODE有什么用途呢? 先构造一个例子,假设我们想给智星职员加工资,其标准是:工资在8000元以下的加20%;工资在8000元或以上的加15%,通常的做法是,先选出记录中的工资字段值? select salary into var-salary from employee,然后对变量var-salary用if-then-else或choose case之类的流控制语句进行判断。 如果用DECODE函数,那么我们就可以把这些流控制语句省略,通过SQL语句就可以直接完成。
看用SAS怎么实现吧,就是这么一个小小的宏,就实现了所谓其他数据库都不能实现的功能,
%macro decode /PARMBUFF;
%local i count ifn valuen countall currfeild valueelse;
%let countall=%sysfunc(countw(&SYSPBUFF,%quote(,)));
%let count=%eval((&countall-1)/2);
%let currfeild=%scan(%quote(&SYSPBUFF),1);
case &currfeild
%do i=1 %to &count;
%let ifn=%scan(%quote(&SYSPBUFF),%eval(&i*2));
%let valuen=%scan(%quote(&SYSPBUFF),%eval(&i*2+1));
when &ifn then &valuen
%end;
%if %eval(&countall>(&count+1)) %then %do;
%let valueelse=%scan(%quote(&SYSPBUFF),&countall);
else &valueelse
%end;
end
%mend;
调用代码如下,和oracle的调用方式和功能实现基本上都是一样的
proc sql;
create table test as
select
%decode(sex,"男",0,"女",1,2)
as abc
from sashelp.class;
quit;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)
