Published on :

Data preparation for earthquakes

This code is also available in: Deutsch Español Français
Awaiting validation
The program reads data from the SASHELP.QUAKES dataset, which contains information about earthquakes. It performs several transformations on the data: it initializes a new text variable 'Depth_Cat' to store depth categories, rounds the 'Magnitude' variable to one decimal place, and attempts to categorize 'Depth' into 'Shallow', 'Intermediate', or 'Deep'. However, due to the sequential implementation of 'IF' statements without 'ELSE IF' clauses, the 'Depth_Cat' variable will ultimately be set to 'Deep' for any observation where the depth is less than or equal to 700, overwriting previous assignments. The resulting 'work.earthquakes' table contains all original variables plus the new 'Depth_Cat' and modified 'Magnitude' variables.
Data Analysis

Type : SASHELP


The data is read from the 'quakes' dataset, which is part of the standard 'SASHELP' library provided with SAS. This is not an external data source in the sense that it should be manually managed or loaded by the script.

1 Code Block
DATA STEP Data
Explanation :
This code block is a DATA STEP that creates a new dataset named 'work.earthquakes'. It reads all observations from the source dataset 'sashelp.quakes'.

1. `length Depth_Cat $ 12;`: Defines the length of the new character variable 'Depth_Cat' to 12 characters.
2. `Magnitude=round(Magnitude, .1);`: Rounds the numeric variable 'Magnitude' to the first decimal place.
3. `if Depth<70 then Depth_Cat="Shallow";`: Attempts to categorize depth. If 'Depth' is less than 70, 'Depth_Cat' is set to 'Shallow'.
4. `if Depth<300 then Depth_Cat="Intermediate";`: If 'Depth' is less than 300, 'Depth_Cat' is set to 'Intermediate'.
5. `if Depth<=700 then Depth_Cat="Deep";`: If 'Depth' is less than or equal to 700, 'Depth_Cat' is set to 'Deep'.

**Important note on categorization logic:** Due to the use of sequential `IF` statements without `ELSE IF` clauses, conditions are evaluated independently. This means that if a depth is, for example, 50 (thus < 70, < 300, and <= 700), 'Depth_Cat' will first be 'Shallow', then overwritten by 'Intermediate', then overwritten again by 'Deep'. Therefore, for all depths <= 700, 'Depth_Cat' will ultimately end up with the value 'Deep'. For mutually exclusive categorization, the use of `IF...THEN ELSE IF...` would be necessary.
Copied!
1DATA work.earthquakes;
2 SET sashelp.quakes;
3 LENGTH Depth_Cat $ 12;
4 Magnitude=round(Magnitude, .1);
5 IF Depth<70 THEN Depth_Cat="Shallow";
6 IF Depth<300 THEN Depth_Cat="Intermediate";
7 IF Depth<=700 THEN Depth_Cat="Deep";
8RUN;
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.