Use SAS macro to implement the decode function in Oracle

Source: Internet
Author: User

Function Introduction

The DECODE function is one of the powerful functions of Oracle PL/SQL. Currently, only oracle SQL provides this function, and other database vendors do not yet implement this function. What is the purpose of DECODE? First, let's construct an example. If we want to add a salary to a staff member of zhixing, the standard is: the salary is less than 8000 RMB plus 20%; the salary is more than 8000 RMB plus 15%, usually, select the salary field value in the record first? Select salary into var-salary from employee, and then use if-then-else or choose case to judge the variable var-salary. If the DECODE function is used, we can omit these flow control statements and directly complete them through SQL statements.

Let's take a look at how SAS can be implemented. It's such a small macro that can't be implemented by other databases,

% Macro decode/PARMBUFF;
% Local I count FN 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 fn = % scan (% quote (& SYSPBUFF), % eval (& I * 2 ));
% Let valuen = % scan (% quote (& SYSPBUFF), % eval (& I * 2 + 1 ));

When & interferon then & valuen
% End;

% If % eval (& countall> (& count + 1) % then % do;
% Let valueelse = % scan (% quote (& SYSPBUFF), & countall );
Else & valueelse
% End;
End
% Mend;

The calling code is as follows, which is basically the same as the calling method and function implementation of oracle.

Proc SQL;
Create table test
Select
% Decode (sex, "male", 0, "female", 1, 2)
As abc
From sashelp. class;
Quit;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.