In this blog we will take some questions that is based on SAS and solve them by providing them with Program,Output and Explanation of the program that we have taken,the structure of the blog is as follows:
1.Questions:Problem statement
2.Program
3.Output
4.Explanation
Now we will consider the problems
PROBLEM 1 :Starting with the Blood data set, create a new, temporary SAS data set containing all the variables in Blood plus a new variable called CholGroup. Define this new variable as follows:
CholGroup- Chol
Low: Low – 110
Med: 111 – 140
High: 141 – High
Program:
libname A15017 “/folders/myfolders/sasblogs”;
data A15017.bloodcells;
infile “/folders/myfolders/blood.csv” dsd;
input id gender$ bloodgrp$ agegrp$ wbc rbc chol;
select;
when (chol lt 110) cholgroup=’low’;
when (chol lt 140) cholgroup=’medium’;
when (chol gt 140) cholgroup=’hig’;
otherwise;
end;
proc print data=A15017.bloodcells (obs=20) ;
run;
OUTPUT
EXPLANATION:
A library is created with A15017 and all the programs are save in those library
In the above I have taken blood.csv data set.In this ,the inputs are obs,id,Gender,Bloodgroup,Agegroup,wbc cells,rbc,chol. The problem statement is to create a temporary library which i have did at the starting of the program and also to create a new variable named "Cholgroup" and the values of cholgroup is based on chol with conditions like
if
Low – 110 then cholgroup has value low
111 – 140 then Medium
141 – High them High
PROBLEM 2:
There is a school in which kids are of age group 12 and 13, they have given their quiz and got grades like ‘A’, ‘B’, ‘C’, ‘D’, ‘F’ . They also completed their Mid term and final exams. Now the problem is Mid terms and finals are in numerical values (see table below) but Quiz in grades as explained earlier.
PROGRAM:(for data set)
data A15017.school;
input Age Quiz : $1. Midterm Final;
datalines;
12 A 92 95
12 B 88 88
13 C 78 75
13 A 92 93
12 F 55 62
13 B 88 82
;
proc print data=A15017.school noobs;
run;
Now we want to put all the kids of age 12 under category 6 and kids of age 13 under category 8, we shall assign numeric equivalents to grades for example for grade A we shall assign 95, for B, C, D, F we shall assign 85, 75, 70 and 65 respectively.
PROGRAM:
data A15017.A017_school;
set A15017.school;
*simple if else block of two lines to categorize kids of different age groups;
if (Age = 12) then Grade=6;
else if (Age = 13) then Grade = 8;
*Below five lines is to give numerical equivalents to grades in Quiz;
if(Quiz EQ 'A') then EqMarks = 95 ;
else if(Quiz EQ 'B') then EqMarks = 85;
else if(Quiz EQ 'C') then EqMarks = 75;
else if(Quiz EQ 'D') then EqMarks = 70;
else if(Quiz EQ 'F') then EqMarks = 65;
*Below code is to compute the course grade with weightages of 20% to quiz marks
30% to mid term and 50% to final marks;
CourseGrade = (EqMarks*0.2)+(Midterm*0.3)+(Final*0.5);
proc print data=A15017.A017_school noobs;
run;
OUTPUT:
The highlighted part is the final output that is required
Problem 3:Given values of x, y, and z, compute the
AbsZ = absolute value of z
Expx = e raised to the x power
Circumference = 2 times pi times y
Use values of x, y, and z equal to 10, 20, and –30, respectively. Round the values for b and c to the nearest .001.
Program:
libname A15017 “/folders/myfolders/sasblogs”;
data A15017.functions;
input x y z;
pi=constant(‘pi’);
absz=abs(z);
expx=round(exp(x),0.001);
circumference=round(2*pi*y,.001);
datalines;
10 20 -30
;
proc print;
run;
OUTPUT:
EXPLANATION:
The problem statement
here is to know about the commands abs, srqt, exp etc. The datalines are given
and we have to compute the absolute value exponent value and circumference in
this problem. Abs function is used to compute the absolute value. Exp function
is used to compute exponent value that is e power n. Here n is x. So if we give
exp(x) SAS takes it as e^x. To calculate circumference by the formula given in
question (2 times pi times y) first we have to define pi value. So we used
command constant(‘pi’). With this pi value is defined and we can compute
circumference. In the question we are also asked to round the values upto
.001th value so we used round function and we have given .001 also in the
function so that it gets rounded upto that value.
PROBLEM 4:
Using
the SAS data set Health, compute the body mass index (BMI) defined as the weight
in kilograms divided by the height (in meters) squared. Create four other variables
based on BMI: 1) BMIRound is the BMI rounded to the nearest integer, 2) BMITenth
is the BMI rounded to the nearest tenth, 3) BMIGroup is the BMI rounded to the
nearest 5, and 4) BMITrunc is the BMI with a fractional amount truncated.
Program:
libname
A15017 "/folders/myfolders/sasblog";
data
A15017.health;
infile
"/folders/myfolders/health.txt";
input
@1 ID $3.
@4 DOB mmddyy10.
@14 Height $3.
@17 weight $2.;
BMI
= (Weight/2.2) / (Height*.0254)**2;
BMIRound
= round(BMI);
BMIRound_tenth
= round(BMI,.1);
BMIGroup
= round(BMI,5);
BMITrunc
= int(BMI);
run;
proc
print;
run;
OUTPUT:
EXPLANATION:
The
problem statement here is to print the dataset Health and add new variables
BMIRound, BMIRound_Tenth, BMIGroup, BMITrunc. BMI is calculated from the
formula which features Height and Weight. The meaning of each variable is given
in the question. As the names suggest, ROUND is used to round numbers, either
to the nearest integer or to other values such as 10ths or 100ths. The INT
function returns the integer portion of a numeric value. In the output in DOB
column instead of proper date there are weird numbers. What does those numbers
means?
SAS
calculates all the dates from Jan 1st 1960. So the number -5485 indicates that the date
is 5485th day before 1960 Jan 1st and similarly for other
two observations. For printing traditional date format we have to give a format
statement in the program. The syntax for that is (format DOB date9.)
PRBLEM 5:
Problem: Lets learn how to use missing function in this section with the help of a dataset which contains variables lets say ‘A’, ‘B’, ‘C’ in these variables lets put 7 observations in each by deliberately missing some values in the variables.
Now our task is to find how many missing values each variable contains. For this we shall create three more variables to see the count value.
PROGRAM:
data missing;
input A $ B $ C $;
if missing(A) then MissA+1;
if missing(B) then MissB+1;
if missing(C) then MissC+1;
datalines;
X Y Z
X Y Y
Z Z Z
X X .
Y Z .
X . .
;
proc print noobs;
run;
OUTPUT:
EXPLANATION:
In the above output ,we can see the missing values of A are zero,B are one and C is 6
PROBLEM 6:
Merge
the Purchase and Inventory data sets to create a new, temporary SAS data set (Pur_Price)
where the Price value found in the Inventory data set is added to each observation
in the Purchase data set, based on the Model number (Model). There are some models
in the Inventory data set that were not purchased (and, therefore, are not in
the Purchase data set). Do not include these models in your new data set. Based
on the variable Quantity in the Purchase data set, compute a total cost (TotalCost)
equal to Quantity times Price in this data set as well.
PROGRAM:
libname
A15017 "/folders/myfolders/sasblog";
data
A15017.purchase;
input CustNumber Model $ Quantity;
datalines;
101
L776 1
102
M123 10
103
X999 2
103
M567 1
;
proc
sort;
by
model;
run;
proc
print;
run;
libname
A15017 "/folders/myfolders/sasblog";
data
A15017.inventory;
infile
"/folders/myfolders/inventory.txt";
input
model$ price;
proc
sort;
by
model;
proc
print;
run;
data
pur_price;
merge
A15017.purchase(in=Inpurchase) A15017 .inventory(in=Ininventory);
by model;
totalcost=quantity*price;
if Inpurchase=0 and Ininventory=1;
run;
proc print;
run;
OUTPUT:
EXPLANATION:
The
first problem is solved by using MERGE function.
The variable we are merging with is model. There are some models in the
Inventory data set that were not purchased. In order to exclude them we use IN function. Here we use if statement
having IN function in it.
PROBLEM 7:
Problem:
Problem:
we have three speed reading methods, for each method I have 10 observations of speeds in an unstructured way, now I have to read 10 observations for each speed reading method.
PROGRAM
data problem
do Method= 'A','B','C';
do Subj=1 to 10;
input Speed @;
output;
end;
end;
datalines;
250 255 256 300 244 268 301 322 256 333
267 275 256 320 250 340 345 290 280 300
350 350 340 290 377 401 380 310 299 399
;
proc print data=problem noobs;
run;
OUTPUT:
EXPLANATION:
In this first we will
assign the method as "A","B","C".
After assigning the
methods we will iterate the subjects from 1 to 10 to this methods and all the
values in the datalines are assigned to the subjects of the methods.
Problem 8:
In this there is a
screenshot that is attached below,problem statement is to divide the dataset by
their respective regions
PROGRAM:
libname
A15017 "/folders/myfolders/sasblog";
data
A15017.regions;
infile
"/folders/myfolders/Sales.csv" dsd ;
informat
name $15.
customer $20.;
input empid name$ region$ customer$ ;
proc
sort;
by
region;
run;
proc
print;
by
region;
run;
OUTPUT:
EXPLANATION:
In this the region is divided by using the sort procedure by region.
The whole dataset is divided into four different regions in the output.
PROBLEM 9:
In this the problem statement is to subset the dataset with respect to two measures one is country name and other model type
In our first subset we want to have all the observations where country is USA and Model type of bicycle is Mountain Bike and lets save it into a dataset named MountainUSA
In our second subset we want to have all the observations were country is France and Model Type of Bicycle is Road Bike.
PROGRAM:
set a15017.bicycles;
if Country = 'USA' and Model = 'Mountain Bike' then output a17_MountainUSA;
else if Country = 'France' and Model = 'Road Bike' then output a17_RoadFrance;
run;
title "List of MountainUSA";
proc print data=a17_MountainUSA;
run;
title "List of RoadFrance";
proc print data=a17_RoadFrance;
run;
OUTPUT:
EXPLANATION:
In the above program the data set is divided by using If condition
The conditions are two countries namely usa and France are taken into consideration and formed the subset fr this both countries
PROBLEM 10:
Using the Hosp data set, compute the frequencies for the
days of the week, months of the year, and year, corresponding to the admission
dates (variable AdmitDate). Supply a format for the days of the week and months
of the year.Use PROC FREQ to list these frequencies.
PROGRAM:
data hospital;
set hosp;
days=weekday(admitdate);
month=month(admitdate);
year=year(admitdate);
format days
dday.
month mnth.;
run;
proc
format;
value dday
1='sunday'
2='monday'
3='tuesday'
4='wednesday'
5='thursday'
6='friday'
7='saturday';
value mnth
1='jan'
2='feb'
3='march'
4='april'
5='may'
6='june'
7='july'
8='aug'
9='sept'
10='oct'
11='nov'
12='dec';
run;
proc print
data=hospital (obs=10);
run;
proc freq;
table days
month year;
run;
OUTPUT:
EXPLANATION:
In this program I have used set function because I
already have hospital dataset saved in library. Using set function I am taking
that dataset. The problem statement here is to calculate the frequencies of the
days, months and year of AdmitDate variable. For this we use proc freq
function. But before that to find no of days we use Weekday function. Similarly
Month and Year functions for calculating the month and year respectively. Proc
format is used to supply a formal to the functions days months because the
output will be in numbers 1-7 for weekdays and 1-12 for months. Atlast proc
freq is used to calculate the frequencies of days and months.













