{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Data Manipulation\n", "\n", "In this section, we will cover three main topics\n", "\n", "1. Reshaping data frome wide (fat) to long (tall) formats\n", "2. Reshaping data from long (tall) to wide (fat) formats\n", "3. Merging datasets\n", "\n", "To reshape datasets, we will cover two methods\n", "\n", "* PROC TRANSPOSE\n", "* Using a DATA step with arrays\n", "\n", "In order to understand the second more general method, we will first need to learn about a few SAS programming keywords and structures, such as \n", "\n", "* The OUTPUT and RETAIN statements\n", "* Loops in SAS\n", "* SAS Arrays\n", "* FIRST. and LAST. SAS variables\n", "\n", "## The OUTPUT and RETAIN Statements\n", "\n", "When processing any DATA step, SAS follows two default procedures:\n", "\n", "1. When SAS reads the DATA statement at the beginning of each iteration of the DATA step, SAS places missing values in the program data vector for variables that were assigned by either an INPUT statement or an assignment statement within the DATA step. (SAS does not reset variables to missing if they were created by a SUM statement, or if the values came from a SAS data set via a SET or MERGE statement.)\n", "2. At the end of the DATA step after completing an iteration of the DATA step, SAS outputs the values of the variables in the program data vector to the SAS data set being created.\n", "\n", "In this lesson, we'll learn how to modify these default processes by using the OUTPUT and RETAIN statements:\n", "\n", "* The **OUTPUT** statement allows you to control when and to which data set you want an observation written.\n", "* The **RETAIN** statement causes a variable created in the DATA step to retain its value from the current observation into the next observation rather than it being set to missing at the beginning of each iteration of the DATA step.\n", "\n", "### The OUTPUT Statement\n", "\n", "An OUTPUT statement overrides the default process by telling SAS to output the current observation when the OUTPUT statement is processed — not at the end of the DATA step. The OUTPUT statement takes the form:\n", "\n", "`OUTPUT dataset1 dataset2 ... datasetn;`;\n", "\n", "where you may name as few or as many data sets as you like. If you use an OUTPUT statement without specifying a data set name, SAS writes the current observation to each of the data sets named in the DATA step. Any data set name appearing in the OUTPUT statement must also appear in the DATA statement.\n", "\n", "The OUTPUT statement is pretty powerful in that, among other things, it gives us a way:\n", "\n", "* to write observations to multiple data sets\n", "* to control output of observations to data sets based on certain conditions\n", "* to transpose datasets using the OUTPUT statement in conjunction with the RETAIN statement, BY group processing and the LAST.variable statement\n", "\n", "Throughout the rest of this section, we'll look at examples that illustrate how to use OUTPUT statements correctly. We'll work with the following subset of the ICDB Study's log data set (see the course website for icdblog.sas7bdat):" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SAS Connection established. Subprocess id is 2262\n", "\n" ] }, { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

The SAS System

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
ObsSUBJV_TYPEV_DATEFORM
12100061205/06/94cmed
22100061205/06/94diet
32100061205/06/94med
42100061205/06/94phytrt
52100061205/06/94purg
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LIBNAME PHC6089 \"/folders/myfolders/SAS_Notes/data\";\n", "\n", "PROC PRINT data = phc6089.icdblog (obs=5);\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, this log data set contains four variables:\n", "\n", "* `subj`: the subject's identification number\n", "* `v_type`: the type of clinic visit, which means the number of months since the subject was first seen in the clinic\n", "* `v_date`: the date of the clinic visit\n", "* `form`: codes that indicate the data forms that were completed during the subject's clinic visit\n", "\n", "The log data set is a rather typical data set that arises from large national clinical studies in which there are a number of sites around the country where data are collected. Typically, the clinical sites collect the data on data forms and then \"ship\" the data forms either electronically or by mail to a centralized location called a Data Coordinating Center (DCC). As you can well imagine, keeping track of the data forms at the DCC is a monumental task. For the ICDB Study, for example, the DCC received more than 68,000 data forms over the course of the study.\n", "\n", "In order to keep track of the data forms that arrive at the DCC, they are \"logged\" into a data base and subsequently tracked as they are processed at the DCC. In reality, a log data base will contain many more variables than we have in our subset, such as dates the data on the forms were entered into the data base, who entered the data, the dates the entered data were verified, who verified the data, and so on. To keep our life simple, we'll just use the four variables described above.\n", "\n", "
\n", "

Example

\n", "

This example uses the OUTPUT statement to tell SAS to write observations to data sets based on certain conditions. Specifically, the following program uses the OUTPUT statement to create three SAS data sets — s210006, s310032, and s410010 — based on whether the subject identification numbers in the icdblog data set meet a certain condition:

\n", "
" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

The s210006 data set

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
SUBJV_TYPEV_DATEFORM
2100061205/06/94cmed
2100061205/06/94diet
2100061205/06/94med
2100061205/06/94phytrt
2100061205/06/94purg
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "

The s310032 data set

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
SUBJV_TYPEV_DATEFORM
3100322409/19/95backf
3100322409/19/95cmed
3100322409/19/95diet
3100322409/19/95med
3100322409/19/95medhxf
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "

The s410010 data set

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
SUBJV_TYPEV_DATEFORM
410010605/12/94cmed
410010605/12/94diet
410010605/12/94med
410010605/12/94phytrt
410010605/12/94purg
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LIBNAME PHC6089 \"/folders/myfolders/SAS_Notes/data\";\n", "\n", "DATA s210006 s310032 s410010;\n", " set phc6089.icdblog;\n", " if (subj = 210006) then output s210006;\n", " else if (subj = 310032) then output s310032;\n", " else if (subj = 410010) then output s410010;\n", "RUN;\n", " \n", "PROC PRINT data = s210006 (obs=5) NOOBS;\n", " title 'The s210006 data set';\n", "RUN;\n", " \n", "PROC PRINT data = s310032 (obs=5) NOOBS;\n", " title 'The s310032 data set';\n", "RUN;\n", " \n", "PROC PRINT data = s410010 (obs=5) NOOBS;\n", " title 'The s410010 data set';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

As you can see, the DATA statement contains three data set names — s210006, s310032, and s410010. That tells SAS that we want to create three data sets with the given names. The SET statement, of course, tells SAS to read observations from the permanent data set called stat481.icdblog. Then comes the IF-THEN-ELSE and OUTPUT statements that make it all work. The first IF-THEN tells SAS to output any observations pertaining to subject 210006 to the s210006 data set; the second IF-THEN tells SAS to output any observations pertaining to subject 310032 to the s310032 data set; and, the third IF-THEN statement tells SAS to output any observations pertaining to subject 410010 to the s410010 data set. SAS will hiccup if you have a data set name that appears in an OUTPUT statement without it also appearing in the DATA statement.

\n", "

The PRINT procedures, of course, tell SAS to print the three newly created data sets. Note that the last PRINT procedure does not have a DATA= option. That's because when you name more than one data set in a single DATA statement, the last name on the DATA statement is the most recently created data set, and the one that subsequent procedures use by default. Therefore, the last PRINT procedure will print the s410010 data set by default.

\n", "

Note that the IF-THEN-ELSE construct used here in conjunction with the OUTPUT statement is comparable to attaching the WHERE= option to each of the data sets appearing in the DATA statement.

\n", "

Before running the code be sure that you have saved the icdblog dataset and changed the LIBNAME statement to the folder where you saved it.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Example

\n", "

Using an OUTPUT statement suppresses the automatic output of observations at the end of the DATA step. Therefore, if you plan to use any OUTPUT statements in a DATA step, you must use OUTPUT statements to program all of the output for that step. The following SAS program illustrates what happens if you fail to direct all of the observations to output:

\n", "
" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

The subj210006 data set

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
SUBJV_TYPEV_DATEFORM
2100061205/06/94cmed
2100061205/06/94diet
2100061205/06/94med
2100061205/06/94phytrt
2100061205/06/94purg
2100061205/06/94qul
2100061205/06/94sympts
2100061205/06/94urn
2100061205/06/94void
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA subj210006 subj310032;\n", " set phc6089.icdblog;\n", " if (subj = 210006) then output subj210006;\n", "RUN;\n", " \n", "PROC PRINT data = subj210006 NOOBS;\n", " title 'The subj210006 data set';\n", "RUN;" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "

\n", "\n", "
198  ods listing close;ods html5 (id=saspy_internal) file=stdout options(bitmap_mode='inline') device=svg style=HTMLBlue; ods
198! graphics on / outputfmt=png;
NOTE: Writing HTML5(SASPY_INTERNAL) Body file: STDOUT
199
200 PROC PRINT data = subj310032 NOOBS;
201 title 'The subj310032 data set';
202 RUN;
NOTE: No observations in data set WORK.SUBJ310032.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

203
204 ods html5 (id=saspy_internal) close;ods listing;

205
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "PROC PRINT data = subj310032 NOOBS;\n", " title 'The subj310032 data set';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

The DATA statement contains two data set names, subj210006 and subj310032, telling SAS that we intend to create two data sets. However, as you can see, the IF statement contains an OUTPUT statement that directs output to the subj210006 data set, but no OUTPUT statement directs output to the subj310032 data set. Launch and run the SAS program to convince yourself that the subj210006 data set contains data for subject 210006, while the subj310032 data set contains 0 observations. You should see a message in the log window like the one shown above as well as see that no output for the subj310032 data set appears in the output window.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Example

\n", "

If you use an assignment statement to create a new variable in a DATA step in the presence of OUTPUT statements, you have to make sure that you place the assignment statement before the OUTPUT statements. Otherwise, SAS will have already written the observation to the SAS data set, and the newly created variable will be set to missing. The following SAS program illustrates an example of how two variables, current and days_vis, get set to missing in the output data sets because their values get calculated after SAS has already written the observation to the SAS data set:

\n", "
" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

The subj310032 data set

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
SUBJV_TYPEV_DATEFORMcurrentdays_vis
3100322409/19/95backf..
3100322409/19/95cmed..
3100322409/19/95diet..
3100322409/19/95med..
3100322409/19/95medhxf..
3100322409/19/95phs..
3100322409/19/95phytrt..
3100322409/19/95preg..
3100322409/19/95purg..
3100322409/19/95qul..
3100322409/19/95sympts..
3100322409/19/95urn..
3100322409/19/95void..
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA subj210006 subj310032 subj410010;\n", " set phc6089.icdblog;\n", " if (subj = 210006) then output subj210006;\n", " else if (subj = 310032) then output subj310032;\n", " else if (subj = 410010) then output subj410010;\n", " current = today();\n", " days_vis = current - v_date;\n", " format current mmddyy8.;\n", "RUN;\n", " \n", "PROC PRINT data = subj310032 NOOBS;\n", " title 'The subj310032 data set';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

The main thing to note in this program is that the current and days_vis assignment statements appear after the IF-THEN-ELSE and OUTPUT statements. That means that each observation will be written to one of the three output data sets before the current and days_vis values are even calculated. Because SAS sets variables created in the DATA step to missing at the beginning of each iteration of the DATA step, the values of current and days_vis will remain missing for each observation.

\n", "

By the way, the today( ) function, which is assigned to the variable current, creates a date variable containing today's date. Therefore, the variable days_vis is meant to contain the number of days since the subject's recorded visit v_date. However, as described above, the values of current and days_vis get set to missing. Launch and run the SAS program to convince yourself that the current and days_vis variables in the subj310032 data set contain only missing values. If we were to print the subj210006 and subj410020 data sets, we would see the same thing.

\n", "

The following SAS program illustrates the corrected code for the previous DATA step, that is, for creating new variables with assignment statements in the presence of OUTPUT statements:

\n", "
" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

The subj310032 data set

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
SUBJV_TYPEV_DATEFORMcurrentdays_vis
3100322409/19/95backf09/30/209143
3100322409/19/95cmed09/30/209143
3100322409/19/95diet09/30/209143
3100322409/19/95med09/30/209143
3100322409/19/95medhxf09/30/209143
3100322409/19/95phs09/30/209143
3100322409/19/95phytrt09/30/209143
3100322409/19/95preg09/30/209143
3100322409/19/95purg09/30/209143
3100322409/19/95qul09/30/209143
3100322409/19/95sympts09/30/209143
3100322409/19/95urn09/30/209143
3100322409/19/95void09/30/209143
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA subj210006 subj310032 subj410010;\n", " set phc6089.icdblog;\n", " current = today();\n", " days_vis = current - v_date;\n", " format current mmddyy8.;\n", " if (subj = 210006) then output subj210006;\n", " else if (subj = 310032) then output subj310032;\n", " else if (subj = 410010) then output subj410010;\n", "RUN;\n", " \n", "PROC PRINT data = subj310032 NOOBS;\n", " title 'The subj310032 data set';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Now, since the assignment statements precede the OUTPUT statements, the variables are correctly written to the output data sets. That is, now the variable current contains the date in which the program was run and the variable days_vis contains the number of days since that date and the date of the subject's visit. Launch and run the SAS program to convince yourself that the current and days_vis variables are properly written to the subj310032 data set. If we were to print the subj210006 and subj410020 data sets, we would see similar results.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Example

\n", "

After SAS processes an OUTPUT statement within a DATA step, the observation remains in the program data vector and you can continue programming with it. You can even output the observation again to the same SAS data set or to a different one! The following SAS program illustrates how you can create different data sets with the some of the same observations. That is, the data sets created in your DATA statement do not have to be mutually exclusive:

\n", "
" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

The symptoms data set

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
SUBJV_TYPEV_DATEFORM
2100061205/06/94sympts
3100322409/19/95sympts
410010605/12/94sympts
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "

The visitsix data set

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
SUBJV_TYPEV_DATEFORM
410010605/12/94cmed
410010605/12/94diet
410010605/12/94med
410010605/12/94phytrt
410010605/12/94purg
410010605/12/94qul
410010605/12/94sympts
410010605/12/94urn
410010605/12/94void
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA symptoms visitsix;\n", " set phc6089.icdblog;\n", " if form = 'sympts' then output symptoms;\n", " if v_type = 6 then output visitsix;\n", "RUN;\n", " \n", "PROC PRINT data = symptoms NOOBS;\n", " title 'The symptoms data set';\n", "RUN;\n", " \n", "PROC PRINT data = visitsix NOOBS;\n", " title 'The visitsix data set';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

The DATA step creates two temporary data sets, symptoms and visitsix. The symptoms data set contains only those observations containing a form code of sympts. The visitsix data set, on the other hand, contains observations for which v_type equals 6. The observations in the two data sets are therefore not necessarily mutually exclusive. In fact, launch and run the SAS program and review the output from the PRINT procedures. Note that the observation for subject 410010 in which form = sympts is contained in both the symptoms and visitsix data sets.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The RETAIN Statement\n", "\n", "When SAS reads the DATA statement at the beginning of each iteration of the DATA step, SAS places missing values in the program data vector for variables that were assigned by either an INPUT statement or an assignment statement within the DATA step. A RETAIN statement effectively overrides this default. That is, a RETAIN statement tells SAS not to set variables whose values are assigned by an INPUT or assignment statement to missing when going from the current iteration of the DATA step to the next. Instead, SAS retains the values. The RETAIN statement takes the generic form:\n", "\n", "`RETAIN variable1 variable2 ... variablen;`\n", "\n", "You can specify as few or as many variables as you want. If you specify no variable names, then SAS retains the values of all of the variables created in an INPUT or assignment statement. You may initialize the values of variables within a RETAIN statement. For example, in the statement:\n", "\n", "`RETAIN var1 0 var2 3 a b c 'XYZ'`\n", "\n", "the variable var1 is assigned the value 0; the variable var2 is assigned the value 3, and the variables a, b, and c are all assigned the character value 'XYZ'. If you do not specify an initial value, SAS sets the initial value of a variable to be retained to missing.\n", "\n", "Finally, since the RETAIN statement is not an executable statement, it can appear anywhere in the DATA step.\n", "\n", "Throughout the remainder of the lesson, we will work with the grades data set that is created in the following DATA step:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

The grades data set

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
idnol_namegtypegrade
10SmithE178
10SmithE282
10SmithE386
10SmithE469
10SmithP197
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA grades;\n", " input idno 1-2 l_name $ 5-9 gtype $ 12-13 grade 15-17;\n", " cards;\n", "10 Smith E1 78\n", "10 Smith E2 82\n", "10 Smith E3 86\n", "10 Smith E4 69\n", "10 Smith P1 97\n", "10 Smith F1 160\n", "11 Simon E1 88\n", "11 Simon E2 72\n", "11 Simon E3 86\n", "11 Simon E4 99\n", "11 Simon P1 100\n", "11 Simon F1 170\n", "12 Jones E1 98\n", "12 Jones E2 92\n", "12 Jones E3 92\n", "12 Jones E4 99\n", "12 Jones P1 99\n", "12 Jones F1 185\n", ";\n", "RUN;\n", " \n", "PROC PRINT data = grades (obs=5) NOOBS;\n", " title 'The grades data set';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The grades data set is what we call a \"subject- and grade-specific\" data set. That is, there is one observation for each grade for each student. Students are identified by their id number (idno) and last name (l_name). The data set contains six different types of grades: exam 1 (E1), exam 2 (E2), exam 3 (E3), exam 4 (E4), each worth 100 points; one project (P1) worth 100 points; and a final exam (F1) worth 200 points. We'll suppose that the instructor agreed to drop the students' lowest exam grades (E1, E2, E3, E4) not including the final exam. Launch and run the SAS program so that we can work with the grades data set in the following examples. Review the output from the PRINT procedure to convince yourself that the data were properly read into the grades data set.\n", "\n", "Before we look at an example using the RETAIN statement, let's look at the SAS variables FIRST. and LAST." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Example

\n", "

The following SAS program illustrates the SAS variables FIRST. and LAST. that can be obtained when using the BY statement on a sorted dataset in a DATA step to identify the first and last grade record for each student in the dataset.

\n", "
" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

The grades data set

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsidnol_namegtypegradefirstGradelastGrade
110SmithE17810
210SmithE28200
310SmithE38600
410SmithE46900
510SmithP19700
610SmithF116001
711SimonE18810
811SimonE27200
911SimonE38600
1011SimonE49900
1111SimonP110000
1211SimonF117001
1312JonesE19810
1412JonesE29200
1512JonesE39200
1612JonesE49900
1712JonesP19900
1812JonesF118501
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "PROC SORT data = grades out = srt_grades;\n", " BY idno;\n", "RUN;\n", "\n", "DATA grades_first_last;\n", " SET srt_grades;\n", " BY idno;\n", " firstGrade = FIRST.idno;\n", " lastGrade = LAST.idno;\n", "RUN;\n", "\n", "PROC PRINT data = grades_first_last;\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Because we are doing BY group processing on the variable idno, we must have the dataset sorted by idno. In this case the dataset was actually already sorted by idno, but I added the PROC SORT anyway to emphasize that the dataset must be sorted first.

\n", "

The SET and BY statement tell SAS to process the data by grouping observations with the same idno together. To do this, SAS automatically creats two temporary variables for each variable name in the BY statement. One of the temporary variables is called FIRST.variable, where variable is the variable name appearing the BY statement. The other temporary variable is called LAST.variable. Both take the values 0 or 1:

\n", "
    \n", "
  • FIRST.variable = 1 when an observation is the first observation in a BY group
  • \n", "
  • FIRST.variable = 0 when an observation is not the first observation in a BY group
  • \n", "
  • LAST.variable = 1 when an observation is the last observation in a BY group
  • \n", "
  • LAST.variable = 0 when an observation is not the last observation in a BY group
  • \n", "
\n", "

SAS uses the values of the FIRST.variable and LAST.variable temporary variables to identify the first and last observations in a group, and therefore the group itself. Oh, a comment about that adjective temporary ... SAS places FIRST.variable and LAST.variable in the program data vector and they are therefore available for DATA step programming, but SAS does not add them to the SAS data set being created. It is in that sense that they are temporary.

\n", "

Because SAS does not write FIRST.variables and LAST.variables to output data sets, we have to do some finagling to see their contents. The two assignment statements:

\n", "
\n",
    "    firstGrade = FIRST.idno;\n",
    "    lastGrade = LAST.idno;\n",
    "    
\n", "

simply tell SAS to assign the values of the temporary variables, FIRST.idno and LAST.idno, to permanent variables, firstGrade and lastGrade, respectively. The PRINT procedure tells SAS to print the resulting data set so that we can take an inside peek at the values of the FIRST.variables and LAST.variables.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Example

\n", "

One of the most powerful uses of a RETAIN statement is to compare values across observations. The following program uses the RETAIN statement to compare values across observations, and in doing so determines each student's lowest grade of the four semester exams:

\n", "
" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Output Dataset: LOWEST

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsidnol_namegradelowgradegtype
110Smith6969E4
211Simon9972E2
312Jones9992E3
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA exams;\n", " set grades (where = (gtype in ('E1', 'E2', 'E3', 'E4')));\n", "RUN;\n", " \n", "DATA lowest (rename = (lowtype = gtype));\n", " set exams;\n", " by idno;\n", " retain lowgrade lowtype;\n", " if first.idno then lowgrade = grade;\n", " lowgrade = min(lowgrade, grade);\n", " if grade = lowgrade then lowtype = gtype;\n", " if last.idno then output;\n", " drop gtype;\n", "RUN;\n", " \n", "PROC PRINT data=lowest;\n", " title 'Output Dataset: LOWEST';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Because the instructor only wants to drop the lowest exam grade, the first DATA step tells SAS to create a data set called exams by selecting only the exam grades (E1, E2, E3, and E4) from the data set grades.

\n", "

It's the second DATA step that is the meat of the program and the challenging one to understand. The DATA step searches through the exams data set for each subject (\"by idno\") and looks for the lowest grade (\"min(lowgrade, grade)\"). Because SAS would otherwise set the variables lowgrade and lowtype to missing for each new iteration, the RETAIN statement is used to keep track of the observation that contains the lowest grade. When SAS reads the last observation of the student (\"last.idno\") it outputs the data corresponding to the lowest exam type (lowtype) and grade (lowgrade) to the lowest data set. (Note that the statement \"if last.idno then output;\" effectively collapses multiple observations per student into one observation per student.) So that we can merge the lowest data set back into the grades data set, by idno and gtype, the variable lowtype is renamed back to gtype.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## DO Loops\n", "\n", "When programming, you can find yourself needing to tell SAS to execute the same statements over and over again. That's when a DO loop can come in and save your day. The actions of some DO loops are unconditional in that if you tell SAS to do something 20 times, SAS will do it 20 times regardless. We call those kinds of loops **iterative DO loops**. On the other hand, actions of some DO loops are conditional in that you tell SAS to do something until a particular condition is met or to do something while a particular condition is met. We call the former a **DO UNTIL** loop and the latter a **DO WHILE** loop. In this lesson, we'll explore the ins and outs of these three different kinds of loops, as well as take a look at lots of examples in which they are used. Then, in the next section, we'll use DO loops to help us process arrays.\n", "\n", "### Iterative DO Loops\n", "\n", "In this section, we'll explore the use of iterative DO loops, in which you tell SAS to execute a statement or a group of statements a certain number of times. Let's take a look at some examples.\n", "\n", "
\n", "

Example

\n", "

The following program uses a DO loop to tell SAS to determine what four times three (4 × 3) equals:

\n", "
" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Four Times Three Equals...

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
answeri
125
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA multiply;\n", " answer = 0;\n", " do i = 1 to 4;\n", " answer + 3;\n", " end;\n", "RUN;\n", " \n", "PROC PRINT NOOBS;\n", " title 'Four Times Three Equals...';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Okay... admittedly, we could accomplish our goal of determining four times three in a much simpler way, but then we wouldn't have the pleasure of seeing how we can accomplish it using an iterative DO loop! The key to understanding the DATA step here is to recall that multiplication is just repeated addition. That is, four times three (4 × 3) is the same as adding three together four times, that is, 3 + 3 + 3 + 3. That's all that the iterative DO loop in the DATA step is telling SAS to do. After having initialized answer to 0, add 3 to answer, then add 3 to answer again, and add 3 to answer again, and add 3 to answer again. After SAS has added 3 to the answer variable four times, SAS exits the DO loop, and since that's the end of the DATA step, SAS moves onto the next procedure and prints the result.

\n", "

The other thing you might want to notice about the DATA step is that there is no input data set or input data file. We are generating data from scratch here, rather than from some input source. Now, launch and run the SAS program, and review the output from the PRINT procedure to convince yourself that our code properly calculates four times three.

\n", "

Ahhh, what about that i variable that shows up in our multiply data set? If you look at our DATA step again, you can see that it comes from the DO loop. It is what is called the index variable (or counter variable). Most often, you'll want to drop it from your output data set, but its presence here is educational. As you can see, its current value is 5. That's what allows SAS to exit the DO loop... we tell SAS only to take the actions inside the loop until i equals 4. Once i becomes greater than 4, SAS jumps out of the loop, and moves on to the next statements in the DATA step. Let's take a look at the general form of iterative DO loops.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To construct an iterative DO loop, you need to start with a DO statement, then include some action statements, and then end with an END statement. Here's what a simple iterative DO loop should look like:\n", "\n", "
\n",
    "DO index-variable = start TO stop BY increment;\n",
    "    action statements;\n",
    "END;\n",
    "
\n", "\n", "where\n", "\n", "* DO, index-variable, start, TO, stop, and END are required in every iterative DO loop\n", "* index-variable, which stores the value of the current iteration of the DO loop, can be any valid SAS variable name. It is common, however, to use a single letter, with i and j being the most used.\n", "* start is the value of the index variable at which you want SAS to start the loop\n", "* stop is the value of the index variable at which you want SAS to stop the loop\n", "* increment is by how much you want SAS to change the index variable after each iteration. The most commonly used increment is 1. In fact, if you don't specify a BY clause, SAS uses the default increment of 1.\n", "\n", "For example,\n", "\n", "`do jack = 1 to 5;`\n", "\n", "tells SAS to create an index variable called jack, start at 1, increment by 1, and end at 5, so that the values of jack from iteration to iteration are 1, 2, 3, 4, and 5. And, this DO statement:\n", "\n", "`do jill = 2 to 12 by 2;`\n", "\n", "tells SAS to create an index variable called jill, start at 2, increment by 2, and end at 12, so that the values of jill from iteration to iteration are 2, 4, 6, 8, 10, and 12.\n", "\n", "
\n", "

Example

\n", "

The following SAS program uses an iterative DO loop to count backwards by 1:

\n", "
" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Counting Backwards by 1

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
i
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA backwardsbyone;\n", " do i = 20 to 1 by -1;\n", " output;\n", " end;\n", "RUN;\n", " \n", "PROC PRINT data = backwardsbyone NOOBS;\n", " title 'Counting Backwards by 1';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

As you can see in this DO statement, you can decrement a DO loop's index variable by specifying a negative value for the BY clause. Here, we tell SAS to start at 20, and decrease the index variable by 1, until it reaches 1. The OUTPUT statement tells SAS to output the value of the index variable i for each iteration of the DO loop. Launch and run the SAS program, and review the output from the PRINT procedure to convince yourself that our code properly counts backwards from 20 to 1.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Rather than specifying start, stop and increment values in a DO statement, you can tell SAS how many times to execute a DO loop by listing items in a series. In this case, the general form of the iterative DO loop looks like this:\n", "\n", "
\n",
    "DO index-variable = value1, value2, value3, ...;\n",
    "    action statements;\n",
    "END;\n",
    "
\n", "\n", "where the values can be character or numeric. When the DO loop executes, it executes once for each item in the series. The index variable equals the value of the current item. You must use commas to separate items in a series. To list items in a series, you must specify\n", "\n", "either all numeric values: \n", "\n", "`DO i = 1, 2, 3, 4, 5;`\n", "\n", "all character values, with each value enclosed in quotation marks \n", "\n", "`DO j = 'winter', 'spring', 'summer', 'fall';`\n", "\n", "or all variable names: \n", "\n", "`DO k = first, second, third;`\n", "\n", "In this case, the index variable takes on the values of the specified variables. Note that the variable names are not enclosed in quotation marks, while quotation marks are required for character values.\n", "\n", "### Nested DO Loops\n", "\n", "Just like in other programming languages. We can nest loops within each other.\n", "\n", "
\n", "

Example

\n", "

Suppose you are interested in conducting an experiment with two factors A and B. Suppose factor A is, say, the amount of water with levels 1, 2, 3, and 4; and factor B is, say, the amount of sunlight, say with levels 1, 2, 3, 4, and 5. Then, the following SAS code uses nested iterative DO loops to generate the 4 by 5 factorial design:

\n", "
" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

4 by 5 Factorial Design

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsij
111
212
313
414
515
621
722
823
924
1025
1131
1232
1333
1434
1535
1641
1742
1843
1944
2045
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA design;\n", "DO i = 1 to 4;\n", " DO j = 1 to 5;\n", " output;\n", " END;\n", " END;\n", "RUN;\n", " \n", "PROC PRINT data = design;\n", " TITLE '4 by 5 Factorial Design';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

First, launch and run the SAS program. Then, review the output from the PRINT procedure to see the contents of the design data set. By doing so, you can get a good feel for how the nested DO loops work. First, SAS sets the value of the index variable i to 1, then proceeds to the next step which happens to be another iterative DO loop. While i is 1:

\n", "
    \n", "
  • SAS sets the value of j to 1, and outputs the observation in which i = 1 and j = 1.
  • \n", "
  • Then, SAS sets the value j to 2, and outputs the observation in which i = 1 and j = 2.
  • \n", "
  • Then, SAS sets the value j to 3, and outputs the observation in which i = 1 and j = 3.
  • \n", "
  • Then, SAS sets the value j to 4, and outputs the observation in which i = 1 and j = 4.
  • \n", "
  • Then, SAS sets the value j to 5, and outputs the observation in which i = 1 and j = 5.
  • \n", "
  • Then, SAS sets the value j to 6, and jumps out of the inside DO loop and proceeds to the next statement, which happens to be the end of the outside DO loop.
  • \n", "
\n", "

SAS then sets the value of the index variable i to 2, then proceeds through the inside DO loop again just as described above. This process continues until SAS sets the value of index variable i to 5, jumps out of the outside DO loop, and ends the DATA step.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### DO UNITL and DO WHILE Loops\n", "\n", "As you now know, the iterative DO loop requires that you specify the number of iterations for the DO loop. However, there are times when you want to execute a DO loop until a condition is reached or while a condition exists, but you don't know how many iterations are needed. That's when the DO UNTIL loop and the DO WHILE loop can help save the day!\n", "\n", "In this section, we'll first learn about the DO UNTIL and DO WHILE loops. Then, we'll look at another form of the iterative DO loop that combines features of both conditional and unconditional DO loops.\n", "\n", "When you use a DO UNTIL loop, SAS executes the DO loop until the expression you've specified is true. Here's the general form of a DO UNTIL loop:\n", "\n", "
\n",
    "DO UNTIL (expression);\n",
    "    action statements;\n",
    "END;\n",
    "
\n", "\n", "where expression is any valid SAS expression enclosed in parentheses. The key thing to remember is that the expression is not evaluated until the bottom of the loop. Therefore, a DO UNTIL loop always executes at least once. As soon as the expression is determined to be true, the DO loop does not execute again.\n", "\n", "
\n", "

Example

\n", "

Suppose you want to know how many years it would take to accumulate 50,000 if you deposit 1200 each year into an account that earns 5% interest. The following program uses a DO UNTIL loop to perform the calculation for us:

\n", "
" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Years until at least $50,000

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
valueyear
1260.001
2583.002
3972.153
5430.764
6962.305
8570.416
10258.937
12031.888
13893.479
15848.1410
17900.5511
20055.5812
22318.3613
24694.2814
27188.9915
29808.4416
32558.8617
35446.8018
38479.1419
41663.1020
45006.2621
48516.5722
52202.4023
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA investment;\n", " RETAIN value 0 year 0;\n", " DO UNTIL (value >= 50000);\n", " value = value + 1200;\n", " value = value + value * 0.05;\n", " year = year + 1;\n", " OUTPUT;\n", " END;\n", "RUN;\n", " \n", "PROC PRINT data = investment NOOBS;\n", " title 'Years until at least $50,000';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Recall that the expression in the DO UNTIL statement is not evaluated until the bottom of the loop. Therefore, the DO UNTIL loop executes at least once. On the first iteration, the value variable is increased by 1200, or in this case, set to 1200. Then, the value variable is updated by calculating 1200 + 1200*0.05 to get 1260. Then, the year variable is increased by 1, or in this case, set to 1. The first observation, for which year = 1 and value = 1260, is then written to the output data set called investment. Having reached the bottom of the DO UNTIL loop, the expression (value >= 50000) is evaluated to determine if it is true. Since value is just 1260, the expression is not true, and so the DO UNTIL loop is executed once again. The process continues as described until SAS determines that value is at least 50000 and therefore stops executing the DO UNTIL loop.

\n", "

Launch and run the SAS program, and review the output from the PRINT procedure to convince yourself that it would take 23 years to accumulate at least $50,000.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you use a DO WHILE loop, SAS executes the DO loop while the expression you've specified is true. Here's the general form of a DO WHILE loop:\n", "\n", "
\n",
    "DO WHILE (expression);\n",
    "      action statements;\n",
    "END;\n",
    "
\n", "\n", "where expression is any valid SAS expression enclosed in parentheses. An important difference between the DO UNTIL and DO WHILE statements is that the DO WHILE expression is evaluated at the top of the DO loop. If the expression is false the first time it is evaluated, then the DO loop doesn't even execute once.\n", "\n", "
\n", "

Example

\n", "

The following program attempts to use a DO WHILE loop to accomplish the same goal as the program above, namely to determine how many years it would take to accumulate \\$50,000 if you deposit \\$1200 each year into an account that earns 5% interest:

\n", "
" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Years until at least $50,000

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
valueyear
1260.001
2583.002
3972.153
5430.764
6962.305
8570.416
10258.937
12031.888
13893.479
15848.1410
17900.5511
20055.5812
22318.3613
24694.2814
27188.9915
29808.4416
32558.8617
35446.8018
38479.1419
41663.1020
45006.2621
48516.5722
52202.4023
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA investtwo;\n", " RETAIN value 0 year 0;\n", " DO WHILE (value < 50000);\n", " value = value + 1200;\n", " value = value + value * 0.05;\n", " year = year + 1;\n", " OUTPUT;\n", " END;\n", "RUN;\n", " \n", "PROC PRINT data = investtwo NOOBS;\n", " title 'Years until at least $50,000';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

The calculations proceed as before. First, the value variable is updated to by calculating 0 + 1200, to get 1200. Then, the value variable is updated by calculating 1200 + 1200*0.05 to get 1260. Then, the year variable is increased by 1, or in this case, set to 1. The first observation, for which year = 1 and value = 1260, is then written to the output data set called investthree. SAS then returns to the top of the DO WHILE loop, to determine if the expression (value < 50000) is true. Since value is just 1260, the expression is true, and so the DO WHILE loop executes once again. The process continues as described until SAS determines that value is as least 50000 and therefore stops executing the DO WHILE loop.

\n", "

Launch and run the SAS program, and review the output from the PRINT procedure to convince yourself that this program also determines that it would take 23 years to accumulate at least \\$50,000.

\n", "

You should also try changing the WHILE condition from value < 50000 to value ≥ 50000 to see what happens. (Hint: you will get no output. Why?)

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You have now seen how the DO WHILE and DO UNTIL loops enable you to execute statements repeatedly, but conditionally so. You have also seen how the iterative DO loop enables you to execute statements a set number of times unconditionally. Now, we'll put the two together to create a form of the iterative DO loop that executes DO loops conditionally as well as unconditionally.\n", "\n", "
\n", "

Example

\n", "

Suppose again that you want to know how many years it would take to accumulate 50,000 if you deposit 1200 each year into an account that earns 5% interest. But this time, suppose you also want to limit the number of years that you invest to 15 years. The following program uses a conditional iterative DO loop to accumulate our investment until we reach 15 years or until the value of our investment exceeds 50000, whichever comes first:

\n", "
" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Value of Investment

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
valueyear
1260.001
2583.002
3972.153
5430.764
6962.305
8570.416
10258.937
12031.888
13893.479
15848.1410
17900.5511
20055.5812
22318.3613
24694.2814
27188.9915
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA investfour (drop = i);\n", " RETAIN value 0 year 0;\n", " DO i = 1 to 15 UNTIL (value >= 50000);\n", " value = value + 1200;\n", " value = value + value * 0.05;\n", " year = year + 1;\n", " OUTPUT;\n", " END;\n", "RUN;\n", " \n", "PROC PRINT data = investfour NOOBS;\n", " title 'Value of Investment';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Note that there are just two differences between this program and that of the program in the previous example that uses the DO UNTIL loop: i) The iteration i = 1 to 15 has been inserted into the DO UNTIL statement; and ii) because the index variable i is created for the DO loop, it is dropped before writing the contents from the program data vector to the output data set investfour.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## SAS Arrays\n", "\n", "In this section, we'll learn about basic array processing in SAS. In DATA step programming, you often need to perform the same action on more than one variable at a time. Although you can process the variables individually, it is typically easier to handle the variables as a group. Arrays offer you that option. For example, until now, if you wanted to take the square root of the 50 numeric variables in your SAS data set, you'd have to write 50 SAS assignment statements to accomplish the task. Instead, you can use an array to simplify your task.\n", "\n", "Arrays can be used to simplify your code when you need to:\n", "\n", "* perform repetitive calculations\n", "* create many variables that have the same attributes\n", "* read data\n", "* transpose \"fat\" data sets to \"tall\" data sets, that is, change the variables in a data set to observations\n", "* transpose \"tall\" data sets to \"fat\" data sets, that is, change the observations in a data set to variables\n", "* compare variables\n", "\n", "In this lesson, we'll learn how to accomplish such tasks using arrays. Using arrays in appropriate situations can seriously simplify and shorten your SAS programs!\n", "\n", "### One-Dimensional Arrays\n", "\n", "A SAS **array** is a temporary grouping of SAS variables under a single name. For example, suppose you have four variables named winter, spring, summer, and, fall. Rather than referring to the variables by their four different names, you could associate the variables with an array name, say seasons, and refer to the variables as seasons(1), seasons(2), seasons(3), and seasons(4). When you pair an array up with an iterative DO loop, you create a powerful and efficient way of writing your computer programs. Let's take a look at an example!\n", "\n", "
\n", "

Example

\n", "

The following program simply reads in the average montly temperatures (in Celsius) for ten different cities in the United States into a temporary SAS data set called avgcelsius:

\n", "
" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Average Monthly Temperatures in Celsius

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Cityjanfebmaraprmayjunjulaugsepoctnovdec
State College, PA-2-2281419212016104-1
Miami, FL202022232627282827262320
St. Louis, MO-1161318232625211571
New Orleans, LA111316202327272726211612
Madison, WI-8-5071419222016102-5
Houston, TX101216202327282826211612
Phoenix, AZ121416212631333230231612
Seattle, WA5671013161818161286
San Francisco, CA101212131415151617161411
San Diego, CA131415161719212221191614
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA avgcelsius;\n", " input City $ 1-18 jan feb mar apr may jun\n", " jul aug sep oct nov dec;\n", " DATALINES;\n", "State College, PA -2 -2 2 8 14 19 21 20 16 10 4 -1\n", "Miami, FL 20 20 22 23 26 27 28 28 27 26 23 20\n", "St. Louis, MO -1 1 6 13 18 23 26 25 21 15 7 1\n", "New Orleans, LA 11 13 16 20 23 27 27 27 26 21 16 12\n", "Madison, WI -8 -5 0 7 14 19 22 20 16 10 2 -5\n", "Houston, TX 10 12 16 20 23 27 28 28 26 21 16 12\n", "Phoenix, AZ 12 14 16 21 26 31 33 32 30 23 16 12\n", "Seattle, WA 5 6 7 10 13 16 18 18 16 12 8 6\n", "San Francisco, CA 10 12 12 13 14 15 15 16 17 16 14 11\n", "San Diego, CA 13 14 15 16 17 19 21 22 21 19 16 14\n", ";\n", "RUN;\n", " \n", "PROC PRINT data = avgcelsius;\n", " title 'Average Monthly Temperatures in Celsius';\n", " id City;\n", " var jan feb mar apr may jun \n", " jul aug sep oct nov dec;\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Now, suppose that we don't feel particularly comfortable with understanding Celsius temperatures, and therefore, we want to convert the Celsius temperatures into Fahrenheit temperatures for which we have a better feel. The following SAS program uses the standard conversion formula:

\n", "
Fahrenheit temperature = 1.8*Celsius temperature + 32
\n", "

to convert the Celsius temperatures in the avgcelsius data set to Fahrenheit temperatures stored in a new data set called avgfahrenheit:

\n", "
" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Average Monthly Temperatures in Fahrenheit

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Cityjanffebfmarfaprfmayfjunfjulfaugfsepfoctfnovfdecf
State College, PA28.428.435.646.457.266.269.868.060.850.039.230.2
Miami, FL68.068.071.673.478.880.682.482.480.678.873.468.0
St. Louis, MO30.233.842.855.464.473.478.877.069.859.044.633.8
New Orleans, LA51.855.460.868.073.480.680.680.678.869.860.853.6
Madison, WI17.623.032.044.657.266.271.668.060.850.035.623.0
Houston, TX50.053.660.868.073.480.682.482.478.869.860.853.6
Phoenix, AZ53.657.260.869.878.887.891.489.686.073.460.853.6
Seattle, WA41.042.844.650.055.460.864.464.460.853.646.442.8
San Francisco, CA50.053.653.655.457.259.059.060.862.660.857.251.8
San Diego, CA55.457.259.060.862.666.269.871.669.866.260.857.2
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA avgfahrenheit;\n", " set avgcelsius;\n", " janf = 1.8*jan + 32;\n", " febf = 1.8*feb + 32;\n", " marf = 1.8*mar + 32;\n", " aprf = 1.8*apr + 32;\n", " mayf = 1.8*may + 32;\n", " junf = 1.8*jun + 32;\n", " julf = 1.8*jul + 32;\n", " augf = 1.8*aug + 32;\n", " sepf = 1.8*sep + 32;\n", " octf = 1.8*oct + 32;\n", " novf = 1.8*nov + 32;\n", " decf = 1.8*dec + 32;\n", " drop jan feb mar apr may jun\n", " jul aug sep oct nov dec;\n", "RUN;\n", " \n", "PROC PRINT data = avgfahrenheit;\n", " title 'Average Monthly Temperatures in Fahrenheit';\n", " id City;\n", " var janf febf marf aprf mayf junf \n", " julf augf sepf octf novf decf;\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

As you can see by the number of assignment statements necessary to make the conversions, the exercise becomes one of patience. Because there are twelve average monthly temperatures, we must write twelve assignment statements. Each assignment statement performs the same calculation. Only the name of the variable changes in each statement. Launch and run the SAS program, and review the output from the PRINT procedure to convince yourself that the Celsius temperatures were properly converted to Fahrenheit temperatures.

\n", "

The above program is crying out for the use of an array. One of the primary arguments for using an array is to reduce the number of statements that are required for processing variables. Let's take a look at an example.

\n", "

The following program uses a one-dimensional array called fahr to convert the average Celsius temperatures in the avgcelsius data set to average Fahrenheit temperatures stored in a new data set called avgfahrenheit:

\n", "
" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Average Monthly Temperatures in Fahrenheit

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Cityjanfebmaraprmayjunjulaugsepoctnovdec
State College, PA28.428.435.646.457.266.269.868.060.850.039.230.2
Miami, FL68.068.071.673.478.880.682.482.480.678.873.468.0
St. Louis, MO30.233.842.855.464.473.478.877.069.859.044.633.8
New Orleans, LA51.855.460.868.073.480.680.680.678.869.860.853.6
Madison, WI17.623.032.044.657.266.271.668.060.850.035.623.0
Houston, TX50.053.660.868.073.480.682.482.478.869.860.853.6
Phoenix, AZ53.657.260.869.878.887.891.489.686.073.460.853.6
Seattle, WA41.042.844.650.055.460.864.464.460.853.646.442.8
San Francisco, CA50.053.653.655.457.259.059.060.862.660.857.251.8
San Diego, CA55.457.259.060.862.666.269.871.669.866.260.857.2
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA avgfahrenheit;\n", " set avgcelsius;\n", " array fahr(12) jan feb mar apr may jun\n", " jul aug sep oct nov dec;\n", " do i = 1 to 12;\n", " fahr(i) = 1.8*fahr(i) + 32;\n", " end;\n", "RUN;\n", " \n", "PROC PRINT data = avgfahrenheit;\n", " title 'Average Monthly Temperatures in Fahrenheit';\n", " id City;\n", " var jan feb mar apr may jun \n", " jul aug sep oct nov dec;\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

If you compare this program with the previous program, you can see the statements that replaced the twelve assignment statements. The ARRAY statement defines an array called fahr. It tells SAS that you want to group the twelve month variables, jan , feb, ... dec, into an array called fahr. The (12) that appears in parentheses is a required part of the array declaration. Called the dimension of the array, it tells SAS how many elements, that is, variables, you want to group together. When specifying the variable names to be grouped in the array, we simply list the elements, separating each element with a space. As with all SAS statements, the ARRAY statement is closed with a semicolon (;).

\n", "

Once we've defined the array fahr, we can use it in our code instead of the individual variable names. We refer to the individual elements of the array using its name and an index, such as, fahr(i). The order in which the variables appear in the ARRAY statement determines the variable's position in the array. For example, fahr(1) corresponds to the jan variable, fahr(2) corresponds to the feb variable, and fahr(12) corresponds to the dec variable. It's when you use an array like fahr, in conjunction with an iterative DO loop, that you can really simplify your code, as we did in this program.

\n", "

The DO loop tells SAS to process through the elements of the fahr array, each time converting the Celsius temperature to a Fahrenheit temperature. For example, when the index variable i is 1, the assignment statement becomes:

\n", "
fahr(1) = 1.8*fahr(1) + 32;
\n", "

which you could think of as saying:

\n", "
jan = 1.8*jan + 32;
\n", "

The value of jan on the right side of the equal sign is the Celsius temperature. After the assignment statement is executed, the value of jan on the left side of the equal sign is updated to reflect the Fahrenheit temperature.

\n", "

Now, launch and run the SAS program, and review the output from the PRINT procedure to convince yourself that the Celsius temperatures were again properly converted to Fahrenheit temperatures. Oh, one more thing to point out! Note that the variables listed in the PRINT procedure's VAR statement are the original variable names jan, feb, ..., dec, not the variables as they were grouped into an array, fahr(1), fahr(2), ..., fahr(12). That's because an array exists only for the duration of the DATA step. If in the PRINT procedure, you instead tell SAS to print fahr(1), fahr(2), ... you'll see that SAS will hiccup. Let's summarize!

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To define an array, you must use an ARRAY statement having the following general form in order to group previously defined data set variables into an array:\n", "\n", "`ARRAY array-name(dimension) ;`\n", "\n", "where:\n", "\n", "* array-name must be a valid SAS name that specifies the name of the array\n", "* dimension describes the number and arrangement of array elements. The default dimension is one.\n", "* elements list the variables to be grouped together to form the array. The array elements must be either all numeric or all character. Using standard SAS Help notation, the term elements appears in <> brackets to indicate that they are optional. That is, you do not have to specify elements in the ARRAY statement. If no elements are listed, new variables are created with default names.\n", "\n", "A few more points must be made about the array-name. Unless you are interested in confusing SAS, you should not give an array the same name as a variable that appears in the same DATA step. You should also avoid giving an array the same name as a valid SAS function. SAS allows you to do so, but then you won't be able to use the function in the same DATA step. For example, if you named an array mean in a DATA step, you would not be able to use the mean function in the DATA step. SAS will print a warning message in your log window to let you know such. Finally, array names cannot be used in LABEL, FORMAT, DROP, KEEP, or LENGTH statements.\n", "\n", "Let's look at another example to see a different way to define the array used to convert degrees Celsius to Farenheit.\n", "\n", "
\n", "

Example

\n", "

The following program is identical to the program in the previous example, except the 12 in the ARRAY statement has been changed to an asterisk (*) and we use a SAS list to grab the variables for the array:

\n", "
" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Average Monthly Temperatures in Fahrenheit

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Cityjanfebmaraprmayjunjulaugsepoctnovdec
State College, PA28.428.435.646.457.266.269.868.060.850.039.230.2
Miami, FL68.068.071.673.478.880.682.482.480.678.873.468.0
St. Louis, MO30.233.842.855.464.473.478.877.069.859.044.633.8
New Orleans, LA51.855.460.868.073.480.680.680.678.869.860.853.6
Madison, WI17.623.032.044.657.266.271.668.060.850.035.623.0
Houston, TX50.053.660.868.073.480.682.482.478.869.860.853.6
Phoenix, AZ53.657.260.869.878.887.891.489.686.073.460.853.6
Seattle, WA41.042.844.650.055.460.864.464.460.853.646.442.8
San Francisco, CA50.053.653.655.457.259.059.060.862.660.857.251.8
San Diego, CA55.457.259.060.862.666.269.871.669.866.260.857.2
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA avgfahrenheittwo;\n", " set avgcelsius;\n", " array fahr(*) jan -- dec;\n", " do i = 1 to 12;\n", " fahr(i) = 1.8*fahr(i) + 32;\n", " end;\n", "RUN;\n", " \n", "PROC PRINT data = avgfahrenheittwo;\n", " title 'Average Monthly Temperatures in Fahrenheit';\n", " id City;\n", " var jan feb mar apr may jun \n", " jul aug sep oct nov dec;\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Simple enough! Rather than you having to tell SAS how many variables and listing out exactly which ones you are grouping in an array, you can let SAS to the dirty work of counting the number of elements and listing the ones you include in your variable list. To do so, you simply define the dimension using an asterisk (*) and use the SAS list shortcut. You might find this strategy particularly helpful if you are grouping so many variables together into an array that you don't want to spend the time counting and listing them individually. Incidentally, throughout this lesson, we enclose the array's dimension (or index variable) in parentheses ( ). We could alternatively use braces { } or brackets [ ].

\n", "

The above program used a SAS list to shorten the list of variable names grouped into the fahr array. In some cases, you could also consider using the special name lists _ALL_, _CHARACTER_ and _NUMERIC_:

\n", "
    \n", "
  • Use _ALL_ when you want SAS to use all of the same type of variables (all numeric or all character) in your SAS data set.
  • \n", "
  • Use _CHARACTER_ when you want SAS to use all of the character variables in your data set.\n", "
  • \n", "
  • Use _NUMERIC_ when you want SAS to use all of the numeric variables in your data set.
  • \n", "
\n", "

In this case, we could have used the _NUMERIC_ keyword instead as shown in the following program.

\n", "
" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Average Monthly Temperatures in Fahrenheit

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Cityjanfebmaraprmayjunjulaugsepoctnovdec
State College, PA28.428.435.646.457.266.269.868.060.850.039.230.2
Miami, FL68.068.071.673.478.880.682.482.480.678.873.468.0
St. Louis, MO30.233.842.855.464.473.478.877.069.859.044.633.8
New Orleans, LA51.855.460.868.073.480.680.680.678.869.860.853.6
Madison, WI17.623.032.044.657.266.271.668.060.850.035.623.0
Houston, TX50.053.660.868.073.480.682.482.478.869.860.853.6
Phoenix, AZ53.657.260.869.878.887.891.489.686.073.460.853.6
Seattle, WA41.042.844.650.055.460.864.464.460.853.646.442.8
San Francisco, CA50.053.653.655.457.259.059.060.862.660.857.251.8
San Diego, CA55.457.259.060.862.666.269.871.669.866.260.857.2
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA avgfahrenheitthree;\n", " set avgcelsius;\n", " array fahr(*) _NUMERIC_;\n", " do i = 1 to 12;\n", " fahr(i) = 1.8*fahr(i) + 32;\n", " end;\n", "RUN;\n", " \n", "PROC PRINT data = avgfahrenheitthree;\n", " title 'Average Monthly Temperatures in Fahrenheit';\n", " id City;\n", " var jan feb mar apr may jun \n", " jul aug sep oct nov dec;\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating New Variable in an Array Statement\n", "\n", "So far, we have learned several ways to group existing variables into an array. We can also create new variables in an ARRAY statement by omitting the array elements from the statement. When our ARRAY statement fails to reference existing variables, SAS automatically creates new variables for us and assigns default names to them.\n", "\n", "
\n", "

Example

\n", "

The following program again converts the average monthly Celsius temperatures in ten cities to average montly Fahrenheit temperatures. To do so, the already existing Celsius temperatures, jan, feb, ..., and dec, are grouped into an array called celsius, and the resulting Fahrenheit temperatures are stored in new variables janf, febf, ..., decf, which are grouped into an array called fahr:

\n", "
" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Average Monthly Temperatures

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Cityjanjanffebfebfmarmarfapraprfmaymayfjunjunfjuljulfaugaugfsepsepfoctoctfnovnovfdecdecf
State College, PA-228.4-228.4235.6846.41457.21966.22169.82068.01660.81050.0439.2-130.2
Miami, FL2068.02068.02271.62373.42678.82780.62882.42882.42780.62678.82373.42068.0
St. Louis, MO-130.2133.8642.81355.41864.42373.42678.82577.02169.81559.0744.6133.8
New Orleans, LA1151.81355.41660.82068.02373.42780.62780.62780.62678.82169.81660.81253.6
Madison, WI-817.6-523.0032.0744.61457.21966.22271.62068.01660.81050.0235.6-523.0
Houston, TX1050.01253.61660.82068.02373.42780.62882.42882.42678.82169.81660.81253.6
Phoenix, AZ1253.61457.21660.82169.82678.83187.83391.43289.63086.02373.41660.81253.6
Seattle, WA541.0642.8744.61050.01355.41660.81864.41864.41660.81253.6846.4642.8
San Francisco, CA1050.01253.61253.61355.41457.21559.01559.01660.81762.61660.81457.21151.8
San Diego, CA1355.41457.21559.01660.81762.61966.22169.82271.62169.81966.21660.81457.2
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA avgtemps;\n", " set avgcelsius;\n", " array celsius(12) jan feb mar apr may jun \n", " jul aug sep oct nov dec;\n", " array fahr(12) janf febf marf aprf mayf junf\n", " julf augf sepf octf novf decf;\n", " do i = 1 to 12;\n", " fahr(i) = 1.8*celsius(i) + 32;\n", " end;\n", "RUN;\n", " \n", "PROC PRINT data = avgtemps;\n", " title 'Average Monthly Temperatures';\n", " id City;\n", " var jan janf feb febf mar marf;\n", " var apr aprf may mayf jun junf;\n", " var jul julf aug augf sep sepf;\n", " var oct octf nov novf dec decf;\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

The DATA step should look eerily similar to that of Example 7.6. The only thing that differs here is rather than writing over the Celsius temperatures, they are preserved by storing the calculated Fahrenheit temperatures in new variables called janf, febf, ..., and decf. The first ARRAY statement tells SAS to group the jan, feb, ..., dec variables in the avgcelsius data set into a one-dimensional array called celsius. The second ARRAY statement tells SAS to create twelve new variables called janf, febf, ..., and decf and to group them into an array called fahr. The DO loop processes through the twelve elements of the celsius array, converts the Celsius temperatures to Fahrenheit temperatures, and stores the results in the fahr array. The PRINT procedure then tells SAS to print the contents of the twelve Celsius temperatures and twelve Fahrenheit temperatures side-by-side. Launch and run the SAS program, and review the output from the PRINT procedure to convince yourself that the Celsius temperatures were properly converted to Fahrenheit temperatures.

\n", "

Alternatively, we could let SAS do the naming for us in the fahr array.

\n", "
" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Average Monthly Temperatures in Fahrenheit

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Cityfahr1fahr2fahr3fahr4fahr5fahr6fahr7fahr8fahr9fahr10fahr11fahr12
State College, PA28.428.435.646.457.266.269.868.060.850.039.230.2
Miami, FL68.068.071.673.478.880.682.482.480.678.873.468.0
St. Louis, MO30.233.842.855.464.473.478.877.069.859.044.633.8
New Orleans, LA51.855.460.868.073.480.680.680.678.869.860.853.6
Madison, WI17.623.032.044.657.266.271.668.060.850.035.623.0
Houston, TX50.053.660.868.073.480.682.482.478.869.860.853.6
Phoenix, AZ53.657.260.869.878.887.891.489.686.073.460.853.6
Seattle, WA41.042.844.650.055.460.864.464.460.853.646.442.8
San Francisco, CA50.053.653.655.457.259.059.060.862.660.857.251.8
San Diego, CA55.457.259.060.862.666.269.871.669.866.260.857.2
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA avgtempsinF;\n", " set avgcelsius;\n", " array celsius(12) jan feb mar apr may jun \n", " jul aug sep oct nov dec;\n", " array fahr(12);\n", " do i = 1 to 12;\n", " fahr(i) = 1.8*celsius(i) + 32;\n", " end;\n", "RUN;\n", " \n", "PROC PRINT data = avgtempsinF;\n", " title 'Average Monthly Temperatures in Fahrenheit';\n", " id City;\n", " var fahr1-fahr12;\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Note that when we define the fahr array in the second ARRAY statement, we specify how many elements the fahr array should contain (12), but we do not specify any variables to group into the array. That tells SAS two things: i) we want to create twelve new variables, and ii) we want to leave the naming of the variables to SAS. In this situation, SAS creates default names by concatenating the array name and the numbers 1, 2, 3, and so on, up to the array dimension. Here, for example, SAS creates the names fahr1, fahr2, fahr3, ..., up to fahr12. That's why we refer to the Fahrenheit temperatures as fahr1 to fahr12 in the PRINT procedure's VAR statement. Launch and run the SAS program, and review the output from the PRINT procedure to convince yourself that the Celsius temperatures were again properly converted to Fahrenheit temperatures.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Temporary Array Elements\n", "\n", "When elements of an array are constants needed only for the duration of the DATA step, you can omit the variables associated with an array group and instead use temporary array elements. Although they behave like variables, temporary array elements:\n", "\n", "* do not appear in the resulting data set;\n", "* do not have names and can be only referenced by their array names and dimensions; and\n", "* are automatically retained, rather than being reset to missing at the beginning of the next iteration of the DATA step.\n", "\n", "In this section, we'll look at three examples that involve checking a subset of Quality of Life data for errors. In the following example, we'll look to see if the data recorded in ten variables — qul3a, qul3b, ..., and qul3j —are within an expected range without using an array. Then, we'll look to see if the data recorded in the same ten variables are within an expected range using an array that corresponds to three new variables error1, error2, and error3. Finally, we'll look to see if the data recorded in the same ten variables are within an expected range using an array containing only temporary elements.\n", "\n", "
\n", "

Example

\n", "

The following program first reads a subset of Quality of Life data (variables qul3a, qul3b, ..., and qul3j) into a SAS data set called qul. Then, the program checks to make sure that the values for each variable have been recorded as either a 1, 2, or 3 as would be expected from the data form. If a value for one of the variables does not equal 1, 2, or 3, then that observation is output to a data set called errors. Otherwise, the observation is output to the qul data set. Because the error checking takes places without using arrays, the program contains a series of ten if/then statements, corresponding to each of the ten concerned variables:

\n", "
" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Observations in Qul data set with no errors

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obssubjqul3aqul3bqul3cqul3dqul3equl3fqul3gqul3hqul3iqul3j
11100111233332113
22110111232123213
33100171233333221
45100011111112122
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "

Observations in Qul data set with errors

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obssubjqul3aqul3bqul3cqul3dqul3equl3fqul3gqul3hqul3iqul3j
12100122341223311
24110204333322222
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA qul errors;\n", " input subj qul3a qul3b qul3c qul3d qul3e \n", " qul3f qul3g qul3h qul3i qul3j;\n", " flag = 0;\n", " if qul3a not in (1, 2, 3) then flag = 1;\n", " if qul3b not in (1, 2, 3) then flag = 1;\n", " if qul3c not in (1, 2, 3) then flag = 1;\n", " if qul3d not in (1, 2, 3) then flag = 1;\n", " if qul3e not in (1, 2, 3) then flag = 1;\n", " if qul3f not in (1, 2, 3) then flag = 1;\n", " if qul3g not in (1, 2, 3) then flag = 1;\n", " if qul3h not in (1, 2, 3) then flag = 1;\n", " if qul3i not in (1, 2, 3) then flag = 1;\n", " if qul3j not in (1, 2, 3) then flag = 1;\n", " if flag = 1 then output errors;\n", " else output qul;\n", " drop flag;\n", " DATALINES;\n", " 110011 1 2 3 3 3 3 2 1 1 3\n", " 210012 2 3 4 1 2 2 3 3 1 1\n", " 211011 1 2 3 2 1 2 3 2 1 3\n", " 310017 1 2 3 3 3 3 3 2 2 1\n", " 411020 4 3 3 3 3 2 2 2 2 2\n", " 510001 1 1 1 1 1 1 2 1 2 2\n", " ;\n", "RUN;\n", "PROC PRINT data = qul;\n", " TITLE 'Observations in Qul data set with no errors';\n", "RUN;\n", "PROC PRINT data = errors;\n", " TITLE 'Observations in Qul data set with errors';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

The INPUT statement first reads an observation of data containing one subject's quality of life data. An observation is assumed to be error-free (flag is initially set to 0) until it is found to be in error (flag is set to 1 if any of the ten values are out of range). If an observation is deemed to contain an error (flag = 1) after looking at each of the ten values, it is output to the errors data set. Otherwise (flag = 0) , it is output to the qul data set.

\n", "

First, note that two of the observations in the input data set contain data recording errors. The qul3c value for subject 210012 was recorded as 4, as was the qul3a value for subject 411020. Then, launch and run the SAS program. Review the output to convince yourself that qul contains the four observations with clean data, and errors contains the two observations with bad data.

\n", "

You should also appreciate that this is a classic situation that cries out for using arrays. If you aren't yet convinced, imagine how long the above program would be if you had to write similar if/then statements to check for errors in, say, a hundred such variables.

\n", "

The following program performs the same error checking as the previous program except here the error checking is accomplished using two arrays, bounds and quldata:

\n", "
" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Observations in Qul data set with no errors

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obssubjqul3aqul3bqul3cqul3dqul3equl3fqul3gqul3hqul3iqul3jerror1error2error3
11100111233332113123
22110111232123213123
33100171233333221123
45100011111112122123
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "

Observations in Qul data set with errors

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obssubjqul3aqul3bqul3cqul3dqul3equl3fqul3gqul3hqul3iqul3jerror1error2error3
12100122341223311123
24110204333322222123
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA qul errors;\n", " input subj qul3a qul3b qul3c qul3d qul3e \n", " qul3f qul3g qul3h qul3i qul3j;\n", " array bounds (3) error1 - error3 (1 2 3);\n", " array quldata (10) qul3a -- qul3j;\n", " flag = 0;\n", " do i = 1 to 10;\n", " if quldata(i) ne bounds(1) and\n", " quldata(i) ne bounds(2) and\n", " quldata(i) ne bounds(3)\n", " then flag = 1;\n", " end;\n", " if flag = 1 then output errors;\n", " else output qul;\n", " drop i flag;\n", " DATALINES;\n", " 110011 1 2 3 3 3 3 2 1 1 3\n", " 210012 2 3 4 1 2 2 3 3 1 1\n", " 211011 1 2 3 2 1 2 3 2 1 3\n", " 310017 1 2 3 3 3 3 3 2 2 1\n", " 411020 4 3 3 3 3 2 2 2 2 2\n", " 510001 1 1 1 1 1 1 2 1 2 2\n", " ;\n", "RUN;\n", " \n", "PROC PRINT data = qul;\n", " TITLE 'Observations in Qul data set with no errors';\n", "RUN;\n", " \n", "PROC PRINT data = errors;\n", " TITLE '\n", "Observations in Qul data set with errors';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

If you compare this program to the previous program, you'll see that the only differences here are the presence of two ARRAY definition statements and the IF/THEN statement within the iterative DO loop that does the error checking.

\n", "

The first ARRAY statement uses a numbered range list to define an array called bounds that contains three new variables — error1, error2, and error3. The \"(1 2 3)\" that appears after the variable list error1-error3 tells SAS to set, or initialize, the elements of the array to equal 1, 2, and 3. In general, you initialize an array in this manner, namely listing as many values as their are elements of the array and separating each pair of values with a space. If you intend for your array to contain character constants, you must put the values in single quotes. For example, the following ARRAY statement tells SAS to define a character array (hence the dollar sign \\$) called weekdays:

\n", "
ARRAY weekdays(5) $ ('M' 'T' 'W' 'R' 'F');
\n", "

and to initialize the elements of the array as M, T, W, R, and F.

\n", "

The second ARRAY statement uses a name range list to define an array called quldata that contains the ten quality of life variables. The IF/THEN statement uses slightly different logic than the previous program to tell SAS to compare the elements of the quldata array to the elements of the bounds array to determine whether any of the values are out of range.

\n", "

Now, launch and run the SAS program. Review the output to convince yourself that just as before qul contains the four observations with clean data, and errors contains the two observations with bad data. Also, note that the three new error variables error1, error2, and error3 remain present in the data set.

\n", "

The valid values 1, 2, and 3 are needed only temporarily in the previous program. Therefore, we alternatively could have used temporary array elements in defining the bounds array. The following program does just that. It is identical to the previous program except here the bounds array is defined using temporary array elements rather than using three new variables error1, error2, and error3:

\n", "
" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Observations in Qul data set with no errors

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obssubjqul3aqul3bqul3cqul3dqul3equl3fqul3gqul3hqul3iqul3j
11100111233332113
22110111232123213
33100171233333221
45100011111112122
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "

Observations in Qul data set with errors

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obssubjqul3aqul3bqul3cqul3dqul3equl3fqul3gqul3hqul3iqul3j
12100122341223311
24110204333322222
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA qul errors;\n", " input subj qul3a qul3b qul3c qul3d qul3e \n", " qul3f qul3g qul3h qul3i qul3j;\n", " array bounds (3) _TEMPORARY_ (1 2 3);\n", " array quldata (10) qul3a -- qul3j;\n", " flag = 0;\n", " do i = 1 to 10;\n", " if quldata(i) ne bounds(1) and\n", " quldata(i) ne bounds(2) and\n", " quldata(i) ne bounds(3)\n", " then flag = 1;\n", " end;\n", " if flag = 1 then output errors;\n", " else output qul;\n", " drop i flag;\n", " DATALINES;\n", " 110011 1 2 3 3 3 3 2 1 1 3\n", " 210012 2 3 4 1 2 2 3 3 1 1\n", " 211011 1 2 3 2 1 2 3 2 1 3\n", " 310017 1 2 3 3 3 3 3 2 2 1\n", " 411020 4 3 3 3 3 2 2 2 2 2\n", " 510001 1 1 1 1 1 1 2 1 2 2\n", " ;\n", "RUN;\n", " \n", "PROC PRINT data = qul;\n", " TITLE 'Observations in Qul data set with no errors';\n", "RUN;\n", " \n", "PROC PRINT data = errors;\n", " TITLE '\n", "Observations in Qul data set with errors';\n", "RUN" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

If you compare this program to the previous program, you'll see that the only difference here is the presence of the _TEMPORARY_ argument in the definition of the bounds array. The bounds array is again initialized to the three valid values \"(1 2 3)\".

\n", "

Launch and run the SAS program. Review the output to convince yourself that just as before qul contains the four observations with clean data, and errors contains the two observations with bad data. Also, note that the temporary array elements do not appear in the data set.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Array Bounds\n", "\n", "Each of the arrays we've considered thus far have been defined, by default, to have a lower bound of 1 and an upper bound which equals the number of elements in the array's dimension. For example, the array my_array:\n", "\n", "`ARRAY my_array(4) el1 el2 el3 el4;`\n", "\n", "has a lower bound of 1 and an upper bound of 4. In this section, we'll look at three examples that concern the bounds of an array. In the first example, we'll use the DIM function to change the upper bound of a DO loop's index variable dynamically (rather than stating it in advance). In the second example, we'll define the lower and upper bounds of a one-dimensional array to create a bounded array. In the third example, we'll use the LBOUND and HBOUND functions to change the lower and upper bounds of a DO loop's index variable dynamically.\n", "\n", "
\n", "

Example

\n", "

The following program reads the yes/no responses of five subjects to six survey questions (q1, q2, ..., q6) into a temporary SAS data set called survey. A yes response is coded and entered as a 2, while a no response is coded and entered as a 1. Just four of the variables (q3, q4, q5, and q6) are stored in a one-dimensional array called qxs. Then, a DO LOOP, in conjunction with the DIM function, is used to recode the responses to the four variables so that a 2 is changed to a 1, and a 1 is changed to a 0:

\n", "
" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

The survey data using dim function

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obssubjq1q2q3q4q5q6
11001120100
21002211110
310032210.1
410041.0001
51005211110
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA survey (DROP = i);\n", " INPUT subj q1 q2 q3 q4 q5 q6;\n", " ARRAY qxs(4) q3-q6;\n", " DO i = 1 to dim(qxs);\n", " qxs(i) = qxs(i) - 1;\n", " END;\n", " DATALINES;\n", "1001 1 2 1 2 1 1\n", "1002 2 1 2 2 2 1\n", "1003 2 2 2 1 . 2\n", "1004 1 . 1 1 1 2\n", "1005 2 1 2 2 2 1\n", ";\n", "RUN;\n", " \n", "PROC PRINT data = survey;\n", " TITLE 'The survey data using dim function';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

First, note that although all of the survey variables (q1, ..., q6) are read into the survey data set, the ARRAY statement groups only 4 of the variables (q3, q4, q5, q6) into the one-dimensional array qxs. For example, qxs(1) corresponds to the q3 variable, qxs(2) corresponds to the q4 variable, and so on. Then, rather than telling SAS to process the array from element 1 to element 4, the DO loop tells SAS to process the array from element 1 to the more general DIM(qxs). In general, the DIM function returns the number of the elements in the array, which in this case is 4. The DO loop tells SAS to recode the values by simply subtracting 1 from each value. And, the index variable i is output to the survey data set by default and is therefore dropped.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Example

\n", "

As previously discussed and illustrated, if you do not specifically tell SAS the lower bound of an array, SAS assumes that the lower bound is 1. For most arrays, 1 is a convenient lower bound and the number of elements is a convenient upper bound, so you usually don't need to specify both the lower and upper bounds. However, in cases where it is more convenient, you can modify both bounds for any array dimension.

\n", "

In the previous example, perhaps you find it a little awkward that the array element qxs(1) corresponds to the q3 variable, the array element qxs(2) corresponds to the q4 variable, and so on. Perhaps you would find it more clear for the array element qxs(3) to correspond to the q3 variable, the array element qxs(4) to correspond to the q4 variable, ..., and the array element qxs(6) to correspond to the q6 variable. The following program is similar in function to the previous program, except here the task of recoding is accomplished by defining the lower bound of the qxs array to be 3 and the upper bound to be 6:

\n", "
" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

The survey data using bounded arrays

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obssubjq1q2q3q4q5q6
11001120100
21002211110
310032210.1
410041.0001
51005211110
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA survey2 (DROP = i);\n", " INPUT subj q1 q2 q3 q4 q5 q6;\n", " ARRAY qxs(3:6) q3-q6;\n", " DO i = 3 to 6;\n", " qxs(i) = qxs(i) - 1;\n", " END;\n", " DATALINES;\n", "1001 1 2 1 2 1 1\n", "1002 2 1 2 2 2 1\n", "1003 2 2 2 1 . 2\n", "1004 1 . 1 1 1 2\n", "1005 2 1 2 2 2 1\n", ";\n", "RUN;\n", " \n", "PROC PRINT data = survey2;\n", " TITLE 'The survey data using bounded arrays';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

If you compare this program with the previous program, you'll see that only two things differ. The first difference is that the ARRAY statement here defines the lower bound of the qxs array to be 3 and the upper bound to be 6. In general, you can always define the lower and upper bounds of any array dimension in this way, namely by specifying the lower bound, then a colon (:), and then the upper bound. The second difference is that, for the DO loop, the bounds on the index variable i are specifically defined here to be between 3 and 6 rather than 1 to DIM(qxs) (which in this case is 4).

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Example

\n", "

Now, there's still a little bit more that we can do to automate the handling of the bounds of an array dimension. The following program again uses a one-dimensional array qxs to recode four survey variables as did the previous two programs. Here, though, an asterisk (*) is used to tell SAS to determine the dimension of the qxs array, and the LBOUND and HBOUND functions are used to tell SAS to determine, respectively, the lower and upper bounds of the DO loop's index variable dynamically:

\n", "
" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

The survey data by changing upper and lower bounds automatically

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obssubjq1q2q3q4q5q6
11001120100
21002211110
310032210.1
410041.0001
51005211110
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA survey3 (DROP = i);\n", " INPUT subj q1 q2 q3 q4 q5 q6;\n", " ARRAY qxs(*) q3-q6;\n", " DO i = lbound(qxs) to hbound(qxs);\n", " qxs(i) = qxs(i) - 1;\n", " END;\n", " DATALINES;\n", "1001 1 2 1 2 1 1\n", "1002 2 1 2 2 2 1\n", "1003 2 2 2 1 . 2\n", "1004 1 . 1 1 1 2\n", "1005 2 1 2 2 2 1\n", ";\n", "RUN;\n", " \n", "PROC PRINT data = survey3;\n", " TITLE 'The survey data by changing upper and lower bounds automatically';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

If you compare this program with the previous program, you'll see that only two things differ. The first difference is that the asterisk (*) that appears in the the ARRAY statement tells SAS to determine the bounds on the dimensions of the array during the declaration of qxs. SAS counts the number of elements in the array and determines that the dimension of qxs is 4. The second difference is that, for the DO loop, the bounds on the index variable i are determined dynamically to be between LBOUND(qxs) and HBOUND(qxs).

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Two-Dimensional Arrays\n", "\n", "Two-dimensional arrays are straightforward extensions of one-dimensional arrays. You can think of one-dimensional arrays such as the array barkers:\n", "\n", "`ARRAY barkers(4) dog1-dog4;`\n", "\n", "as a single row of variables:\n", "\n", "`dog1 dog2 dog3 dog4`\n", "\n", "And two-dimensional arrays such as the array pets:\n", "\n", "`ARRAY pets(2,4) dog1-dog4 cat1-cat4;`\n", "\n", "as multiple rows of variables:\n", "\n", "
\n",
    "dog1  dog2  dog3  dog4\n",
    "cat1  cat2  cat3  cat4\n",
    "
\n", "\n", "As the previous ARRAY statement suggests, to define a two-dimensional array, you specify the number of elements in each dimension, separated by a comma. In general, the first dimension number tells SAS how many rows your array needs, while the second dimension number tells SAS how many columns your array needs.\n", "\n", "When you define a two-dimensional array, the array elements are grouped in the order in which they appear in the ARRAY statement. For example, SAS assigns the elements of the array horse:\n", "\n", "`ARRAY horse(3,5) x1-x15;`\n", "\n", "as follows:\n", "\n", "
\n",
    "x1  x2  x3  x4  x5\n",
    "x6  x7  x8  x9  x10\n",
    "x11 x12 x13 x14 x15\n",
    "
\n", "\n", "In this section, we'll look at two examples that involve checking a subset of Family History data for missing values. We'll use one two-dimensional array — the first dimension to store the actual data and the second dimension to store binary status variables that indicate whether a particular data value is missing or not.\n", "\n", "
\n", "

Example

\n", "

This program searches a subset of the family history data for missing values. Here, we use one two-dimensional array called edit. The first row contains the actual data and the second row contains a 0/1 indicator of missingness for the observed data in the corresponding column of the two-dimensional array.

\n", "
" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

The FHX data itself

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsfhx1fhx2fhx3fhx4fhx5fhx6fhx7fhx8fhx9fhx10fhx11fhx12fhx13fhx14
13000.800111.10
23000.000000.00
33000.000000.00
450000011001101
550000180001100
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "

The presence of missing values in FHX data

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsstat1stat2stat3stat4stat5stat6stat7stat8stat9stat10stat11stat12stat13stat14
100001000000100
200001000000100
300001000000100
400000000000000
500000000000000
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA fhx;\n", " input subj v_date mmddyy8. fhx1-fhx14;\n", " array edit(2,14) fhx1-fhx14 stat1-stat14;\n", " do i = 1 to 14;\n", " edit(2,i) = 0;\n", " if edit(1,i) = . then edit(2,i) = 1;\n", " end;\n", " DATALINES;\n", "220004 07/27/93 0 0 0 . 8 0 0 1 1 1 . 1 0 1\n", "410020 11/11/93 0 0 0 . 0 0 0 0 0 0 . 0 0 0\n", "520013 10/29/93 0 0 0 . 0 0 0 0 0 0 . 0 0 1\n", "520068 08/10/95 0 0 0 0 0 1 1 0 0 1 1 0 1 0\n", "520076 08/25/95 0 0 0 0 1 8 0 0 0 1 1 0 0 1\n", ";\n", "RUN;\n", " \n", "PROC PRINT data = fhx;\n", " var fhx1-fhx14;\n", " TITLE 'The FHX data itself';\n", "RUN;\n", " \n", "PROC PRINT data = fhx;\n", " var stat1-stat14;\n", " TITLE 'The presence of missing values in FHX data';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

We have just one ARRAY statement that defines the two-dimensional array edit containing 2 rows and 14 columns. The ARRAY statement tells SAS to group the family history variables (fhx1, ..., fhx14) into the first dimension and to group the status variables (stat1, ..., stat14) into the second dimension. Then, the DO loop tells SAS to review the contents of the 14 variables and to assign each element of the status dimension a value of 0 (\"edit(2,i) = 0;\"). If the element of the edit dimension is missing, however, then SAS is told to change the element of the status dimension from a 0 to a 1 (\"if edit(1,i) = . then edit(2,i) = 1\").

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reshaping Data\n", "\n", "What is wide/long data?\n", "\n", "Wide means that we have multiple columns per observations, e.g. visit1, visit2, visit3" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Wide Dataset

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsidvisit1visit2visit3
111043
2256.
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA wide;\n", " INPUT id visit1 visit2 visit3;\n", " INFILE DATALINES;\n", " DATALINES;\n", "1 10 4 3\n", "2 5 6 .\n", ";\n", "RUN;\n", "\n", "PROC PRINT data = wide;\n", " TITLE 'Wide Dataset';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Long means that we have multiple rows per observation." ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Long Dataset

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsidvisitvalue
11110
2124
3133
4215
5226
623.
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA long;\n", " INPUT id visit value;\n", " INFILE DATALINES;\n", " DATALINES;\n", "1 1 10 \n", "1 2 4 \n", "1 3 3\n", "2 1 5 \n", "2 2 6 \n", "2 3 .\n", ";\n", "RUN;\n", "\n", "PROC PRINT data = long;\n", " TITLE 'Long Dataset';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In SAS, there are two ways to reshape data between wide and long formats\n", "\n", "* PROC TRANSPOSE\n", "* DATA step\n", "\n", "We will explore both ways with some examples.\n", "\n", "### Reshaping Data from Long (Tall) Wide (Fat)\n", "\n", "Recall the tallgrades dataset from an earlier section." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

The tall grades data set

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
idnol_namegtypegrade
10SmithE178
10SmithE282
10SmithE386
10SmithE469
10SmithP197
10SmithF1160
11SimonE188
11SimonE272
11SimonE386
11SimonE499
11SimonP1100
11SimonF1170
12JonesE198
12JonesE292
12JonesE392
12JonesE499
12JonesP199
12JonesF1185
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA tallgrades;\n", " input idno 1-2 l_name $ 5-9 gtype $ 12-13 grade 15-17;\n", " cards;\n", "10 Smith E1 78\n", "10 Smith E2 82\n", "10 Smith E3 86\n", "10 Smith E4 69\n", "10 Smith P1 97\n", "10 Smith F1 160\n", "11 Simon E1 88\n", "11 Simon E2 72\n", "11 Simon E3 86\n", "11 Simon E4 99\n", "11 Simon P1 100\n", "11 Simon F1 170\n", "12 Jones E1 98\n", "12 Jones E2 92\n", "12 Jones E3 92\n", "12 Jones E4 99\n", "12 Jones P1 99\n", "12 Jones F1 185\n", ";\n", "RUN;\n", " \n", "PROC PRINT data = tallgrades NOOBS;\n", " TITLE 'The tall grades data set';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The tallgrades data set contains one observation for each grade for each student. Students are identified by their id number (idno) and last name (l_name). The data set contains six different types of grades: exam 1 (E1), exam 2 (E2), exam 3 (E3), exam 4 (E4), each worth 100 points; one project (P1) worth 100 points; and a final exam (F1) worth 200 points. Launch and run the SAS program so that we can work with the tallgrades data set in the next two examples." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Example

\n", "

In this example, we will transpose the tallgrades dataset from long to wide format by using a DATA step. This will require the use of an array and both the RETAIN and OUTPUT statements, and the FIRST. and LAST. SAS variables.

\n", "
" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

The fat grades data set

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsidnol_nameE1E2E3E4P1F1
110Smith7882866997160
211Simon88728699100170
312Jones9892929999185
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA fatgrades;\n", " set tallgrades;\n", " by idno;\n", " retain E1-E4 P1 F1 i;\n", " array allgrades (6) E1-E4 P1 F1;\n", " if first.idno then i = 1;\n", " allgrades(i) = grade;\n", " if last.idno then output;\n", " i = i + 1;\n", " drop i gtype grade;\n", "RUN;\n", " \n", "PROC PRINT data=fatgrades;\n", " title 'The fat grades data set';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Yikes! This code looks scary! Let's dissect it a bit. First, the tallgrades data set is processed BY idno. Doing so, makes the first.idno and last.idno variables available for us to use. The ARRAY statement defines an array called allgrades and, using a numbered range list, associates the array with six (uninitialized) variables E1, E2, E2, E4, P1, and F1. The allgrades array is used to hold the six grades for each student before they are output in their transposed direction to the fatgrades data set. Because the elements of any array, and therefore allgrades, must be assigned using an index variable, this is how the transposition takes place:

\n", "
    \n", "
  • (\"if first.idno then i = 1;\") If the input observation contains a student idno that hasn't yet been encountered in the data set, then the index variable i is initialized to 1. If the input observation doesn't contain a new student idno, then do nothing other than advance to the next step.
  • \n", "
  • (\"allgrades(i) = grade;\") The grade from the current observation is assigned to the array allgrades. (For example, if the input observation contains Smith's first grade, then allgrades(1) is assigned the value 78. If the input observation contains Smith's second grade, then allgrades(2) is assigned the value 82. And so on.) Note that this assumes that the order of the grades is the same within each student.
  • \n", "
  • (\"if last.idno then output;\") If the input observation is the last observation in the data set that contains the student idno, then dump the program data vector (which contains allgrades) to the output data set. (For example, if the input observation is Smith's final exam grade, then output the now fat observation containing his six grades). If the input observation is not the last observation in the data set that contains the student idno, do nothing other than advance to the next step.
  • \n", "
  • (\"i = i + 1;\") Then, increase the index variable i by 1. (For example, if i is 1, change i to 2.)
  • \n", "
  • (\"retain E1-E4 P1 F1 i;\") Rather than setting E1, E2, E3, E4, P1, F1, and i to missing at the beginning of the next iteration of the data step, retain their current values. (So, for example, for Smith, allgrades(1) would retain its value of 78, allgrades (2) would retain its value of 82, and so on.) Similar for the index coutner i. We do not want to reset this variable until we get to a new student.
  • \n", "
\n", "

The program would keep cycling through the above five steps until it encountered the last observation in the data set. Then, the variables i, gtype, and grade would be dropped from the output fatgrades data set.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Example

\n", "

SAS also has procedure called PROC TRANSPOSE that can be used to transpose datasets between wide and tall formats. Personally, I find this function somewhat unintuitive compared to the DATA step method (at least once you get used to using the DATA step), so I tend to always use the DATA step. However, in this next example we will show how to perform the same transposition using PROC TRANSPOSE and leave it to the reader to decide which method is preferred.

\n", "
" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

The fat grades data set

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsidnol_nameE1E2E3E4P1F1
110Smith7882866997160
211Simon88728699100170
312Jones9892929999185
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "PROC TRANSPOSE data = tallgrades \n", " out = fatgrades2 (drop = _NAME_) ;\n", " BY idno l_name;\n", " VAR grade;\n", " ID gtype;\n", "RUN;\n", " \n", "PROC PRINT data = fatgrades2;\n", " title 'The fat grades data set';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

In PROC TRANSPOSE,

\n", "
    \n", "
  • The OUT statement, as with PROC SORT, specifies the name of the output dataset created by PROC TRANSPOSE.
  • \n", "
  • The BY clause specifies the variable(s) that are unique within the groups between transposed to a single row. In this case, a single observation corresponds to a given student. Although, idno is the main key, last name (l_name) should also be kept. Without adding it to the BY statement, l_name would be dropped from the transposed dataset.
  • \n", "
  • The VAR statement specifies the variable containing the value of the transposed variable. In this case, it is the actual grade value.
  • \n", "
  • The ID statement can be used to specify column names from an existing variable's values. In this case, we use the assessment name to identify the grade.
  • \n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reshaping Data from Wide (Fat) to Tall (Long)\n", "\n", "In the next two examples, we will learn how to transform the newly created wide version of the grades dataset back to the tall dataset version.\n", "\n", "
\n", "

Example

\n", "

In this example, we will use a DATA step to transpose the grades dataset from wide back to tall format.

\n", "
" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Tallgrades2 Data

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
idnol_namegtypegrade
10SmithE178
10SmithE282
10SmithE386
10SmithE469
10SmithP197
10SmithF1160
11SimonE188
11SimonE272
11SimonE386
11SimonE499
11SimonP1100
11SimonF1170
12JonesE198
12JonesE292
12JonesE392
12JonesE499
12JonesP199
12JonesF1185
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA tallgrades2;\n", " set fatgrades;\n", " array gtypes(6) $ _TEMPORARY_ ('E1' 'E2' 'E3' 'E4' 'P1' 'F1');\n", " array grades(*) E1 -- F1;\n", " DO i = 1 TO 6;\n", " gtype = gtypes(i);\n", " grade = grades(i);\n", " OUTPUT;\n", " END;\n", " DROP E1--F1 i ;\n", "RUN;\n", "\n", "PROC PRINT data = tallgrades2 NOOBS;\n", " TITLE 'Tallgrades2 Data';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

We create two arrays: gtypes is a temporary character array to return the columns names (the assessment types) to the variable gtypes and grades to store the grades on the current row (i.e. current student) to be assigned to the grade variable. Each iteration of the DO loop has an OUPUT statement to put each grade in its own row. Note that the idno and l_name are carried over from row to row until we run out of grades to OUTPUT. Then we exit the DO loop and move on the next row (student). We drop the (wide) columns E1, E2, E3, E4, P1, F1, and the index i from the final dataset to obtain the original tall dataset.

\n", "

An alternative way to get the column names is to use the vname function instead of manually listing out the names in an character array. The vname function when applied to a variable returns the variable name as a character value.

\n", "
" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Tallgrades3 Data

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
idnol_namegtypegrade
10SmithE178
10SmithE282
10SmithE386
10SmithE469
10SmithP197
10SmithF1160
11SimonE188
11SimonE272
11SimonE386
11SimonE499
11SimonP1100
11SimonF1170
12JonesE198
12JonesE292
12JonesE392
12JonesE499
12JonesP199
12JonesF1185
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA tallgrades3;\n", " set fatgrades;\n", " array grades(*) E1 -- F1;\n", " DO i = 1 TO 6;\n", " gtype = vname(grades(i));\n", " grade = grades(i);\n", " OUTPUT;\n", " END;\n", " DROP E1--F1 i ;\n", "RUN;\n", "\n", "PROC PRINT data = tallgrades3 NOOBS;\n", " TITLE 'Tallgrades3 Data';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Example

\n", "

Now we will do the same operation as the previous example but using PROC TRANSPOSE.

\n", "
" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Tallgrades2 Data

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsidnol_namegtypegrade
110SmithE178
210SmithE282
310SmithE386
410SmithE469
510SmithP197
610SmithF1160
711SimonE188
811SimonE272
911SimonE386
1011SimonE499
1111SimonP1100
1211SimonF1170
1312JonesE198
1412JonesE292
1512JonesE392
1612JonesE499
1712JonesP199
1812JonesF1185
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "PROC TRANSPOSE data = fatgrades \n", " out = tallgrades3 (RENAME = (COL1 = grade \n", " _NAME_ = gtype));\n", " BY idno l_name;\n", " VAR E1--F1;\n", "RUN;\n", "\n", "PROC PRINT data = tallgrades3;\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

The BY statement defines the grouping variables that define a single observation and are copied to each new row when going from wide to long. The VAR statement defines all the columns that should be gatherd into a multiple rows by defining two new columns _NAME_ which holds the former column name and _COL1_ which holds the data value from that former column in the current row. Typically, we will want to change these default names by using the RENAME dataset option.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Merging Datasets\n", "\n", "In this section, we will learn how to combine multiple SAS datasets into a single dataset by\n", "\n", "* Concatenating two datatsets vertically with the SET statement\n", "* Merging datasets with the MERGE statement to perform inner, outer, left and right joins.\n", "\n", "### Concatenating Two or More Datasets\n", "\n", "To concatenate two or more SAS data sets means to stack one \"on top\" of the other into a single SAS data set. For example, suppose the data set store1 contains three variables, `store` (number), `day` (of the week), and `sales` (in dollars):\n", "\n", "\n", "\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\n", "\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\n", "
StoreDaySale
1M1200
1T1435
1W1712
1R1529
1F1920
1S2325
\n", "\n", "and the data set store2 contains the same three variables:\n", "\n", "\n", "\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\n", "\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\n", "
StoreDaySales
2M2215
2T2458
2W1789
2R1692
2F2105
2S2847
\n", "\n", "Then, when we concatenate the two data sets, we get what I like to call a \"tall\" data set:\n", "\n", "\n", "\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\n", "\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\n", "
StoreDaySales
1M1200
1T1435
1W1712
1R1529
1F1920
1S2325
2M2215
2T2458
2W1789
2R1692
2F2105
2S2847
\n", "\n", "in which the data sets are stacked on top of each other. Note that the number of observations in the new data set is the sum of the numbers of observations in the original data sets. To concatenate SAS data sets, you simplify specify a list of data set names in one SET statement.\n", "\n", "As you know, variable attributes include the type of variable (character vs. numeric), the informat (how the variable is read in) and format (how its values are printed) of a variable, the length of the variable, and the label (how its variable name is printed) of a variable. Concatenating data sets when variable attributes differ across the input data sets may pose problems for SAS (and therefore you):\n", "\n", "* If the data sets you name in the SET statement contain variables with the same names and types, you can concatenate the data sets without modification.\n", "* If the variable types differ, you must modify one or more of the data sets before concatenating them. SAS will not concatenate the data sets until you do.\n", "* If the lengths, formats, informats or labels differ, you may want to modify one or more of the data sets before concatenating them. SAS will concatenate the data sets; you may just not like the results. These attributes will be taken from the dataset that is read in first (i.e. appears first in the SET statement)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Example

\n", "

The following program concatenates the store1 and store2 data sets to create a new \"tall\" data set called bothstores:

\n", "
" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

The bothstores data set

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
StoreDaySales
1M1200
1T1435
1W1712
1R1529
1F1920
1S2325
2M2215
2T2458
2W1798
2R1692
2F2105
2S2847
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA store1;\n", " input Store Day $ Sales;\n", " DATALINES;\n", " 1 M 1200\n", " 1 T 1435\n", " 1 W 1712\n", " 1 R 1529\n", " 1 F 1920\n", " 1 S 2325\n", " ;\n", "RUN;\n", " \n", "DATA store2;\n", " input Store Day $ Sales;\n", " DATALINES;\n", " 2 M 2215\n", " 2 T 2458\n", " 2 W 1798\n", " 2 R 1692\n", " 2 F 2105\n", " 2 S 2847\n", " ;\n", "RUN;\n", " \n", "DATA bothstores;\n", " set store1 store2;\n", "RUN;\n", " \n", "PROC PRINT data = bothstores NOOBS;\n", " title 'The bothstores data set';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Note that the input data sets — store1 and store2 — contain the same variables — Store, Day, and Sales — with identical attributes. In the third DATA step, the DATA statement tells SAS to create a new data set called bothstores, and the SET statement tells SAS that the data set should contain first the observations from store1 and then the observations from store2. Note that although we have specified only two input data sets here, the SET statement can contain any number of input data sets.

\n", "

Launch and run the SAS program, and review the output from the PRINT procedure to convince yourself that SAS did indeed concatenate the store1 and store2 data sets to make one \"tall\" data set called bothstores. You might then want to edit the SET statement so that store1 follows store2, and re-run the SAS program to see that then the contents of store1 follow the contents of store2 in the bothstores data set.

\n", "

In general, a data set that is created by concatenating data sets contains all of the variables and all of the observations from all of the input data sets. Therefore, the number of variables the new data set contains always equals the total number of unique variables among all of the input data sets. And, the number of observations in the new data set is the sum of the numbers of observations in the input data sets. Let's return to the contrived example we've used throughout this lesson.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Example

\n", "

The following program concatenates the one and two data sets to create a new \"tall\" data set called onetopstwo:

\n", "
" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

The onetopstwo data set

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
IDVarAVarBVarC
10A1B1  
20A2B2  
30A3B3  
40  B4C1
50  B5C2
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA one;\n", " input ID VarA $ VarB $;\n", " DATALINES;\n", " 10 A1 B1\n", " 20 A2 B2\n", " 30 A3 B3\n", " ;\n", "RUN;\n", " \n", "DATA two;\n", " input ID VarB $ VarC $;\n", " DATALINES;\n", " 40 B4 C1\n", " 50 B5 C2\n", " ;\n", "RUN;\n", " \n", "DATA onetopstwo;\n", " set one two;\n", "RUN;\n", " \n", "PROC PRINT data = onetopstwo NOOBS;\n", " title 'The onetopstwo data set';\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

As you review the first two DATA steps, in which SAS reads in the respective one and two data sets, note that the total number of unique variables is four — ID, VarA, VarB, and VarC. The total number of observations among the two input data sets is 3 + 2 = 5. Therefore, we can expect the concatenated data set onetopstwo to contain four variables and five observations. Launch and run the SAS program, and review the output to convince yourself that SAS did grab first all of the variables and all of the observations from the one data set and then all of the variables and all of the observations from the two data set. As you can see, to make it all work out okay, observations arising from the one data set have missing values for VarC, and observations from the two data set have missing values for VarA.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Match-Merging SAS Datasets\n", "\n", "Match-merging is one of the most powerful methods of combining two or more SAS data sets. A match-merge combines observations across data sets based on the values of one or more common variables. \n", "\n", "Consider a fictional dataset called `base` that has baseline data for patients with the ids 1 to 10 and their age, and `visits` dataset which contains data for patients with the ids 1 to 8 and 11, the visit number (all patients have 3 visits), and some quantitative outcome measurement.\n", "\n", "To match-merge, you simply specify the data sets you would like to merge in a MERGE statement, and indicate the variables on which you would like to merge in a BY statement. One thing to keep in mind, though, is **you can't match-merge SAS data sets unless they are sorted by the variables appearing in the BY statement**. The variables that you are merging on (i.e that appear in the BY statement) must have the same name in both datasets. If this is not the case, then use the rename option to change the variable name in one of the datasets to match the other before merging." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Example

\n", "

In the following SAS program, we will perform an outer join of the base and visits dataset by merging based in the patient id variable.

\n", "
" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Baseline Dataset

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsidage
1150
2251
3352
4453
5554
6655
7756
8857
9958
101059
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "

Visits Dataset

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsidvisitoutcome
11111
21212
31313
42121
52222
62323
73131
83232
93333
104141
114242
124343
135151
145252
155353
166161
176262
186363
197171
207272
217373
228181
238282
248383
2511350
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA base;\n", " INPUT id age;\n", " DATALINES;\n", "1 50\n", "2 51\n", "3 52\n", "4 53\n", "5 54\n", "6 55\n", "7 56\n", "8 57\n", "9 58\n", "10 59\n", ";\n", "RUN;\n", "\n", "DATA visits;\n", " DO id = 1 TO 8;\n", " DO visit = 1 TO 3;\n", " outcome = 10*id + visit;\n", " OUTPUT;\n", " END;\n", " END;\n", " id = 11;\n", " visit = 3;\n", " outcome = 50;\n", " OUTPUT;\n", "RUN;\n", "\n", "PROC PRINT data = base;\n", " TITLE \"Baseline Dataset\";\n", "RUN;\n", "\n", "PROC PRINT data = visits;\n", " TITLE \"Visits Dataset\";\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

To peform an outer join between the base and visits dataset, we simply use the MERGE statemet with these two datasets and a BY statement with the common variable id. Note that these two datasets are already sorted by id. If they were not, we would also need to sort both datasets by id first.

\n", "
" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Outer Join

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsidagevisitoutcome
1150111
2150212
3150313
4251121
5251222
6251323
7352131
8352232
9352333
10453141
11453242
12453343
13554151
14554252
15554353
16655161
17655262
18655363
19756171
20756272
21756373
22857181
23857282
24857383
25958..
261059..
2711.350
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA outer;\n", " MERGE base visits;\n", " BY id;\n", "RUN;\n", "\n", "PROC PRINT data = outer;\n", " TITLE \"Outer Join\";\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

In this merge, all variables and rows from both datasets are kept with the id columns from both datasets merged to a single column. Note that for id's that appeared in base and not in visits, the variables in visits, visit and outcome, that were not in base were set to missing (see id 9 and 10), and for ids in the visit dataset that were not in the base dataset, the variables that were in base but not in visit, age in this case, were set to missing (see id 11).

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before we learn how to do inner, left and right joins, we need to discuss the IN= SAS dataset option. The IN= option tells SAS to create an \"indicator variable\" that takes on either the value 0 or 1 depending on whether or not the current observation comes from the input data set. If the observation does not come from the input data set, then the indicator variable takes on the value 0. If the observation does come from the input data set, then the indicator takes on the value 1. The IN= option is especially useful when merging and concatenating data sets which we'll study in the next two lessons. The basic format of the IN option is:\n", "\n", "`IN = varname`\n", "\n", "where varname is the name of a variable in the input data set. Let's revisit the previous example where we keep track of the IN= variable values.\n", "\n", "
\n", "

Example

\n", "

Let's merge base and visit again, but this time create the IN= variables for each dataset and store their values in two permanent variables in_base and in_visit:

\n", "
" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Outer Join with IN Variables

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsidagevisitoutcomein_basein_visit
2285718111
2385728211
2485738311
25958..10
261059..10
2711.35001
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA outer2;\n", " MERGE base (IN = in1) \n", " visits (IN = in2);\n", " BY id;\n", " in_base = in1;\n", " in_visit = in2;\n", "RUN;\n", "\n", "PROC PRINT data = outer2 (FIRSTOBS=22 OBS=27);\n", " TITLE \"Outer Join with IN Variables\";\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

The ids for patients 1 through 8 appear in both dataset, so for all of the rows corresponding to these ids both in_base and in_visit have the value 1 since we combined data from both datasets to create these rows. But for ids 9 and 10, these only appeared in the base dataset so in_base is 1 and in_visit is 0, since there was no data from visit to use to make these rows. Similarly for id 11, the only data was contained in the visit dataset so in_visit is 1 and in_base is 0.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To perform an inner join, we only want to keep rows that have matching ids in both datasets. Using the IN= variables, we only want to keep rows in which both (or all if we have more than 2 datasets) are 1.\n", "\n", "
\n", "

Example

\n", "

The following SAS program performs an inner join between the base and visit dataset. This means that we will only keep rows in which the merging variable, id, appear in both datasets.

\n", "
" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Inner Join

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsidagevisitoutcome
1150111
2150212
3150313
4251121
5251222
6251323
7352131
8352232
9352333
10453141
11453242
12453343
13554151
14554252
15554353
16655161
17655262
18655363
19756171
20756272
21756373
22857181
23857282
24857383
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA inner;\n", " MERGE base (IN = in1) \n", " visits (IN = in2);\n", " BY id;\n", " IF in1 = 1 AND in2 = 1; *subsetting IF. Only keep these rows;\n", "RUN;\n", "\n", "PROC PRINT data = inner;\n", " TITLE \"Inner Join\";\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Now only the rows made from ids 1 to 8 are in the merged dataset because these were the only ids that occurred in both the base and visit datasets.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Example

\n", "

In this example, we will perform left and right joins using the IN= variables. A left join will keep all records from the left (i.e. first dataset in the MERGE statement) and join matching records from the right dataset and dropping the rest. The reverse happesn for a right join.

\n", "
" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "
\n", "
\n", "

Left Join

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsidagevisitoutcome
1150111
2150212
3150313
4251121
5251222
6251323
7352131
8352232
9352333
10453141
11453242
12453343
13554151
14554252
15554353
16655161
17655262
18655363
19756171
20756272
21756373
22857181
23857282
24857383
25958..
261059..
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "

Right Join

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsidagevisitoutcome
1150111
2150212
3150313
4251121
5251222
6251323
7352131
8352232
9352333
10453141
11453242
12453343
13554151
14554252
15554353
16655161
17655262
18655363
19756171
20756272
21756373
22857181
23857282
24857383
2511.350
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA left;\n", " MERGE base (IN = in1) \n", " visits (IN = in2);\n", " BY id;\n", " IF in1 = 1; *subsetting IF. Only keep these rows;\n", "RUN;\n", "\n", "PROC PRINT data = left;\n", " TITLE \"Left Join\";\n", "RUN;\n", "\n", "DATA right;\n", " MERGE base (IN = in1) \n", " visits (IN = in2);\n", " BY id;\n", " IF in2 = 1; *subsetting IF. Only keep these rows;\n", "RUN;\n", "\n", "PROC PRINT data = right;\n", " TITLE \"Right Join\";\n", "RUN;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises\n", "\n", "For these exercises, we will use the Bike_Lanes_Wide.csv, Bike_Lanes.csv, crashes.csv and roads.csv. Please be sure that you have these datasets downloaded to a convenient location on your computer. See the datasets folder provided on the course webpage.\n", "\n", "1. Read in the Bike_Lanes_Wide.csv dataset and call is `wide` using PROC IMPORT. Print the first few rows of the dataset.\n", "2. Reshape `wide` using either PROC TRANSPOSE or a DATA step. You will need to gather all columns except the name column. Transform into a long dataset with two new columns `lanetype` (the former column names) and `the_length` the data values. In the variable `the_length`, replace 'NA' values with . and convert it to a numeric column.\n", "3. Read in the roads and crashes .csv files and call them `road` and `crash`.\n", "4. Replace (using `tranwrd`) any hyphens (`-`) with a space in `Road` variable of `crash`. Call this data `crash2`. Table the `Road` variable with PROC FREQ.\n", "5. How many observations are in each of the `crash` and `road` datasets?\n", "6. Separate the `Road` column (using `scan`) into (`type` and `number`) in `crash2`. Reassign this to `crash2`. Table `type` from `crash2` using PROC FREQ. Then create a new variable calling it `road_hyphen` using one of the concatenate functions (such as CAT). Unite the `type` and `number` columns using a hyphen (`-`) and then table `road_hyphen` using PROC FREQ.\n", "7. Which and how many years were data collected in the `crash` dataset?\n", "8. Read in the dataset Bike_Lanes.csv and call it `bike`.\n", "9. Keep rows where the record is not missing `type` and not missing `name` and re-assign the output to `bike`.\n", "10. Using PROC MEANS with a BY statement grouping `name` and `type` (i.e for each type within each name), find the `sum` of the `length`. Use an OUTPUT statement to get this summary dataset and only keep the `name`, `type` and sum of the `length` column (after renaming this column `length`). Call this data set `sub`.\n", "11. Reshape `sub` from long to wide by taking the `type` to be the new columns and `length` to be the value in those columns. (NOTE: the `names` have spaces in them. Do we need to replace the spaces with a character before changing them to column names? The DATA step approach is more challenging here since not all streets have the same bike lane types.)\n", "12. Join data in the crash and road datasets to retain only complete data, (using an inner join on road) Merge by the variable `Road`. Call the output merged. How many observations are there?\n", "13. Join data using a `full_join`. Call the output `full`. How many observations are there?\n", "14. Do a left join of the `road` and `crash`. ORDER matters here! How many observations are there?\n", "15. Repeat above with a `right_join` with the same order of the arguments. How many observations are there?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "SAS", "language": "sas", "name": "sas" }, "language_info": { "codemirror_mode": "sas", "file_extension": ".sas", "mimetype": "text/x-sas", "name": "sas" } }, "nbformat": 4, "nbformat_minor": 2 }