Showing posts with label data set. Show all posts
Showing posts with label data set. Show all posts

Thursday, September 10, 2009

Check variable exist in DATA step run time

First off, there is NO way to know exactly which variable exist in DATA step run time.

We can check the variable existence before DATA step run time. The popular method is Macro technology with File I/O functions or Dictionary table.

How to avoid creating this variable if it does not exist and to get the formatted value of the variable if it exist.
Here ia another way:

* Sample;
data dummy;
set sashelp.class (obs=0);
run;

data test;
set sashelp.class;

if _n_ = 1 then do;
drop varexist dsid rc;
retain varexist;
dsid = open('dummy');
varexist = varnum(dsid, "name");
rc = close(dsid);
end;

length tmp $ 20;
if varexist then do;
tmp = vvaluex("name");
end;
else tmp = '';
run;

Sunday, May 17, 2009

How to avoid "retain" observations in Match-Merging

There are two merging techniques: One-to-One Merging and Match-Merging.
Match-Merging is the most popular operation in SAS DATA step programming.
The reason is that Match-Merging always “retain” last observation until new one is read into PDV.
(Note that One-to-One merging is not.)

However, sometimes we need not retain last observation.
Since SAS do not provide option to control it, we have to initialize PDV manually every time.

Here is the code:

OUTPUT; * save PDV to dataset;
ARRAY CHAR _CHARACTER_ ;
ARRAY NUM _NUMERIC_ ;
DO OVER CHAR ; CHAR = '' ; END ;
DO OVER NUM ; NUM = . ; END ;


or

*For SAS 9.1 and later;
OUTPUT;
CALL MISSING(OF _ALL_);

Data Set List - A close friend to Set/Merge statement

Are you boring with writing a long string to concatenate/merge many datasets? With the introduction of Data Set Lists in SAS 9.2, it is “gone with the wind”.

Data set lists can give out the list of existing datasets efficiently and flexibly.
Just like variable list, it also has two formulas:
Name prefix lists: SALE1:
Numbered range lists: sales1-sales4

Thursday, April 16, 2009

What is _IORC_?

It is short for Input-Output Return code. The definition is at below from SAS online doc:
The automatic variable _IORC_ contains the return code for each I/O operation that the MODIFY statement attempts to perform.

However, it is not quite right. We should get some points for _IORC_:
#1: it is automatic variable, which means it exist in PDV but do not output.
#2: it used with indexed dataset to check whether the direct read found a matching obervation. That means it do make sense in dataset with key= option, no matter what statement is used, e.g. SET statement, MODIFY statement, etc.
#3: it is a good habit to always check _IORC_ after reading indexed dataset. Here is a good example: http://support.sas.com/kb/26/002.html.
#4: since SET statement retain all variable values, we should STOP or re-assign all variables to missing and _ERROR_ to 0 when _IORC_ is not _SOK (no matching observation).