Published on :
ETL SASHELP

Executing an iterative DO loop

This code is also available in: Deutsch Español Français
Awaiting validation
The program reads the first two records from the `sashelp.shoes` table. For each record read, it executes a `DO` loop three times (for `Year` from 1 to 3). Inside the loop, it increases the `ProjectedSales` value by 5% and writes a new record to the `forecast` table at each iteration using the `OUTPUT` statement. Finally, it keeps only certain columns and applies a monetary format to `ProjectedSales`. The `PUTLOG` statements help track execution in the SAS© log.
Data Analysis

Type : SASHELP


The data comes from the `sashelp.shoes` table. The script only reads the first two observations (obs=2 option).

1 Code Block
DATA STEP Data
Explanation :
This code block is a DATA step that creates the `forecast` table. It begins by reading the first two observations from `sashelp.shoes`, renaming the `Sales` column. A `DO` loop is used to iterate three times for each input observation. In each iteration, the `ProjectedSales` variable is recalculated (increased by 5%) and a new record is explicitly written to the output table with the `OUTPUT` statement. After the loop, the `KEEP` statement selects the columns to retain and `FORMAT` applies a monetary format to `ProjectedSales`.
Copied!
1DATA forecast;
2 putlog 'Top of DATA Step ' Year= _N_=;
3 SET sashelp.shoes(obs=2 rename=(Sales=ProjectedSales));
4 DO Year = 1 to 3;
5 ProjectedSales=ProjectedSales*1.05;
6 OUTPUT;
7 putlog 'Value of Year written to table: ' Year=;
8 END;
9 putlog 'Outside of DO Loop: ' Year=;
10 keep Region Product Subsidiary Year ProjectedSales;
11 FORMAT ProjectedSales dollar10.;
12RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.