SAS functions and procedures by 49OJi7


* SAS functions and procedures.                                  *

The SAS data ‘step’

data <data set(s)>;
set <data set(s)>;
....SAS statements....
if <condition> then do;
else if <condition> then <statement>;
else <statement>;

The SAS data ‘step’. This is the primary method of manipulating data using
SAS. The line

      data <data set(s)>

gives the output data set name(s).

      set <data set(s)>

gives the input data set(s). Where more than one data set is given in the
statement, the data sets are concatenated.

      a) if <condition> then <statement>;
      b) if <condition> then do;

can be applied for various purposes. When multiple statements are executed if
<condition> is true, the if statement takes the form presented in b).
Otherwise, it takes the form presented in a). Frequently, the IF-THEN
statement is used to apply an assignment statement when it applies only to
some records but not to all. It can also be used to group records. An ‘if’
statement without a ‘then’ part (e.g. if <condition>;) subsets the data set to
only the wanted records.

      where <field name> = <value>

subsets the data to relevant rows in the data set. It is similar to a
subsetting IF statement but more efficient because it avoids reading unwanted
rows. The where is optional and required only if it is necessary to work with
a subset of the data set.

‘SAS statements’ are then processed one record at a time. When the run;
statement is processed the record is output. The next record in the input data
set is then processed. This is repeated until all records in the data set are
processed and output;

merge <data set(s)>;
by <field name(s)>

Merge data sets using the field(s) <field name(s)>.

index=(<field name(s)>)

Apply the <field name(s)> index to the data set. A data set indexed by a field
does not need to be sorted by the <field name(s)> before merging data sets.

keep=<field name(s)>

Keep the <field name(s)> in the input or output data set.

drop=<field name(s)>

Drop the <field name(s)> from the input or output data set.

out=<data set>

Output data set name.

proc sort data=<data set>;
by <field name(s)>;

Sort the <data set> by the <field name(s)>.


Take the integer part of x.

compress(<field name>)

Remove spaces from <field name>.

proc datasets library=<library name>;
modify <dataset name>;
index create <index name>=(<field names>);

Apply the <field names> composite index to the data set. A data set indexed by
composite fields does not need to be sorted by the <field names> before
merging data sets. The <library name> is a specified directory location,
library=templib is the default location for data sets.

retain <field name(s)>

Retain the value, from the previous record, of <field name(s)>.

first.<field name>
Identifies the first instance of a value over <field name> within the nesting
given in the by statement.

last.<field name>

Identifies the last instance of a value over <field name> within the nesting
given in the by statement.

proc append
  base=<data set 1>
  data=<data set 2> force;

Append <data set 2> to <data set 1> and output to <data set 1>. The force
option is required where a field, or fields, do not appear on both data sets.
The output data set contains all fields contained on each data set. These
entries are set to missing for records on data sets not containing this field
on input.

proc summary data=<data set 1> nway missing;
class <field 1a> <field 2a> .... <field ma>;
var <field 1b> <field 2b .... <field nb>;
output out=<data set 2> mean=<field 1c> <field 2c> .... <field nc>;

Calculate the arithmetic means of the fields <field 1b>, <field 2b,....,
<field nb> over the nesting of fields <field 1a>,<field 2a>,....,<field ma>
for <data set 1>. Output these mean values in <data set 2> and calling the
output mean fields <field 1c>,<field 2c>, ....,<field nc> respectively.

select (<select-expression>);
       when (<value(s)>) <statement>;
       when (<other value(s)>) <statement>;
       otherwise <statement>;

The SELECT statement is an alternative to nested IF statements. SAS first
evaluates <select-expression>. It then compares the result with each of the
values in the WHEN clauses. The first time the two are found to be equal, the
associated statements are executed, and processing of the SELECT group stops.
If <select-expression> isn’t equal to any of the values in any of the WHEN
clauses, the statement associated with OTHERWISE is executed. (If there is no
match, and no
OTHERWISE clause, it will cause an error.)

%macro <macro-name>;
....SAS statements....

A macro allows the re-use of the same piece of code enclosed within %macro
<macro-name> and %mend. %macro <macro-name> marks the beginning of the macro
while %mend marks the end of the macro. A macro is invoked by adding the
percent prefix to its name as follows:

%include ‘<>’;

includes any macros or sas commands stored in the external file <a-piece-of-> in the current piece of code.

%let <macro-variable> = <value>;

Assigns a value to macro-variable.

%substr(<source>, <position>, <N>);

This function returns <N> characters, beginning at character number <position>
from the string <source>.

To top