Saturday, September 23, 2017

Comma position in macro call

Comma is used to separate parameters in the macro call. We seldom care about the comma position.

For example:
%a(lib=a
, name=b)

%a(lib=a,
name=b)

Are the two macro calls identical? It may, or may not. If the code is deployed in LATIN1 but runs in UTF-8 session, it will have different result. In first call, the actual value of parameter LIB is "a(invisible carriage return)".

To make a general availability, please always put the comma just behind value in macro call.

Wednesday, September 6, 2017

Sample codes for SAS View

SAS has two kinds of views: SQL view and DATA view. They work similarly but we need to know what's the difference. Here, I give out some samples for clarification.
/* create DATA view */
data a / view=a;
    set sashelp.class;
run;

/* Create SQL view */
proc sql;
    create view b as
        select * from sasehlp.class;
quit;

/* Display Data view definition. 
Note: it can't be used for SQL view. */
data view=a;
    describe;
run;

/* Display SQL view definition. 
Note: it can't be used for DATA view. */
proc sql;
    describe view b;
quit;

/* There are 4 ways to delete views. 
 The last one is my favorite as it has most flexibility. */
proc delete data=work.a work.b (memtype=view); 
run;

proc sql;
    drop view work.a, work.b;
quit;

proc datasets lib=work nolist nowarn;
    delete a b / memtype = view;
run; quit;

proc datasets lib=work nolist nowarn memtype=(data view);
    delete a b;
run; quit;

Sunday, September 3, 2017

Check if dataset is locked.

Here is the tip to check if the dataset is locked by other PID. It is useful as it has write operation but actually does not change anything.
proc append base=dsn data=dsn(obs=0); run;

Friday, September 1, 2017

FINDW with null argument

Few SAS functions can take null argument. Definitely FINDW is special case. It means we can't use default delimiters when it is using modifier. Below is the sample codes and snippet from SAS documentation.
The FINDW function allows character arguments to be null. Null arguments are treated as character strings with a length of zero. Numeric arguments cannot be null.

data _null_;
    str = "aa bb";

    n1 = findw(str, "bb");  * Default deimilters are used;
    n2 = findw(str, "bb", ); * delimiter is NULL character;
    n3 = findw(str, "bb", ' '); * delimiter is blank character;
    n4 = findw(str, "bb", , 'E'); * delimiter is NULL character;
    n5 = findw(str, "bb", , 'SE'); * delimiter is space character, specified by 'S';
    put n1= n2= n3= n4= n5=;
run;

/* Output: n1=4 n2=0 n3=4 n4=0 n5=2*/