Wednesday, March 29, 2017

CALL SET routine

I have dig the CALL SET routine and found two interesting points:
#1. In macro, it will resolve macro variable automatically. It means no leading ampersand is needed.
#2. In macro, it will link the dataset variable to closest macro variable that has same name. If the macro variable does not exist, it will create local one.

1    %macro setvar;
2        %local sex;
3       %let dsid=%sysfunc(open(sashelp.class, i));
4       %syscall set(dsid); /* No leading ampersand with %SYSCALL */
5
6       %put Note: Before fetchobs;
7       %put _local_;
8
9       %let rc=%sysfunc(fetchobs(&dsid, 10));
10      %let rc=%sysfunc(close(&dsid));
11
12      %put Note: After fetchobs, which will create local macro variable which does not exist.;
13      %put _local_;
14   %mend setvar;
15
16   %global name;
17   %setvar

Note: Before fetchobs
SETVAR DSID 1
SETVAR SEX

Note: After fetchobs, which will create local macro variable which does not exist.
SETVAR AGE 12
SETVAR DSID 1
SETVAR HEIGHT 59
SETVAR SEX M
SETVAR WEIGHT 99.5

18
19   %put _global_;
GLOBAL NAME John
GLOBAL RC 0

Monday, March 27, 2017

READONLY macro variable

READONLY macro variable is new feature of SAS 9.4. It can be defined as global or local like normal macro variable. However, it has different behaviors as follows:
1. As to global READONLY macro variable, it can not be changed or deleted once created.
2. Global READONLY macro variable is unique and can not be overlapped by local READONLY one.
3. The READONLY macro variable can not overwrite existing normal one

* Sample 1;
%global/readonly mv_ro=1;
%symdel mv_ro; /* ERROR: The variable MV_RO was declared READONLY and cannot be deleted */

* Sample 2;
%macro test;
    %local/readonly mv_ro=2; /* ERROR: The variable MV_RO was previously declared as READONLY and cannot be re-declared. */
%mend;
%test

* Sample 3;
%global mv;
%let mv=1;
%global/readonly mv=2; /* ERROR: The variable MV was previously declared and cannot be made READONLY. */

Thursday, March 16, 2017

Temporary file/folder

To use temporary file/folder, we can avoid leaving any legacy files in system. Here are my codes:
* SAS code to create temporary file;
filename a temp;
filename a list;

* SAS code to create temporary folder:
data _null_;
    guid=uuidgen();
    tmpdir=dcreate(guid,getoption('work'));
    call symputx('tmpdir',tmpdir);
run;

%put &=tmpdir;