Professional Documents
Culture Documents
PDF Sas For R Users A Book For Budding Data Scientists First Edition Ohri Ebook Full Chapter
PDF Sas For R Users A Book For Budding Data Scientists First Edition Ohri Ebook Full Chapter
https://textbookfull.com/product/analyzing-health-data-in-r-for-
sas-users-1st-edition-monika-maya-wahi/
https://textbookfull.com/product/feature-engineering-for-machine-
learning-principles-and-techniques-for-data-scientists-first-
edition-casari/
https://textbookfull.com/product/doing-data-science-in-r-an-
introduction-for-social-scientists-1st-edition-mark-andrews/
https://textbookfull.com/product/data-analysis-techniques-for-
physical-scientists-1st-edition-claude-a-pruneau/
SAS certification prep guide base programming for SAS 9
Fifth Edition. Edition Sas Sas Sas
https://textbookfull.com/product/sas-certification-prep-guide-
base-programming-for-sas-9-fifth-edition-edition-sas-sas-sas/
https://textbookfull.com/product/clinical-trial-data-analysis-
with-r-and-sas-second-edition-chen/
https://textbookfull.com/product/practical-statistics-for-data-
scientists-2nd-edition-peter-bruce/
https://textbookfull.com/product/introduction-to-data-analysis-
with-r-for-forensic-scientists-international-forensic-science-
and-investigation-1st-edition-curran/
https://textbookfull.com/product/analysis-of-correlated-data-
with-sas-and-r-mohamed-m-shoukri/
SAS for R Users
SAS for R Users
Ajay Ohri
Delhi, IN
This edition first published 2020
© 2020 John Wiley & Sons, Inc.
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system,
or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording
or otherwise, except as permitted by law. Advice on how to obtain permission to reuse material
from this title is available at http://www.wiley.com/go/permissions.
The right of Ajay Ohri to be identified as the author of this work has been asserted in accordance
with law.
Registered Office
John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, USA
Editorial Office
111 River Street, Hoboken, NJ 07030, USA
For details of our global editorial offices, customer services, and more information about Wiley
products visit us at www.wiley.com.
Wiley also publishes its books in a variety of electronic formats and by print‐on‐demand. Some
content that appears in standard print versions of this book may not be available in other formats.
10 9 8 7 6 5 4 3 2 1
This book is dedicated to my students and my family, my son Kush Ohri,
members of my church, and my God Jesus Christ.
vii
Contents
Preface xiii
Scope xiv
Preface
I would like to thank the generosity of the SAS Institute and its employees
to provide SAS On Demand for Academics for free without whom this book
would not exist. In addition, I also want to thank the baristas from Starbucks
Gurgaon. These are the people who downvote my questions on Stackoverflow.
You inspire me guys.
SAS for R users is aimed at entry‐level data scientists. It is not aimed at
researchers in academia nor is it aimed at high‐ end data scientists working on
Big Data, deep learning, or machine learning. In short, it is merely aimed at
human learning business analytics (or data science as it is now called).
Both SAS and R are widely used languages and yet both are very different.
SAS is a programming language that was designed in the 1960s which is broadly
divided into Data Steps and a wide variety of Procedure or PROC steps, while
R is an object oriented, mostly functional, language designed in the 1990s.
There are many, many books covering either but only very few books
covering both.
Why then write the book? After all, I have written two books on R, and one
on Python for R. SAS language remains the most widely used language in
enterprises, contributing directly to the brand name, and profitability of one
of the largest private software companies that invests hugely in its own research
instead of borrowing research in the name of open source. A statistics student
knowing Python (esp Machine Learning ML), R, SAS, Big Data (esp Spark ML),
Data Visualization (using Tableau) is a mythical unicorn unavailable to
recruiters who often have to settle for a few of these skills and then train them
in house.
As a teacher, I want my students to have jobs – there is no ideological tilt to
open source or any company here. The probability of students getting jobs
from campus greatly increases if they know BOTH SAS and R not just one of
them. That is why this book has been written.
xiv
Scope
This book is designed for professionals and students; people who want to enter
data science and who have a coding background with some basics of statistical
information. It is not aimed at researchers or people who like giraffes and do
not read the book from the beginning.
1
1.1 About SAS
SAS used to be called the Statistical Analysis System Software suite developed
by the SAS Institute for advanced analytics, business intelligence, data man-
agement, and predictive analytics. Developed at North Carolina State
University from 1966 until 1976, when the SAS Institute was incorporated. It
was then further developed in the 1980s and 1990s with the additional statisti-
cal procedures and components. SAS is a language, a software suite and a com-
pany created by Anthony James Barr and James Goodnight along with two
others. For purposes of this book we will use SAS for SAS computer language.
●● SAS also provides a graphical point and click user interface for non‐technical
users.
While a graduate student in statistics at North Carolina State University,
James Goodnight wrote a computer program for analyzing agricultural data.
After a few years, James’s application had attracted a diverse and loyal
following among its users, and the program’s data management and reporting
capabilities had expanded beyond James’s original intentions.
In 1976, he decided to work at developing and marketing his product on a
full‐time basis, and the SAS Institute was founded. Since its beginning, a dis-
tinguishing feature of the company has been its attentiveness to users of the
software. Today, the SAS Institute is the world’s largest privately‐held software
company, and Dr. James Goodnight is its CEO. He continues to be actively
involved as a developer of SAS System software as well as being one of the most
widely respected CEOs in the community.
SAS for R Users: A Book for Data Scientists, First Edition. Ajay Ohri.
© 2020 John Wiley & Sons, Inc. Published 2020 by John Wiley & Sons, Inc.
2 SAS for R Users
1.1.1 Installation
While SAS Software for Enterprises is priced at an annual license, for students,
researchers and learners you can choose from the SAS University Edition
(a virtual machine) at https://www.sas.com/en_in/software/university‐edition.
html or SAS on Demand at https://odamid.oda.sas.com/SASLogon/login
(a software as a service running SAS in browser).
To install the SAS University Edition on your Virtual machine you can follow
the following steps (I am using VMware Workstation for this):
●● Run your Virtual Machine and click on file.
●● Open and select SAS University Edition (the extension of the file should
be .ova). You can provide a new name and storage path for your new Virtual
Machine and then import.
●● Now, you need to initially run the virtual machine and use the link provided
in the VM to connect to the SAS University Edition in your browser.
1.2 About R
R is a language and environment for statistical computing and graphics. It is
a GNU project which is similar to the S language and environment which was
developed at Bell Laboratories (formerly AT&T, now Lucent Technologies)
by John Chambers and colleagues. R can be considered as a different imple-
mentation of S. R was initially written by Robert Gentleman and Ross Ihaka.
About SAS and R 3
1.2.2 Installation of R
You can download and install R from https://www.r‐project.org (or specifically
from https://cloud.r‐project.org for your operating system). You can then
download and install the IDE RStudio from https://www.rstudio.com/
products/rstudio/download/#download. Lastly, you can install any of 12 000+
packages (see https://cran.r‐project.org/web/views and https://www.
rdocumentation.org) using install.packages(“PACKAGENAME”) from
within R. These packages can be downloaded from the CRAN (Comprehensive
R Archive Network).
Within https://www.datacamp.com/community/tutorials/r‐packages‐guide,
R packages are collections of functions and datasets developed by the com-
munity. They increase the power of R either by improving existing base
R functionalities, or by adding new ones. For example, you can use sqldf
package to use SQL with R and RODBC package to connect to RDBMS
databases.
In addition, an excellent resource is how to learn SAS for R users from
the SAS Institute itself.
https://support.sas.com/edu/schedules.html?ctry=us&crs=SP4R
4 SAS for R Users
data ajay;
set input;
run;
b) Proc Step which are procedural steps for analysis and output.
Function SAS R
Import data proc import read_csv (readr package)
Print data proc print ajay
data=ajay;
run;
Structure proc contents str(ajay)
of Data data=ajay;
Object run;
Frequency of proc freq data= table(ajay$var1,ajay$var2)
Categorical ajay;
Variables tables var1*var2;
(Cross run;
Tabulation)
Analysis of Proc means summary(ajay$var1,ajay$var2)
Numerical Proc means data=
Variables ajay; library(Hmisc)
without/ Var var1 var2; summarize(ajay$var1,ajay$grp
with Run; 1,summary)
grouped by
another Proc means summarize(ajay$var1,ajay$grp
variable daya=ajay; 1,summary)
Var var1 var2;
Class grp1;
run;
1.5 Summary
In this chapter we have introduced R and SAS languages, and briefly compared
their main functions/syntax.
4 Who designed R?
8 TASK: Suppose you know SQL. Can you identify functions or packages you
can use in SAS and R respectively to run SQL commands?
Quiz Answers
1 JAMES GOODNIGHT
2 1976
5 1993
6 UNIVERSITY OF AUCKLAND
7 SAS
2.1 Importing Data
Importing data is the first step in analyzing data. It is important that you
have reliable and relevant data. You should be able to import data correctly
because the computer processes what data you input. If the imported data is
faulty, the analysis that you will receive after performing various tasks on it will
also be erroneous and misleading.
This concept is also commonly known as GIGO (Garbage In Garbage OUT).
Therefore, the input step is one of the most important steps in the data science
pipeline. There could also be different ways to input data in R and SAS from
files or from data connections. Importing of datasets calls for certain functions
in R whereas it calls for certain procedures for the same in SAS.
2.1.1 Packages in R
Importing of data in R can be done using certain packages and functions, and
to use those packages, we need to install them in our application.
Installing a package has the following command in R:
install.packages(“package_name”)
After installation to use this package you must load that package. Loading
a package means getting the package in active state (session). To load a
package use:
library(“package_name”)
SAS for R Users: A Book for Data Scientists, First Edition. Ajay Ohri.
© 2020 John Wiley & Sons, Inc. Published 2020 by John Wiley & Sons, Inc.
8 SAS for R Users
Updating a package:
update.packages(“package_name”)
Note that we install the package only once, we update it occasionally and we
load it every time we begin a R session. To unload a package, we use:
detach.packages(“package_name”)
uninstall.packages(“package_name”)
data first;
infile datalines
input m n;
datalines;
5 10
10 20
20 40
;
run;
Data Input, Import and Print 9
data second;
infile datalines
input name$ points;
datalines;
ajay 10
kush 9
;
run;
data third;
infile datalines missover dsd;
input m n o;
,2,3
4,,6
7,8,9
Here we use the proc. import step to import a raw data file and save it as an
SAS dataset. DBMS is used to specify the file type, e.g.: CSV, XLS etc.
getnames = yes is to specify that the first row contains column names.
Note: The type of dataset created (temporary or permanent) depends on the
name you specify in the out = statement.
A permanent dataset has to be referenced by a two‐level name: ‐ library_
name.data_set_name whereas a temporary dataset just has a one‐level
name.
data temp_setname;
set libname.permaset_name;
run;
2.3 Importing Data in R
There are a number of ways to import data into R, and several formats are
available:
1) From CSV files using readr or data. Table package
2) From Excel to R
3) From SAS to R
4) From SPSS to R
5) From Stata to R, and more
6) From Relational Databases (RDBMS) using RODBC
7) From json files using jsonlite package
https://rforanalytics.wordpress.com/useful‐links‐for‐r/odbc‐databases‐for‐r
Let us explore some of the ways to import data in R.
Data Input, Import and Print 11
read.csv("file_path.csv") or read.table("file_path.csv")
read_csv("file_path.csv")
fread("file_path.csv")
You can use the system. Time() function to verify that as follows:
system.time(read.csv("file_path.csv"))
system.time(read_csv("file_path.csv"))
system.time(fread("file_path.csv"))
install.packages("readxl")
library("readxl")
read_excel("file_path.xls")
12 SAS for R Users
Example: To import sheet 1 of an excel file with the first row as column
names
We can also use sheet names put within double quotes instead of the sheet
number to specify the sheet we want from any excel file.
install.packages("sas7bdat")
library("sas7bdat")
read.sas7bdat("file_path.sas7bdat")
install.packages("foreign")
library("foreign")
read.spss("file_path.spss")
read.dta("file_path.dta")
objectname=value;
or
objectname <‐ value;
objectname=read_csv("file_path",parameters)
Data Input, Import and Print 13
> ajay=c(11,2,30)
> ajay
[1] 11 2 30
We can do the same for other types of data except string variables which will
be in quotes (i.e. “ten”)
ajay=c(10,20,30,40)
dates2=c("26jun98","1/09/2005","1January2016")
newone=c("Raj","Shiva","Kamal","Ajay")
ajay2=c(23,45,78,NA,NA,89,NA)
is.na(ajay2)
## [1] FALSE FALSE FALSE TRUE TRUE FALSE TRUE
ajay3=data.frame(c("a","b"),c(1,2))
a 1
b 2
my_matrix = matrix(
c(2, 4, 3, 1, 5, 7),
nrow=3,
ncol=2)
This code makes a matrix with values in c() arranged in three rows and two
columns arranged column wise. Note: vector and matrix must have all values
of the same type but data frames can have values of different types.
ajay=NULL
for (i in 1:20) {
ajay[i]=i}
print(ajay)
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
data first;
infile datalines
input m n;
datalines;
5 10
10 20
20 40
;
run;
data second;
infile datalines ;
input name$ points;
datalines;
ajay 10
kush 9
;
run;
data third;
infile datalines missover dsd;
input m n o;
datalines;
,2,3
4,,6
7,8,9
;
run;
Missover option is used to prevent the data step from going to the next line
if it does not find values for all variables in the input statement in the current
record. Here the dsd option is used to treat commas as separator characters.
16 SAS for R Users
2.6 Printing Data
After importing the data, the next important step is to print that data to have a
look at the type of data you now have to analyze.
run;
The code below will help you print the first five observations of the dataset
named ajaydat.
run;
The code below will help you print the observations ranging from 10 to 20
for dataset ajaydat.
run;
2.6.2 Print in R
In R, printing of data does not need any function or package. You simply write
the dataset name and then run it to print the data.
If you read data in mydata and write the data_set name:
The whole data in mydata will be printed at console.
mydata=read_csv("file_path")
mydata
Data Input, Import and Print 17
Only the first observation of mydata is printed to the console. Default value
of n is 6.
head(mydata,n)
mydata[10:20]
2.7 Summary
Importing data in R requires a variety of functions to import different types of
files whereas proc. import is used with different options or parameters to
import any type of file in SAS. Data input in R is done using the c() function
and using a data step with input option in SAS. In R, printing a dataset just
requires the writing of the name of the dataset and running it, whereas SAS
uses proc. print to print any dataset.
4 Which function in R can you use to measure the time taken by a code to
execute?
6 How can you create a temporary dataset from a permanent one in SAS
using a data step?
8 What is the missover option used for in the infile statement in a SAS data
step?
Another random document with
no related content on Scribd:
The Harpy Eagle.
Owls and Eagles.
CHAPTER I.
introduction.
CHAPTER II.
About my Birth.—The Death of my Parents.—My first Journey.—My
Wonder at seeing the Country.—Lambs.—I find out where
Milk comes from.—Reflections and good Advice.
I was born in the city of New York, in the year 1790. My parents
were both English people. At first, they were in poor circumstances,
but my father became a merchant, and acquired some property. He
died, however, in the midst of success; and in a few months after my
mother followed. I was thus left an orphan, at the age of six years,
but with a fortune of about ten thousand dollars.
My mother had a brother living in the small town of Salem,
situated upon the eastern border of the State of New York, and
touching the line of Connecticut. He kept a tavern; and, as it was
upon the great road that was then the route between Boston and
New York, he had a good situation and a thriving business.
To the care of this uncle I was committed by my mother’s will, and
immediately after her death I was taken to my uncle’s residence. I
had never been out of the city of New York, and had never seen the
country. I had supposed the world one great city, and never fancied
that there were hills, and forests, and rivers, and fields without any
houses. I still remember my journey from New York to Salem very
well. I remember that the sight of so many new things, put the
recollection of my father and my mother out of my mind, and
banished the sorrow I had felt at seeing my parents laid into the
coffin, and carried away, to return to me no more. I was delighted at
everything I met, and particularly remember some lambs that I saw
playing on a hill-side. They were scampering about, jumping from
rock to rock, and chasing each other at full speed. I had never seen
a lamb before, and I thought these the prettiest creatures that were
ever made. I have since seen lions and tigers, and many other
strange creatures; but I have never met with any animal, that excited
in me half the admiration that I felt when I saw those little lambs.
I suppose some of my young friends in the country will laugh at
what I am now going to tell them; but it is nevertheless true. As I was
going from New York to Salem, we stopped one night at a small inn.
When we arrived at this place, the sun was an hour high, and I had
some time to play about the house. As I was running around,
peeping at every new and strange thing, I saw some cows in the
barn-yard. I had seen cows before, but still I went up to the gate and
looked through, and there I saw a woman, sitting upon a little stool,
and milking one of the cows. Now I had never seen a cow milked
before, nor, indeed, did I know where milk came from. I had not
thought about it at all. If I had been asked the question, I should
probably have said, that we got milk as we do water, by pumping it
from the cistern, or drawing it out of the well.
I looked at the woman for some time, wondering what she could
be about. When she had done, she came out of the yard, and I saw
that her pail was full of milk. “What is that that you have got?” said I.
“It is milk,” said the woman. “Where did you get it?” said I. “I got it
from the cow, you little simpleton!” said the woman; and then she
went into the house.
I did not like to be called a simpleton, for I had come all the way
from the great city of New York, and supposed that I knew
everything. I soon found, however, that I was ignorant of many useful
things that children of my age in the country were well acquainted
with.
The little incident, however, that I have just related, was not
without its use to me. It set me thinking about other things, and I
began to ask questions about every article of food and dress,—
where they came from, and how they were made; and, in this way, I
obtained a great deal of knowledge. I would recommend it to my
young readers to follow my example in this respect. They will find it
very amusing to study into these matters. Let them one day inquire
about hats, what they are made of, where the materials come from,
how they are obtained, and how they are wrought into hats. Another
day, let them take up the subject of coats, and learn all about the
cloth, the buckram, silk, twist, and buttons, that are used in making
them. So let them go through with dress; and then they may inquire
about bread, and other articles of food; and then they may learn all
about the furniture in the house. From this subject, they may go on
and learn how houses are built. I can assure my young readers, that,
in this way, they may spend their time very pleasantly, and become
well acquainted with all those useful things with which we are
surrounded. If I had done this before I went to Salem, I should have
known where milk came from, and not been called simpleton by a
milkmaid.
CHAPTER III.
Wise Observations.—Story of the Hat.
CHAPTER IV.
Arrival at my Uncle’s.—The Village.—Bill Keeler.—My first Day at
School.—Trouble.
I must now return to the story of myself. The morning after I left
the little tavern where I discovered how milk was obtained, we
proceeded on our journey, and at evening arrived at my uncle’s
house. It was an old-fashioned building, painted red, with a large
sign swinging in front, upon one side of which was the picture of a
stout barn-yard cock, and on the other side was the head of a bull.
So my uncle’s tavern went by the name of the “Cock and Bull.”
I soon became acquainted with the family, and in a few weeks
was quite familiar with the main street and all the by-lanes in the
village. My uncle had no children, but there was living with him a boy
about ten years old, by the name of Bill Keeler. He became my
principal companion, and, being a very knowing sort of lad, gave me
an insight into many things, which I could not otherwise have
understood.
After I had been at my uncle’s about six months, it was concluded
to send me to school. I was now seven years of age, but, strange as
it may seem to boys and girls of the present day, I did not know my
letters, and, what is more remarkable, I had a great dislike to the
idea of going to school. I believe it is the case that all people who
grow up ignorant acquire a settled dislike to learning and learned
people. As an owl can see best in the dark, because the light seems
to put his eyes out, so ignorant people love ignorance and darkness,
because truth and knowledge offend and distress them. I mention
these things as a warning to my reader against growing up in
ignorance, and thereby becoming a lover of darkness, rather than
light.
Well, I went to school for the first time, and I remember all about it
to this day. The schoolhouse was situated in a large space, where
four roads met. It was a bleak and desolate hill-side, partly covered
with heaps of stones, thrown out of the path, or gathered from the
neighboring fields. There were a few groups of tangled briers and
stunted huckleberry bushes amid these heaps of stones. On the
lower side of the hill, there was an old gnarled oak growing out of a
heap of splintered rocks, at the foot of which there bubbled forth a
small stream of pure water. This fountain went by the pretty name of
“Silver Spring.”
Bill Keeler led me into the school, which was then kept by
Mistress Sally St. John. She looked at me through her spectacles,
and over her spectacles, and then patted me on the head, told me I
was a good boy, and sent me to a seat. In about an hour I was called
up, the spelling-book opened, and the alphabet being placed before
me, the mistress pointed to the first letter, and asked me what it was.
I looked at the letter very carefully, and then gazed in the face of
Mistress St. John, but said nothing. “What’s that?” said she,
peremptorily, still pointing to the first letter of the alphabet. Now I
hadn’t been used to being scolded, and therefore felt a little angry at
the manner in which the school-mistress addressed me. Beside, at
that moment I saw Bill Keeler at the other end of the room, looking at
me with a saucy twinkle in his eye, which made me still more angry.
“What’s that?” again said the school-mistress, still sharper than
before. It was time for me to do something. “I’ll not tell you!” said I.
“Why not?” said the school-mistress, greatly amazed at my conduct.
“Because I didn’t come here to teach you your letters; but I came
here to learn them.”
The school-mistress shut up her book. Bill Keeler rolled up his
eyes, and made his mouth into a round O. “Go to your seat!” said the
school-mistress. I turned to go. “Stop!” said the school-mistress,
fetching me a slap on the side of the head; at the same moment she
opened the book, and again presented the alphabet to my view.
“Look, there!” said she, pointing with her finger to the top letter; “do
you see that?” I answered, “Yes.” “Well, that’s A,” said she. “That’s
A?” said I, doubtingly. “Yes,” said the mistress sharply. “I don’t
believe it!” said I. “Why don’t you believe it?” said she. “Because I
never heard of it before,” I replied. “Go to your seat!” said the school-
mistress; and away I went.
Such was my first day’s schooling. In the evening, Mistress St.
John called upon my uncle, and told him I was the most stupid
creature she ever saw, and very ill-mannered beside; and she hoped
I would by no means be permitted to come again to her school. My
uncle was greatly offended, not with me, but with the school-
mistress. He declared I should not go near her again; and, for more
than a year, I was permitted to amuse myself in my own way. I was
greatly pleased with all this at the time, but I have since often
thought how severely I was punished for my ill behavior at school.
For more than a year, I was left to run about in idleness, getting bad
habits, and losing the precious time that should have been devoted
to the acquisition of knowledge. Thus it always happens, that, soon
or late, we are made to suffer for our misconduct.
(To be continued.)
Swallows.