Data standardization and temperature conversions with PROC IML

This code is also available in: Deutsch Español English Français
Difficulty Level
Beginner
Published on :
The script begins by standardizing the numerical variables of the SASHELP.CLASS dataset using the median as the center and the MAD (Median Absolute Deviation) as a robust scale. Then, it performs temperature conversions from Celsius to Fahrenheit and Kelvin, illustrating basic matrix operations in IML. Finally, a simple arithmetic calculation example is presented.
Data Analysis

Type : MIXTE


The script uses the SASHELP.CLASS dataset for standardization. Temperature data and variables 'x', 'y' are created directly within the IML script.

1 Code Block
PROC IML
Explanation :
This block starts by defining a `SPI` library pointing to `SASUSER`. It then initializes a `PROC IML` session to read the numerical variables from the `SASHELP.CLASS` dataset. It calculates the median (`c`) and MAD (`s`) of each variable, then standardizes the data (`stdX`) and displays the calculated statistics.
Copied!
1LIBNAME SPI (SASUSER); /* location of data sets */
2 
3PROC IML;
4 /* standardize data by using robust estimates of center and scale */
5 use Sashelp.Class; /* open data set for reading */
6 read all var _NUM_ into x[colname=VarNames]; /* read variables */
7 close Sashelp.Class; /* close data set */
8
9 /* estimate centers and scales of each variable */
10 c = median(x); /* centers = medians of each column */
11 s = mad(x); /* scales = MAD of each column */
12 stdX = (x - c) / s; /* standardize the data */
13 
14 PRINT c[colname=VarNames]; /* print statistics for each column */
15 PRINT s[colname=VarNames];
2 Code Block
PROC IML Data
Explanation :
This block creates a `Celsius` vector with several values. It then converts these temperatures to Fahrenheit and Kelvin degrees using matrix operations and displays the results.
Copied!
1 /* convert temperatures from Celsius to Fahrenheit scale */
2 Celsius = {-40, 0, 20, 37, 100}; /* some interesting temperatures */
3 Fahrenheit = 9/5 * Celsius + 32; /* convert to Fahrenheit scale */
4 PRINT Celsius Fahrenheit;
5 
6 Kelvin = Celsius + 273.15; /* convert to Kelvin scale */
7 PRINT Kelvin;
3 Code Block
PROC IML Data
Explanation :
This block illustrates a simple arithmetic calculation in IML: assigning values to `x` and `y`, adding them to obtain `z`, then displaying the result. The `PROC IML` session ends with the `quit` statement.
Copied!
1 /* Present a simple example program with comments. */
2 /* The PROC IML statement is not required in SAS/IML Studio programs. */
3 x = 3; /* 1 */ /* NUMBERS indicate steps that */
4 y = 2; /* are described in a list */
5 z = x + y; /* 2 */ /* AFTER the program. */
6 
7 PRINT z; /* display result */ /* Other statements are briefly */
8 /* described WITHIN the program. */
9 
10QUIT;
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.

Related Documentation

Aucune documentation spécifique pour cette catégorie.