SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Search||next

Creating Variables  
Assignment Statements


Sometimes, reading observations from an existing data set doesn't provide the information you need. To modify existing values or create new variables, you can use an assignment statement in any DATA step.


General form, assignment statement:
variable=expression;

where

  • variable names a new or existing variable
  • expression is any valid SAS expression.

NOTE: The assignment statement is one of the few SAS statements that doesn't begin with a keyword.


The assignment statement in the DATA step below creates a new variable, CholChange, by subtracting values of Chol1 (initial blood cholesterol reading) from values of Chol2 (final blood cholesterol reading).
     data lab23.drug1h;
        set research.cltrials;
        if placebo='YES';
        CholChange=chol2-chol1;
     run;
Caution: Notice the use of mixed case in the assignment statement. When the assignment statement creates a new variable, the case used in the statement is written to the data set.


Placebo Glucose Chol1 Chol2 CholChange
YES 413 220 205 -15
YES 322 186 183 -3
YES 304 231 225 -6


Sample Assignment Statements

Let's look at some other examples of assignment statements.

The expression can also contain the variable name on the left of the equal sign, as the second assignment statement below illustrates. This statement redefines the values of the variable Glucose as 10 percent higher.

     data lab23.drug1h;
        set research.cltrials;
        if placebo='YES';
        CholChange=chol2-chol1;
        glucose=glucose+glucose*.10;
     run;
Caution: The assignment statement doesn't use mixed case because the variable Glucose already exists. Assignment statements that redefine the values of variables have no effect on existing names.

When a variable name appears on both sides of the equal sign, the original value on the right is used to evaluate the expression. The result is assigned to the variable on the left of the equal sign.
     data lab23.drug1h;
        set research.cltrials;
        if placebo='YES';
        CholChange=chol2-chol1;
        glucose=glucose+glucose*.10;
     run;  ^       ^
           result  original value

You can use these arithmetic operators in expressions in assignment statements:


Character ValueOperation
** exponentiation
* multiplication
/ division
+ addition
- subtraction



  |next


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

Terms of Use & Legal Information | Privacy Statement