data cars;
input make $ vol hp mpg sp wt;
datalines;
Audi200QuatroWag 132 162 23.1 121 40
BMW750IL 119 295 16.7 157 45
BuickElectraWagon 160 140 22.9 110 45
BuickReatta 50 165 23.6 122 40
CadillacBrougham 129 140 22.9 110 45
CadillacBrougham 129 175 19.5 121 45
ChevroletBeretta 106 95 32.2 106 30
ChevroletCaprice 131 140 25.3 114 40
ChevroletCorsica 113 95 32.2 106 30
ChryslerLebaronConv 94 100 28.0 104 35
ChryslerNewYorker 121 150 23.6 117 40
DaihatsuCharade 92 53 46.5 96 20
DaihatsuCharade 94 80 43.4 107 22.5
DodgeColt 96 81 39.3 105 25
DodgeDaytona 99 100 31.5 108 30
DodgeDynasty 115 100 28.0 105 35
DodgeShadow 102 93 31.5 105 30
EagleSpirit 111 100 31.4 108 30
EagleSummit 101 81 35.1 102 27.5
FordEscort 103 90 42.2 109 25
FordEscort 101 84 36.1 103 27.5
FordEscortWagon 114 84 36.1 103 27.5
FordMustang 92 96 28.0 102 35
FordTempo 103 98 31.4 107 30
FordThunderbird 116 120 25.6 107 40
GM/GeoMetro 92 55 56.0 97 20
GM/GeoMetroLSI 92 55 55.9 97 20
GM/GeoMetroXF1 89 49 65.4 96 17.5
GM/GeoPrism 97 102 35.4 111 27.5
GM/GeoSprint 92 55 45.4 97 20
GM/GeoSprintTurbo 89 70 46.2 105 20
GM/GeoStorm 89 95 38.8 111 25
HondaCivic 99 92 40.9 113 22.5
HondaCivic 99 92 40.9 110 25
HondaCivic 99 92 38.4 110 25
HondaCivicCRX 50 92 40.9 113 22.5
HondaCivicCRX 50 92 38.8 113 22.5
HondaCivicCRX 50 92 38.4 110 25
HondaCivicCRXHF 50 62 59.2 98 22.5
HondaCivicCRXHF 50 62 53.3 98 22.5
HondaCivicWagon 117 92 38.4 110 25
IsuzuStylus 101 95 40.0 111 25
JaguarXJSConvert 50 263 17.0 147 45
LexusLS400 112 245 23.5 148 40
LincolnContinental 123 140 23.9 114 40
Mazda323Protege 107 103 36.3 112 27.5
Mercedes500SL 50 322 18.1 165 45
Mercedes560SEL 115 238 17.2 140 45
Nissan300ZX 50 280 23.4 160 40
NissanCentraCoupe 98 90 35.1 106 27.5
NissanCentraWagon 88 90 35.0 106 27.5
OldsCutlassSup 113 180 30.4 133 35
OldsCutlassSup 113 160 28.9 125 35
Oldsmobile98 127 165 23.6 122 40
OldsTrof/Toronado 114 165 23.6 122 40
PontiacBonneville 123 165 23.6 122 40
PontiacLeMans 107 74 40.7 101 25
PontiacSunbirdConv 88 95 32.2 106 30
Rolls-RoyceVarious 107 236 13.2 130 55
Saab9000 124 130 28.0 115 35
SubaruLoyale 102 90 29.5 109 25
SubaruJusty 89 73 41.1 103 22.5
SubaruJusty 89 73 40.4 103 22.5
SubaruJusty 89 66 39.6 100 22.5
SubaruJusty4wd 89 73 39.3 103 22.5
SuzukiSwift 92 70 49.0 105 20
ToyotaCamry 101 115 33.7 109 35
ToyotaCamry 101 115 32.6 109 35
ToyotaCamry 101 115 31.3 109 35
ToyotaCamry 101 115 28.0 109 35
ToyotaCamryWagon 124 115 31.3 109 35
ToyotaCelica 86 102 33.2 109 30
ToyotaCelica 86 102 32.9 109 30
ToyotaCelica 86 130 31.2 120 30
ToyotaCorolla 113 102 35.3 111 27.5
ToyotaCorolla 92 130 32.3 120 30
ToyotaCorolla 92 102 32.2 109 30
ToyotaTercel 91 78 38.9 106 22.5
ToyotaTercel 91 78 38.2 106 22.5
VolksJettaDiesel 104 52 46.9 90 27.5
Volvo740 111 145 27.7 120 35
Volvo760Wagon 135 162 23.4 121 40
;
PROC PRINT DATA=cars;
RUN;
a.Create a new variable called Density which is determined by the vehicle weight (WT x 100) divided by the vehicle’s volume (VOL).
Print the 21st through the 30th observations from the data set including the new variable value.
b. b.Create a categorical variable, FuelEcon, based on the values of MPG in the data set.
Let Category 1 include all vehicles below 20.0 MPG, 2 = 20.0 to 29.9 MPG, 3 = 30.0 to 39.9, 4 = 40.0 to 49.9, and 5 includes anything 50 MPG or above.
Print just the Model and the FuelEconvariables for the entire data set.
I'm stuck on creating these variables for this data set. How would I go about doing this ? ( Part a and b should be seperate from each other)
To get you started, add this statement to your program for question a. Note doing maths in SAS is very similar to writing it by hand but using characters on the keyboard to substitute the mathematical ones.
Density = (wt * 100) / vol;
You need to use if else statements to create the categorical variable. Otherwise, you could use proc format to create a new format and create a new variable with a put statement and apply the format to mpg variable. I prefer to just use the if-else statements.
data cars2;
set cars;
density = (wt* 100)/ vol;
run;
title "Density variable for obs 21 - 30";
proc print data=cars2 (firstobs=21 obs=30);
var wt vol density;
run;
title;
data categories;
set cars;
*create FuelEcon;
if ^missing(MPG) and mpg < 20.0 then fuelecon=1;
else if 20.0 <= mpg < 30.0 then fuelecon=2;
else if 30.0 <= mpg < 40.0 then fuelecon=3;
else if 40.0 <= mpg < 50.0 then fuelecon=4;
else if mpg >= 50 then fuelecon=5;
run;
title "Check derivation of fuelecon variable";
proc means data=categories n nmiss min max;
class fuelecon / missing;
var mpg;
run;
title;
title "Print model and fuelecon for categories ds";
proc print data=categories;
var make fuelecon;
run;
title;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.