For the January 20, 2022 workshop we will start with the first topic. If there is interest in the other topics, I will add more workshops in the schedule.
Before we can start using the SAS program and learn how to write the code and match it to our data and trials, we need to be aware of different versions of SAS that we have access to.
Available Versions of SAS
- PC Standalone Version – PC-SAS or SAS 9.4
- Available for Windows ONLY – if you’re using a Mac, you will need to have a VM to emulate Windows to run this version
- Available through CCS Software Distribution Centre for a fee, please check https://guelph.onthehub.com/WebStore/Welcome.aspx for updated pricing and access information or email firstname.lastname@example.org for more information
- SAS OnDemand For Academics – SAS Studio
- Since this is a Cloud system – please recognize that your data and programs reside in the cloud and NOT on your local system, and you are using their computer resources NOT your own system – accessed through a web browser with your own personal login
SAS 9.4 & SAS Studio (OnDemand): What Parts of SAS do you have access to?
SAS is an extremely large and complex software program with many different components. We primarily use Base SAS, SAS/STAT, SAS/ACCESS, and maybe bits and pieces of other components such as SAS/IML.
- BaseSAS – base SAS programming, DATA Step
- SAS/STAT – the PROCs used for statistical analyses
- SAS/IML – SAS’ matrix programming language
- SAS/ACCESS – allows you to interact with different data formats
- Some parts of SAS/ETS – time series analysis
To see exactly what you have access to, you can run the following code:
You will see the components available to you listed in the log window.
Also note the additional information available to you:
- License information
- Expiration date – very handy to be aware of, especially if you are running your own copy of your PC
- SAS components available to you
SAS 9.4: What does SAS look like?
There are a number of components to the SAS interface:
- Results and Explorer windows to the left
- Editor, Log, Output, and Results Viewer windows to the right, taking up most of the screen
SAS 9.4: What do each of these windows do?
- Results Window – a Table of Contents for all of your results.
- Explorer Window – similar to Windows Explorer – allows you to navigate SAS libraries and files
- Editor Window – this is where you will spend most of your time, writing and editing program files
- Log Window – this window is extremely helpful, think of it as your best friend in SAS, it tells you what SAS has done every step of your program and processing
- Output Window – SAS versions 9.2 and earlier, use this window to display all results and output. SAS 9.3 and higher use a new window called the Results Viewer. All the results are presented in an HTML format.
SAS Studio: What does SAS look like?
You’ll notice the same windows as SAS 9.4 – but called slightly different things. CODE = Editor; LOG = log; RESULTS=Results Viewer;
However, you’ll see a few handy items on the left-hand side of your screen in SAS Studio – items you will become very familiar with! But let’s dig into these as we work through getting our data in and so on – in the next part of our session.
How does SAS work?
SAS is divided into 2 areas:
- DATA step
- PROCs (short for PROCedures)
DATA step is all about data manipulation – one of the key strengths to SAS
PROCs – this is where you will find most of your statistical procedures.
Let’s Get Started with Reading Data
Notes to Reading Data into SAS are available as a PDF document for participants in the OAC W22 Intro to SAS workshop. If you did not participate in the workshop and would like a copy of the notes – please send me an email.
Before I learn a new software or new skills, I often like to do some homework and ask the silly questions like: what, when, why, and how, to give me a base understanding of the software. So, let’s work through these questions for R.
What is R?
R is a system that is used for statistical computation and graphics. It has a number of aspects to is that include a programming language, graphics, interfaces or connection opportunities with other languages, and debugging capabilities. I have found that many do not refer to R as a statistical software package, because it can do so much more.
What does this all mean? It means that R is a very robust program that folks use for a variety of reasons, it’s not just for statistical analysis!
Where did R come from?
The history of software packages can be quite interesting to learn about. For instance R has been defined as a “dialect” of S. Some of you may remember the statistical software called S-Plus? Well, that’s where R comes from. It was developed in the 1980s and has been one of the fastest growing open-source software packages since.
What does “open-source” mean?
I’m sure you’ve heard of this term in the past or in different contexts. One thing that you will hear when people talk about R, is that it is free or that is is open-source. Keep in mind that open-source means that it is freely available for people to use, modify, and redistribute. Which usually translates to: there is no cost to acquire and use the R software! Another aspect of open-source is that it is or rather can be community-driven. So, any and all modifications to the software and subsequent documentation (if it exists) is driven by the community.
Please note, that R has matured over the years, and today’s R community is extremely strong, and encouraging documentation for anything that is released, making it a very desirable product. This may not always be the case with open-source software.
Who uses R?
Business, academia, statisticians, data miners, students, and the list goes on. Maybe we should ask the question, who is NOT using R, and then ask the question Why?
There are so many different statistical software options today and which one you choose to use will depend on several different factors:
- What does your field of study use?
- If you are a graduate student, what does your supervisor suggest and use?
- What type of analyses are you looking to perform and does your program of choice offer those analyses?
- What types of support do you have access to?
How does R work?
If you’re looking for a statistical package that is point and click, R is not for you! R is driven by coding. YES! you will have to learn how to write syntax in R. You can use R interactively by using R-Studio, and you may never reach a point in your studies or your research where you will move away from the interactive capabilities of R – so no big worries! Besides, today there are a lot of resources available to help you learn how to use R. So don’t let that stop you!
Base R and Packages
When you download and install R, the Base R program is installed. To run many of the analyses you may be required to install a package. What is a package? It is a collection of functions, data, and documentation that extend the current capabilities of the Base R program. These are what makes R so versatile! As we work through our workshops and associated code, I will provide you with the name of the Package. There are a number of ways to acquire and install packages, we will review these as we work through them. Please note that there may be several packages that perform a similar analysis, please read all the documentation before selecting a package to use.
I will add a page to this Blog in the near future (Summer 2018) that will list the packages and associated documentation that I have used and recommend.
How do I acquire R? Where can I download it?
Visit the Comprehensive R Archive Network (CRAN) website to download the R software. https://cran.r-project.org/ Please note that this will also be the website used to download future packages used in analyses as well.
To download R-Studio, visit The RStudio website at https://www.rstudio.com/
Both websites have comprehensive instructions to assist you with the installation on your own computers.
Let’s get started by reviewing some definitions
When you think about conducting any statistical analysis, your starting point is data. So let’s start with a few definitions of the different data types observed in R.
Numeric, Character, or Logical
A quick overview of the different types of data you can work with in R.
- Numeric = numbers
- Character = words
- Logical = TRUE or FALSE – not all data is in the form of numbers or letters, sometimes you might have data that has been collected as matching a criteria (TRUE) or not matching a criteria (FALSE). We’ll work through examples of this in another session, for now just be aware that this type of data is commonly used in R.
- How do you find out what form your data are in?
- The results of this statement will tell you exactly what form your data are.
testform <- c(12, 13, 15)
Numeric Classes in R
Numbers are handled in a couple of ways in R. These are referred to as the Numeric Classes of R, and two that we will are known as integer and double. Having a basic understanding of these different numeric classes will come in handy.
- If you think back to high school math, you’ll probably remember the term “integer”. First thing that comes to my mind when I think of integer – is Whole number, no fractions, no decimal places.
- As you can imagine storing numeric data as integers does not require a lot of space. So, in terms of computing, if you do not foresee your analysis needing decimals and precision numbers, then integers are the way to go.
- Double precision floating point numbers – think of this as the decimals side of your numeric data.
- Storing Double numeric data takes up more space than Integer data. But sometimes you’re just not sure what you will need, so R will switch between the 2 numeric classes as it is required for your analysis.
Data Types in R
Let’s review the different data types available to you in R.
- Let’s not panic at some of these terms, but work through examples of each. Think of a vector as a column of data or one variable.
- Vectors can be numeric, characters, or logical format.
- How to create a vector:
# a numeric vector
a = c(2, 4.5, 6, 12)
# a character vector
b = c(“green”, “blue”, “yellow”)
# a logical vector
c = (TRUE, TRUE, FALSE, TRUE)
a = ; b = ; c = ; creating vectors called a, b, c respectively. Please note that a <- is the same as a =
c(x, x, x ) tells R that we are creating a vector or a column with the contents found in the parentheses. The , tells R to drop to the next row in the vector/column being created.
character values must be contained in ” “, but logical values do not.
- Think of a matrix as an object made up of rows and columns.
- The vectors within a matrix must all be the same type, so all numeric, or all character, or all logical.
- How to create a matrix:
# creates a 5 x 4 numeric matrix – 5 rows by 4 columns
y <- matrix(1:20, nrow=5,ncol=4)
y = or y <- create a matrix called y
matrix( ) – call the function matrix to create the matrix y
1:20 – the values of the matrix
nrows = let’s R know how many rows are in the matrix that you are creating
ncol= let’s R know how many columns are in the matrix that you are creating.
Resulting matrix y will look like:
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
- Arrays are very similar to matrices. Think of an array as a matrix with an added dimension. For example, we may have a matrix that contains data for 2015. We want to add in the same data for 2016 in the same format. So we can create an array, with a matrix that contains 2015 data and a matrix that contains a matrix of the 2016 data.
- A Data Frame is a general form of a matrix. What this really means, is that a data frame is like a dataset that we use in other programs such as SAS and SPSS. The columns or variables do not need to be the same type as is required in a matrix.
- We can have one vector/column/variable in a data frame that is integer (numeric), followed by a second one that is character, followed by a third that is logical. But in a matrix, all three vectors/columns/variables must be the same type: numeric, character, or logical.
- How to create a data frame:
d <- c(10, 12, 31, 4)
e <- c(“blue”, “green”, “red”, NA)
f <- c(TRUE, TRUE, TRUE, FALSE)
sampledata <- data.frame(d, e, f)
names(sampledata) <- c(“ID”, “Colour”, “Passed”) # variable names
sampledata <- or sampledata = name of the data frame that we are creating
data.frame( ) calling on the function that creates a data frame
d, e, f tells R that we are creating the data frame with the 3 vectors in the order of d, followed by e, followed by f
names(sac(“ID”, “Colour”, “Passed”) mpledata) – providing variable names within the data frame
c(“ID”, “Colour”, “Passed”) – creating or identifying the 3 variable names within the data frame: ID, Colour, Passed are the variable names
- an ordered collection of objects.
- objects in the list do not have to be the same type.
- You can create a list of objects and store them under one name.
- How to create a list:
# a string, a numeric vector, a matrix, and a scaler
wlist <- list(name=”Fred”, mynumbers=a, mymatrix=y, age=5.3)
wlist <- or wlist = creating a list called wlist
list( ) – calling the function to create a list
name=”Fred”, mynumbers=a, mymatrix=y, age=5.3 values that are to be contained in the list called wlist
Factors are categorical variables in your data. You can have a nominal factor or you can have an ordinal factor. Yup, those words again – remember nominal and ordinal data are categorical pieces of data, so you can fall into one group or another. Nominal, there is no relationship or order to the categories, whereas ordinal data there is an order to the different levels.
Available interfaces for using R
There are essentially two ways to use or interact with R: RStudio or by using the R Console. The code or syntax you will write or use will be the same for either interface, however RStudio provides you with a more interactive experience. This is the interface that I will use for these workshops. I will demonstrate the R Console to show you the basic differences.
Let’s take a tour and become familiar with the windows in RStudio
When you first open RStudio you’ll see 4 windows or 4 sections on your screen: editor, console, history, and environments with tabs. Let’s start with the environments window – you should see 6 tabs: Environment, Files, Plots, Packages, Help, and Viewer. The Environment tab lists the files/datasets that are being used during the current project. The Files tab allows you to view all the files that are available in your working directory. The Plots tab will show any plots that are created during your session. The Packages tab will list all packages that you have loaded. The Help tab is self-explanatory. A quick sidenote, the Help window is great! Please take advantage of it by using the search function in the Help tab.
The History window will list all the lines of code that you have run until you clear it out. A great way to see what you have done – especially if you encounter troubles along the way.
That leaves the editor and the console. The editor is where you open an R script file and the console is where you run your code as you type it in. To run code that is in your editor – select the bits of code and hit Ctrl-Enter to run it. In the console, you type the line, hit enter and it runs immediately. I use these two windows in tandem. To move between these two windows – Ctrl-2 moves you to the Console window and Ctrl-1 brings you back to the editor window. Of course, a mouse works great too!
One more quick tip – the console window can fill up quite quickly and to me, can feel very cluttered. Remember the History window will keep a history of your code, so it would be ok to clear out the console as you see fit. In order to do this, use Ctrl-L to clear it out.
Sometimes having your program always refer to the same directory, when saving files or when opening files, can be very handy. You’ll always know where your files are! R makes it very easy to accomplish it.
First, let’s do it the long way. To see what the current working directory of your RStudio is by typing in your editor window:
To change the working directory for the current project you are working on type:
Of course, you’ll want to make this a directory on your computer 😉 But as you look at this – do you notice anything odd about this statement??? You’ll notice that the slashes / are the opposite direction than you normally see on a Windows machine. Changing these manually can be a time consuming effort. One way around this is to add an extra \ after everyone in your location. See below:
Always double-check your working directory by checking getwd() Are the results what you were expecting? If not, try it again.
There are easier ways to accomplish this as well:
- In RStudio, Session in the File Menu provides 3 options for setting your working directory:
- To Source File location (the directory where you save your R script and program files). If you try this when you first open RStudio you will get a message that says: “The currently active source file is not saved so doesn’t have a directory to change into.” In other words you haven’t opened any files yet, so R has NO idea where it is working from. This option works only after you have opened a file.
- To Files Pane Location – in the Files Pane – navigate to the location you want to have as your Working Directory. Once you have it selected in the Files Pane, then choose Session -> Set Working Directory -> Files Pane location. You will see the new working directory appear in your console and it should match what you select in the Files Pane.
- Choose Directory – will open a windows dialogue box where you navigate and select the directory of choice. This option is probably the best option once you have opened RStudio and have not opened a file.
- While you are in the Files Pane location – navigate to the directory that you would like to set as your working directory, then in the Files Pane – select More -> Set Working Directory. This option is very similar to the Files Pane Location option under the Session menu of RStudio.
As mentioned in the first section of this workshop, R is made up of a number of packages. Remember that a package is a collection of functions, data, and documentation on a specific topic or analysis. There are 2 types of packages: standard packages – those that came with your Base R OR a package that you downloaded from CRAN or elsewhere.
To view a complete list of R packages available on CRAN, please visit https://cran.r-project.org/web/packages/available_packages_by_name.html
Once you download a package of interest, you need to install it and load it before the functions within are available to you in the R environment.
There are a couple of ways, that I am aware of – at the moment, of downloading and installing new packages.
RStudio file menu – Tools – Install Package
Notice that the default is to search for the package on the CRAN website
If you are using this method – please ensure that the Install dependencies box is selected.
Typing a command in the Editor window
You will notice that with either method that in you RStudio Console – there will be a series of operations that occur during the installation.
Once you have the package installed, you will still need to load the package in order to let R know that you are ready to use the functions within the package. Without loading the package, R will not be aware of the package or of the functions you may be trying to use within a package. To load a package please type:
As we work through the workshop, I will try to have the packages we are using listed at the top of each R script we will be using. I will ask you to install the package and then load it to make it available for your session.
For anyone who relies more on their keyboard than their mouse, here are a few keyboard shortcuts that may be helpful.
Keyboard Shortcut Function
Ctrl-Enter Submit code
Ctrl-1 Move to Source window
Ctrl-2 Move to Console window
Ctrl-L Clear Console window
Alt – <-
Let’s start playing with our RStudio by learning different ways of getting the data in. If you are not attending the workshop and would like a copy of the notes, please send me an email.
I’m back! and feeling energized and very excited to be starting workshops again real soon! Ok May 4 will be the first set.
I’ll be offering SAS workshops on Tuesday mornings, R workshops on Tuesday afternoons, and what I like to call Crimes of Statistics workshops on Thursday afternoons. Yes, we’ll be continuing to use the TEAMS 0nline platform, and I encourage everyone who joins me to get involved in the workshops!
Where are the workshops? Too many to list here – so please visit the registration sites:
Tuesday workshops –https://oacstats_workshops.youcanbook.me/
Thursday workshops – https://oacstatstutorials.youcanbook.me/
Oh I did say something about a new website?? Well… it’s a Sharepoint site – it was fun to play around with it. What does this mean really? It’s a landing site for all of our workshops. I like to think of it as a one-stop-shop. Ok, I’m really hoping that it works out to be the one-stop-shop. If you drop by, let me know please!! Oops – would you like the link too https://uoguelphca.sharepoint.com/sites/OACStats_Workshops Now you can go check it out 🙂
Well that’s all the news for today – I’m aiming to keep this site a bit more moving along with my SASsyFridays blog – so stay tuned.
Take care everyone and get ready for that snow!!