You are on page 1of 7

Bios J709/J710

Homework 5 (36 pts)


Name: CHANDANA SAI CHIDRALA

Files needed
1. Nines.sas7bdat

Things to do:
1. Answer the problems below using SAS. You should put all answers in this word
document.
2. Save all your code in the sections marked finished code
3. Use comments before each section/problem in the SAS Code file (proc procedure)
a. For example:
*Problem 1 – Using the PROC Freq procedure;
Or /* Problem 1 – Using Proc freq */
4. Also use Titles for each procedure that sends output to the output window – Comments
and titles are different (I would like both)
a. For example:
Title “Problem 1 – Using proc Contents”;

1
Problems

1. Run the program below in the Unfinished Code section. Add the following to the data step:
(10 pts)
a. Use “do groups” to add the variables “PulseGroup” and “SBPGroup”, with the
following criteria:
i. For ages less than 50 years of age:
1. If Pulse is less than 70, set PulseGroup equal to ‘Low’
2. Otherwise, set PulseGroup equal to ‘High’
3. If SBP is less than 130, set SBPGroup equal to ‘Low’
4. Otherwise, set SBPGroup equal to ‘High’
ii. For ages greater than or equal to 50 years of age:
1. If Pulse is less than 74, set PulseGroup equal to ‘Low’
2. Otherwise, set PulseGroup equal to ‘High’
3. If SBP is less than 140, set SBPGroup equal to ‘Low’
4. Otherwise, set SBPGroup equal to ‘High’
Final dataset should look like this

Unfinished Code

*HW 5 - Problem 1;
data HW.vitals;
input ID $3. Age Pulse SBP DBP;
label SBP = "Systolic Blood Pressure"
DBP = "Diastolic Blood Pressure";
datalines;
001 23 68 120 80
002 55 72 188 96
003 78 82 200 100
004 18 58 110 70
005 43 52 120 82
006 37 74 150 98
;
Run;

2
Finished Code

title "problem 1- Using Do Loops ";

*HW 5 - Problem 1;

data HW.vitals;

input ID $3. Age Pulse SBP DBP;

label SBP = "Systolic Blood Pressure"

DBP = "Diastolic Blood Pressure";

if Age < 50 then do;

if Pulse < 70 then PulseGroup = 'Low';

else PulseGroup = 'High';

if SBP < 130 then SBPGroup = 'Low';

else SBPGroup = 'High';

end;

else if age ge 50 then do;

if Pulse < 74 then PulseGroup = 'Low';

else PulseGroup = 'High';

if SBP < 140 then SBPGroup = 'Low';

else SBPGroup = 'High';

end;

datalines;

001 23 68 120 80

002 55 72 188 96

003 78 82 200 100

004 18 58 110 70

005 43 52 120 82

006 37 74 150 98

Run;
3
2. Merge the datasets (runners & results) by “ID” into a new dataset called “Two” in the HW
library. You will need to adjust the input statements first. Also do the following: (10 pts)

a. Create a variable called dob (date of birth) using the variables mth, day, & year to
create a date variable (you need another sas function for this).
b. Reduce this data set to only those people that have race results.
c. Create a new variable called avg which is the average of race1, race2 & race3.
Use the mean() function for this. Also round this new variable to 1 decimal place.
d. Create another variable called age which is the age today for each person. Have
the age stored as an integer (no decimals)
e. Drop the variables mth, day, and yr in the final data set.
f. Fill in the table below:

Name Avg Age


Steve 10.4 65
John 10.7 33
Susan 11.3 22
Danny 11.9 57

Unfinished Code

*HW 5 - Problem 2;
*Merge example with mdy, format & age;
data hw.runners;
input id name mth day yr ;
datalines;
01steve 9 11958
02john 11 21990
03susan 8 32001
04tina 7 41991
05suan 12101976
06danny 3111966
;
run;
data hw.results;
input id race1 race2 race3 ;
datalines;
0110.110.310.7
0311.211.111.7
0210.710.810.5
0612.211.911.7
;
run;
data hw.two;
run;

Finished Code

4
Title " Problem 2 - Using Proc sort and mean";
*HW 5 - Problem 2;
data hw.runners;
input id $2. name $ mth day yr;
format dob mmddyy10.;
dob = mdy(mth, day, yr);
datalines;
01 Steve 9 1 1958
02 John 11 2 1990
03 Susan 8 3 2001
04 Tina 7 4 1991
05 Susan 12 10 1976
06 Danny 3 11 1966
;
run;

data hw.results;
input id $2. race1 race2 race3;
datalines;
01 10.1 10.3 10.7
03 11.2 11.1 11.7
02 10.7 10.8 10.5
06 12.2 11.9 11.7
;
run;
proc sort data = hw.results;
by id;
run;
data hw.two;
merge hw.runners(in=run) hw.results(in=res);
by id;
if res;
avg = mean(race1, race2, race3);
format avg 4.1;
age = intck('year', dob, today());
drop mth day yr;
run;
proc print data=hw.two noobs;
var name avg age;
run;

5
3. Create a data set called “Three” with the following specifics: (8 pts)
a. First create a macro variable called “Start” and have it equal to 1.
b. Next create another macro variable called “end” and have it equal to 10.
c. Then create the data set using a do loop with the start and end macro variables as
the starting and ending points of the do loop.
d. In the do loop, create four variables
i. Var1 from a uniform distribution with a seed of 100
ii. Var2 from a uniform distribution with a seed of 100
iii. Var3 from a standard normal distribution with a seed of 101
iv. Var4 from a standard normal distribution with a seed of 101

Unfinished Code

*HW 5 - Problem 3;
data
run;

Finished Code

Title "Problem 3 - Using Random functions and do loop";


%let Start = 1;
%let End = 10;
data Three;
do i = &Start to &End;
Var1 = ranuni(100);
Var2 = ranuni(100);
Var3 = rannor(101);
Var4 = rannor(101);
output;
end;
drop i;
run;

SAS Data set

6
4. Use the SAS data set called “Nines” for the following tasks: (8 pts)
a. Create a new SAS data set called “nines2” from “nines”
b. Using an array remove all values of 999 from all variables in the data set.

Unfinished Code

*HW 5 - Problem 4;
data hw.nines2;
set hw.nines;
run;

Finished Code

Title "Problem 4 - using array";


data hw.nines2;
set hw.nines;
array numvars{8} x y z a1 a2 a3 a4 a5; /* Numeric variables */
array charvars{3} char1 char2 char3; /* Character variables */

do i = 1 to 8;
if numvars{i} = 999 then numvars{i} = .;
end;

do i = 1 to 3;
if charvars{i} = '999' then charvars{i} = '';
end;

drop i;
run;

SAS Data Set

You might also like