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
|
![]() ![]() ![]() ![]() ![]() |
|
![]() |
![]() |
Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.