Tuesday, September 22, 2009

CALL SORTx routines

With CALL SORTx routines, we can sort and EXCHANGE the values of the variables. It is a very helpful since we need not assign a temporary variable for exchange of the values.
Below is an interesting sample code:

* Get the first two max height values;
data _null_;
set sashelp.class end=last;
retain max1 max2;

call sortn(height, max2, max1);
if last then put "max height value: " max1 max2;
run;

Monday, September 14, 2009

ANYDT* informats: Key to date/time values

Before SAS 9, we have to handle date/time values conditionaly if they are not in uniform style. Now, it becomes easy to input many different variations of date/time values using ANYDT* informats.
It is mainly used in PROC IMPORT, the Import Wizard, raw file input which have different styles of date/time values.

As to ANYDT* informats, notice the two points at below:
#1: There is no INVALID DATA message or set _ERROR_ to 1 if the input text cannot be interpreted. (SAS Note)
#2: A new system option DATESTYLE is introduced to determine the sequence of month, day, and year

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;

Monday, September 7, 2009

PUT function vs. INPUT function

What about the relationship between them?

In my opinion, INPUT function is the inverse process of PUT function.
PUT function: convert the variable value to a character string
INPUT function: convert a character string to the variable value

Thursday, September 3, 2009

Macro variable in PROC SQL

There are two ways to store values of column in PROC SQL.
1. mv with separator, which store values of the rows into one mv
2. mv range, which store value of one row per mv

* Sample code;
proc sql;
select distinct sex, name
into:sex seperated by ' ', :name1-:name100
from sashelp.class;

%let num = &sqlobs;
quit;

%put Number: &sqlobs;
%put _global_;