SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Creating Variables
Lesson Summary

This page contains

I. Text Summary

To go to the page where a task, programming feature, or concept was presented, select a link.


Assignment Statements
To modify existing values or create new variables, add an assignment statement to your DATA step. You can include arithmetic operators and SAS functions in assignment statements.

Accumulating Totals
Use a sum statement to create a variable that accumulates values down observations. The sum statement adds the value of the right side of the plus sign (+) to the numeric variable on the left side, then retains the new value for subsequent observations.

Assigning Values Conditionally
To perform an action based on a condition, write an IF-THEN statement. It executes a SAS statement when the condition in the IF clause is true. You can include comparison and logical operators in IF-THEN statements.

Providing an Alternative Action
Instead of writing a series of IF-THEN statements, you can use the ELSE statement for an alternative action when the condition in an IF-THEN statement is false. Multiple ELSE statements let you specify a series of mutually exclusive conditions.

Specifying Lengths for Variables
Because the first value of a new variable determines its length, make sure to include a LENGTH statement before the first value is referenced in the DATA step. Specify the length in bytes.

Executing Multiple Statements Conditionally
To execute a group of SAS statements as a unit, create a DO group. DO and END statements define the group, which can include any number of executable statements.

Nesting DO Groups
Because DO and END are themselves executable SAS statements, any DO group can be nested within another DO group. Be careful to match each DO and END statement.


II. Syntax

To go to the page where a statement or option was presented, select a link.

LIBNAME libref 'SAS-data-library';
DATA SAS-data-set;
     SET SAS-data-set;
     LENGTH variable length;
     variable+expression;
     IF expression THEN statement;
          DO;
               IF expression THEN statement;
                    DO;
                         variable=expression;
                         statement(s);
                    END;
               ELSE statement;
          END;
     ELSE statement;
RUN;



III. Sample Program
     libname finance 'c:\records\loans';
     data finance.newloan;
        set finance.records(drop=amount rate);
        length Type $ 14;
        TotalLoan+payment;
        if code='1' then
           do;
              if months=360 then
                 do;
                    Type='Fixed Mortgage';
                    note='Check against current
                          prime lending Rate.';
                 end;
              else Type='Fixed Other';
           end;
        else Type='Variable';
     run;


IV. Points to Remember
  • The assignment statement is one of the few SAS statements that doesn't begin with a keyword.

  • When defining conditions for IF-THEN statements, specify character values in quotes and in the same case as they appear in the data.

  • Logical comparisons that are enclosed in parenthesis are evaluated as true or false before they are compared to other expressions.

  • You must specify the LENGTH statement before any other reference to the variable in the DATA step. The length of a new variable is determined by the first reference in the DATA step.

  • To specify the length of a character variable add a dollar sign ($) after the variable name: LENGTH variable $ length;

  • Indenting statements helps you keep track of DO groups.


back||next

Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement