Tuesday, June 12, 2018

Identify trailing string using $revers.

In SAS, it is easy to identify leading string using ":" operator modifier, e.g. "a =: b". However, there was no easy way to identify trailing ones. Traditionally we could use combination of substr/reverse/length/strip functions. Personally I prefer format $revers as it combines the functionality of substr and left-align. Note that the width should be the length of the character variable.
data _null_;
    a = 'abc efg   ';
    if put(a, $revers10.) =: 'gfe' then put "a has trailing string efg";
run;

/*
Output:
a has trailing string efg
*/

Result: 

Friday, March 23, 2018

LUA - it is safe to drive SAS

PROC LUA can create safe environment to drive SAS because you don't worry about changing any existing library, fileref, macros and global options outside LUA session. Furthermore, you can also setup new library, fileref, macros and global options in LUA session. It will maintain all of them inside LUA session until it terminates or restarts.

Below are the sample code to demonstrate the concepts:

filename test temp;
libname test "c:\";
%macro test;
    %put outside;
%mend;

proc lua restart;
submit;
sas.submit [[
/* It will fail to delete existing fileref or library */
filename test clear;
libname test clear;
]]
endsubmit;
run;

proc lua restart;
submit;
sas.submit [[
filename test temp;
libname test "c:\temp";
options obs=0;
%macro test;
    %put inside;
%mend;

filename test list;
libname test list;
proc options option=obs; run;
%test
]]
endsubmit;
run;

filename test list;
libname test list;
proc options option=obs; run;
%test

Tuesday, March 13, 2018

How to call LUA file?

As we know, LUA is great fit to SAS. Here are some tips on how to integrate LUA file into SAS.

For example, the LUA file is at c:\lua\test.lua
/* #1. using INFILE option of PROC LUA */
FILENAME LUAPATH "c:\lua";
PROC LUA INFILE="test"; RUN;

/* #2. leverage current folder */
X "CD c:\lua";
PROC LUA;
SUBMIT;
dofile("test.lua")
ENDSUBMIT;
RUN;

/* #3. using absolute path */
PROC LUA;
SUBMIT;
dofile(c:\\lua\\test.lua) -- escape backslash character
dofile [[c:\lua\test.lua]] -- or using [[ and ]]
ENDSUBMIT;
RUN;

/* #4. %INCLUDE statement supports both SAS and LUA starting from 9.4 */
%INCLUDE "c:\lua\test.lua";

/* $5. passed by SUBMIT option */
%let luaname=c:\lua\test.lua;

PROC LUA;
SUBMIT "name=[[&luaname]]";
    print(name)
    dofile(name)
ENDSUBMIT;
RUN;

Wednesday, March 7, 2018

New way to keep data structure in DATA step

In most time, you don't want to add any new variable in DATA step. Normally we create one list of all original variables before DATA step. Actually we can have better way to address this. Please see codes at below:
DATA TEST (DROP=START -- END);
    IF 0 THEN SET SASHELP.CLASS;
    IF 0 THEN START = 0;

    SET SASHELP.CLASS;

    A=1;B=1; * Test Variables;

    IF 0 THEN END = 0;
RUN; 

Monday, February 26, 2018

How to set Environment Variable

options set=varname "varvalue"; /* set environment variable*/
%put %sysfunc(sysget(varname));

options set=varname="varvalue"; /* set environment variable*/
%put %sysfunc(sysget(varname));

Tuesday, January 2, 2018

How to use whole ARRAY?

Generally we use ARRAY element (e.g. VARS[1]) just like normal variable in DATA step programming. However, I prefer to using whole array as it can combine the flexibility of array.
Below is listing (update in progress).

#1. ARRAY name only
DIM function
IN operator
FCMP function with VARARGS option (see SAS Sample 41754)

#2. ARRAY name with OF operator (e.g. OF VARS[*])
SUM / MIN / MAX / MEDIAN / MEAN ...
CALL MISSING
CALL SORTN / CALL SORTC
CAT function family (e.g. CAT/CATX/CATS).
...