Published on :
Général SASHELP

Sans titre

This code is also available in: Deutsch Español Français
Awaiting validation
Data Analysis

Type : SASHELP


Both DATA steps use the standard `sashelp.cars` dataset as the input source, which is an example dataset provided with SAS.

1 Code Block
DATA STEP Data
Explanation :
This DATA block creates a new dataset named `cars2` from `sashelp.cars`. It evaluates the `MSRP` value to assign a numerical `Cost_Group` (1, 2, 3, or 4) using a series of `IF-THEN/ELSE IF-THEN/ELSE` conditions. Only the variables specified in the `KEEP` statement are retained in the final dataset.
Copied!
1DATA cars2;
2 SET sashelp.cars;
3 IF MSRP<20000 THEN Cost_Group=1;
4 ELSE IF MSRP<40000 THEN Cost_Group=2;
5 ELSE IF MSRP<60000 THEN Cost_Group=3;
6 ELSE Cost_Group=4;
7 keep Make Model Type MSRP Cost_Group;
8RUN;
2 Code Block
DATA STEP Data
Explanation :
This DATA block replaces the previously created `cars2` dataset. It first defines the length of the new character variable `CarType` to 6. Then, it assigns the value 'Basic' to `CarType` if `MSRP` is less than 60000, and 'Luxury' in all other cases. The 'Make', 'Model', 'MSRP', and the new `CarType` variables are retained.
Copied!
1DATA cars2;
2 SET sashelp.cars;
3 LENGTH CarType $ 6;
4 IF MSRP<60000 THEN CarType="Basic";
5 ELSE CarType="Luxury";
6 keep Make Model MSRP CarType;
7RUN;
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.