Variable Assignment Statements

Assignment Statement

Evaluates an expression and stores the result in a variable.
Valid:in a DATA step



names a new or existing variable.

Range:Variable can be a variable name, array reference, or SUBSTR function.
Tip: Variables that are created by the Assignment statement are not automatically retained.

is any SAS expression.

Tip:expression can contain the variable that is used on the left side of the equal sign. When a variable appears on both sides of a statement, the original value on the right side is used to evaluate the expression, and the result is stored in the variable on the left side of the equal sign. For more information, see Expressions in SAS Language Reference: Concepts.

Assignment statements evaluate the expression on the right side of the equal sign and store the result in the variable that is specified on the left side of the equal sign.

These assignment statements use different types of expressions:

  • name='Amanda Jones';
  • WholeName='Ms. '||name;
  • a=a+b;

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

1.6. Variables and Assignment¶

Each set-off line in this section should be tried in the Shell.


Nothing is displayed by the interpreter after this entry, so it is not clear anything happened. Something has happened. This is an assignment statement, with a variable, , on the left. A variable is a name for a value. An assignment statement associates a variable name on the left of the equal sign with the value of an expression calculated from the right of the equal sign. Enter

Once a variable is assigned a value, the variable can be used in place of that value. The response to the expression is the same as if its value had been entered.

The interpreter does not print a value after an assignment statement because the value of the expression on the right is not lost. It can be recovered if you like, by entering the variable name and we did above.

Try each of the following lines:

The equal sign is an unfortunate choice of symbol for assignment, since Python’s usage is not the mathematical usage of the equal sign. If the symbol ↤ had appeared on keyboards in the early 1990’s, it would probably have been used for assignment instead of =, emphasizing the asymmetry of assignment. In mathematics an equation is an assertion that both sides of the equal sign are already, in fact, equal. A Python assignment statement forces the variable on the left hand side to become associated with the value of the expression on the right side. The difference from the mathematical usage can be illustrated. Try:

so this is not equivalent in Python to . The left hand side must be a variable, to which the assignment is made. Reversed, we get a syntax error. Try

This is, of course, nonsensical as mathematics, but it makes perfectly good sense as an assignment, with the right-hand side calculated first. Can you figure out the value that is now associated with width? Check by entering

In the assignment statement, the expression on the right is evaluated first. At that point was associated with its original value 10, so had the value of 10 + 5 which is 15. That value was then assigned to the variable on the left ( again) to give it a new value. We will modify the value of variables in a similar way routinely.

Assignment and variables work equally well with strings. Try:

Try entering:

Note the different form of the error message. The earlier errors in these tutorials were syntax errors: errors in translation of the instruction. In this last case the syntax was legal, so the interpreter went on to execute the instruction. Only then did it find the error described. There are no quotes around , so the interpreter assumed was an identifier, but the name fred was not defined at the time the line was executed.

It is both easy to forget quotes where you need them for a literal string and to mistakenly put them around a variable name that should not have them!

Try in the Shell:

Now , without the quotes, makes sense.

There are more subtleties to assignment and the idea of a variable being a “name for” a value, but we will worry about them later, in Issues with Mutable Objects. They do not come up if our variables are just numbers and strings.

Autocompletion: A handy short cut. Idle remembers all the variables you have defined at any moment. This is handy when editing. Without pressing Enter, type into the Shell just

Then hold down the key and press the key. This key combination is abbreviated . (On a Mac, that may give you a funny character: In that case you need to hold down both the control key and the alt/option key when pressing the ‘/’. This may hold in other places the Alt key is called for in Windows.)

You should see autocompleted to be

This is particularly useful if you have long identifiers! You can press several times if more than one identifier starts with the initial sequence of characters you typed. If you press again you should see . Backspace and edit so you have , and then and press again. You should not see fred this time, since it does not start with .

1.6.1. Literals and Identifiers¶

Expressions like or are called literals, coming from the fact that they literally mean exactly what they say. They are distinguished from variables, whose value is not directly determined by their name.

The sequence of characters used to form a variable name (and names for other Python entities later) is called an identifier. It identifies a Python variable or other entity.

There are some restrictions on the character sequence that make up an identifier:

  • The characters must all be letters, digits, or underscores , and must start with a letter. In particular, punctuation and blanks are not allowed.

  • There are some words that are reserved for special use in Python. You may not use these words as your own identifiers. They are easy to recognize in Idle, because they are automatically colored orange. For the curious, you may read the full list:


There are also identifiers that are automatically defined in Python, and that you could redefine, but you probably should not unless you really know what you are doing! When you start the editor, we will see how Idle uses color to help you know what identifies are predefined.

Python is case sensitive: The identifiers , , and are all different. Be sure to be consistent. Using the auto-completion shortcut in Idle helps ensure you are consistent.

What is legal is distinct from what is conventional or good practice or recommended. Meaningful names for variables are important for the humans who are looking at programs, understanding them, and revising them. That sometimes means you would like to use a name that is more than one word long, like , but blanks are illegal! One poor option is just leaving out the blanks, like . Then it may be hard to figure out where words split. Two practical options are

  • underscore separated: putting underscores (which are legal) in place of the blanks, like .
  • using camel-case: omitting spaces and using all lowercase, except capitalizing all words after the first, like

Use the choice that fits your taste (or the taste or convention of the people you are working with).

first='Sue'last='Wong'name=first+' '+lastname
Categories: 1

0 Replies to “Variable Assignment Statements”

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *