You are on page 1of 4

BIOS J709/J710

SAS Lab 13
Files needed:
Grades.txt
Main.sas7dbat

Copy all the above files into a folder on your flash drive. Create a SAS library called “lab” that
references this folder. (libname lab “g:\lab”;)

1. Run the unfinished code below which creates a SAS data set called “data1”. Notice there are a
lot of missing values.
a. Create a variable called MissCounter that gives a running total of the missing values
for “data1” as they are read in. Use the increment counter we discussed in the lecture
for this problem.

Unfinished Code

*Lab 13 – Problem 1;
data lab.data1;

datalines;
2 . 7 . . 3 5 . . 4 . 3 . 4
;
run;

Finished Code

*Lab 13 – Problem 1;

title "Running number of missing values";


data lab.data1;
input value @@;
if value=. then misscounter + 1;
datalines;
2.7..35..4.3.4
;
run;

proc print data=lab.data1;


run;

2. Change the following unfinished code in Red to perform the same actions, but using “do
groups” instead of if then statements.

Unfinished Code

*Lab 13 – Problem 2;
data lab.grades;
length Gender $ 1 Quiz $ 2 AgeGrp $ 13;
infile 'g:\lab14\grades.txt' missover;

1
input Age Gender Midterm Quiz FinalExam;
if missing(Age) then delete;
if Age le 39 then Agegrp = 'Younger group';
if Age le 39 then Grade = .4*Midterm + .6*FinalExam;
if Age gt 39 then Agegrp = 'Older group';
if Age gt 39 then Grade = (Midterm + FinalExam)/2;
run;

Finished Code
*Lab 13 – Problem 2;

title "Performing do groups";

data lab.grades;

length Gender $ 1 Quiz $ 2 AgeGrp $ 13;

infile '/home/u63561438/my_shared_file_links/rrmoran/Bios 710 -


Data/grades.txt';

input Age Gender Midterm Quiz FinalExam;

if missing(Age) then delete;

if Age le 39 then do;

Agegrp = 'Younger group';

Grade = 0.4*Midterm + 0.6*FinalExam;

end;

else if Age > 39 then do;

Agegrp = 'Older group';

Grade = (Midterm + FinalExam)/2;

end;

run;

proc print data=lab.grades;

run;

2
3. Using the code in the unfinished codes section below and the SAS data set called “Main”,
a. Recode the following variables: ormoth, orracem, orfath, & orracef so that any value
equal to 9 is recoded to a missing value (.). Use the SAS array procedures to perform
this recoding.
b. Show your results in a set of frequency tables for these 4 variables.

Unfinished Code

*Lab 13 – Problem 3;
data lab.mainclean;
set lab.main;

keep ormoth orracem orfath orracef;


run;
proc ;
run;

Finished Code
*Lab 13 – Problem 3;

title "Performing Recode";


data lab.mainclean;
set lab.main;
array vars[*] ormoth orracem orfath orracef;
do i = 1 to dim(vars);
if vars(i) = 9 then vars(i) =" ";
end;
drop i;
keep ormoth orracem orfath orracef;
run;

proc freq data=lab.mainclean;


tables ormoth orracem orfath orracef;
run;

4. Create a SAS data set called “hundred” using a do loop to create a 100 observations.
a. Add a simulated variable called “length” which is normally distributed and has a mean of
20 and a standard deviation of 4.
b. Add another simulated variable call “height” which is uniformly distributed with values
between 0 and 1.

Unfinished Code

*Lab 13 – Problem 4;
data
run;

Finished Code
*Lab 13 – Problem 4;

title "do loop performed";


data lab.hundred;
do i = 1 to 100;
3
length = rand("Normal", 20, 4);
height = uniform (1);
output;
end;
drop i;
run;

proc print data=lab.hundred;


run;

5. Using the code in the unfinished code section below, read in the 3 Fahrenheit temperatures
provided. Create variable called Celsius that is related to Fahrenheit by the following
equation: Celsius{Hour} = (Fahren{Hour} - 32)/1.8; Use a do loop to create this
Celsius variable.

Unfinished Code

*Lab 13 – Problem 5;
data lab.temp;
input Fahren1-Fahren3 @@;

datalines;
32 72 100
;
run;

Finished Code
*Lab 13 – Problem 5;

title "using array function";


data lab.temp;
input Fahren1-Fahren3 @@;
array Fahren[3];
array celsius[3] celsius1-celsius3;
do Hour =1 to 3;
Celsius{Hour} = (Fahren{Hour} - 32)/1.8;
end;
drop hour;
datalines;
32 72 100
;
run;

proc print data=lab.temp;


run;

You might also like