Stat 5411 Fall 2008 Lab 3

Accessing SAS in Batch Mode

In lab 1 you ran SAS two different ways: • Using “batch” commands • Using the windows opened by “sas –dms”

The batch method is somewhat clumsy, but it works with a minimum of technology setup. You can run this through any terminal connection to the ub computer UMD. On a PC one option includes Putty which can be downloaded from the UMD Technology Services software website. www.d.umn.edu/Campus Services/ Facilities & Technology/Information Technology/ Services/Software Gets you to http://www.d.umn.edu/itss/software/ Software to download or access online/Secure telnet and ftp clients/Download Putty For example I can connect to ub via wireless service from home.

When running Putty the “Hostname” is ub.d.umn.edu You will not be able to open X-windows applications with Putty.

For another practice at running SAS in batch mode: To connect to ub on the lab machine

• Open the Macintosh HD (hard drive). • Open the Applications folder. • In the utilities folder open the application X11. • At the prompt enter “ssh –X username@ub” (without the “ “ marks and hit Return. o Use your own username, e.g. rregal, the same as your email name. • Enter “yes”when asked if you want to continue connecting. • Enter your password (the same as for email) and hit Return”. • Enter “x” at the prompt asking for the terminal . • In the UMENU window, type a u or scroll down to “Unix” near the bottom and hit Return.

Change to the 5411 directory you created in lab 1. The UNIX means “change directory”.

% cd 5411

The following data are weights required to get a 1” bend in brass and steel rods of different lengths, spans of 5” and 13”, bridged across two supports. The columns are: material, span, weight.

Brass 5 60 Brass 5 61 Brass 5 65 Brass 13 20 Brass 13 18 Brass 13 18 Steel 5 96 Steel 5 103 Steel 5 99 Steel 13 24 Steel 13 24 Steel 13 25

Now to create the data called bridge.dat On the ub computer that we are accessing from the lab ub, one way to enter data into a file is with with the pico editor. Enter

% pico bridge.dat

To create the file bridge.dat. • Enter the data into the file without column headings. o Names for the columns will be given with the “input” statement in the SAS commands. • Note: If you copy/ from here, you end up with tabs between columns. By default SAS expects “white space” spaces, not tabs. You need to delete the tabs and replace them with spaces. • The pico commands are shown at the bottom of the screen. • pico with ^x. meaning holding the control key and "x" at the same . o Enter "y" in answer to "Save modified buffer ...". o Type Enter key to accept the name bridge.dat as the name of the file.

Confirm that the file was created by listing the saved files. % ls is the UNIX command to list all file names. Now print the file to the screen just to be sure it’s ok.

% bridge.dat For a short file like this, it all fits onto one page. In general type the enter key to get the next page. Typing "q" quits right then.

Now create the SAS command file called bridge.sas % pico bridge.sas Enter these commands into the file and exit with ^x, control-x, as before. options ls=80; libname here '.'; data here.bridge; title 'My Name Lab 3'; infile 'bridge.dat'; input material $ span weight; ln_weight = log(weight); * In SAS this the natural log. Log base 10 is log10(weight); proc print; goptions device=pdfc gsfmode=append gsfname=grafout; filename grafout 'bridge.pdf'; axis2 logbase=10; proc gplot; plot weight*span=material; plot weight*span=material/vaxis=axis2; run;

The options and filename statements create the plots in a color pdf (pdfc device driver) file.

To plot data, we can use procedure “plot” or procedure “gplot”. • plot produces simple typewriter type plots in the output window. o These are handy for quick looks at the data and if you are running SAS in batch mode where you can’t open graphics windows. • gplot produces prettier graphs for a particular graphics device. o By default graphs pop up in a Graphics window. o You can set the device as above to other options such as pdfc, color pdf. o If you tried running the SAS commands without specifying where to send the graphs, you would get a question of what device to use. If for example you type in response, you would get a PostScript file or whatever device you specify.

To run SAS use the command % sas bridge By default "sas xx" means run the commands in xx.sas

Running sas creates at least one and hopefully three files. The file bridge.log has information on what happened when running bridge.sas. If there were any errors, the error messages are in bridge.log. Unfortunately, SAS error statements are not very clear. A common problem is to forget a semicolon delimiter, after which SAS is just confused. The error message doesn’t come until the line where SAS finally finds a semicolon. An error early in the run can cascade to further errors later. Sometimes it works best to fix the first error message's problem, and then run again.

Look at bridge.log % more bridge.log

Usually NOTE comments in the log file are fine. If there are any ERROR comments listed in the log file, fix any problems in bike.sas using pico, and rerun the commands with "sas bridge".

The file bridge.lst has output created by the run, unless errors kept the output from being created. It's best to check the .log file before the .lst file to sure there weren't error problems or important warnings in the run. See if the .lst file and the pdf file were created. % ls

If all went well, the file bridge.lst is listed. If not, fix any problems in bridge.sas using pico, and rerun the commands with "sas bridge". If the .lst file is there, look at the file, this time with pico. % pico bridge.lst The printed data should match the bridge data shown above.

Another way to get data into SAS is to include the data with the command file. The following could be put into bridge.sas and run as usual with % sas bridge

options ls=80; libname here '.'; data here.bridge; title 'My Name Lab 3'; input material $ span weight; datalines; Brass 5 60 Brass 5 61 Brass 5 65 Brass 13 20 Brass 13 18 Brass 13 18 Steel 5 96 Steel 5 103 Steel 5 99 Steel 13 24 Steel 13 24 Steel 13 25 ; proc print; run;

Transferring files to and from ub and the Mac

Now we will transfer the pdf file from ub to the Mac. On the Macintosh choose • Lab Apps • Internet • Fetch (double click) (or use the doggie at bottom of the screen) • File/New Connection • Hostname: ub.d.umn.edu • Username/Password Double click your 5411 directory which you created earlier

The file bridge.pdf should be listed. • Drag the bridge.pdf file to the Mac desktop. • Double click the pdf file to look at the plots

Viewing these plots of the data we can see how the data turned out. One conclusion is that in the log scale the variances are more constant. The plotting symbols in the plot aren’t very striking. In the next exercise we will create plots with more control over the plotting symbols.

Email your self the bridge.pdf file as an attachment • pine • Specify the attachment as 5411/bridge.pdf. o If this works, the attachment name will be expanded to include the size of the file being attached.

Plotting in the windowed display from “sas –dms”

An Excel file syrup.xls was imported to SAS on my PC version of SAS with the following commands. Note: This is not an Excel 2007 file.

proc import replace datafile="F:\5411_current_2008\Labs\syrup.xls" out=labs.syrup; sheet='data';

Unfortunately for, the UNIX version doesn’t do this Excel conversion except through a server.

To see how the Excel file started out, open a web browser to http://www.d.umn.edu/~rregal1/documents/5411/syrup.xls Save the file. This ends up on the desktop.

Open the Excel file on the desktop and look at the data sheet as well as the graph sheet. These data are better analyzed in the log scale, partly because of constant variances but also for some better reasons we will cover later. In Excel you can change an axis scale to a log scale with the values labeled in the original scale by changing one of the options for the axis format. Excel however, requires that the axis limits be powers of 10 which is sometimes not great. SAS and other graphing programs allow more versatility.

Now using the UNIX copy command, , copy a file of SAS commands and SAS data file from my directory to your UNIX 5411 directory and make sure the file got there. (You should still be in the 5411 directory. You can always check this with the “print working directory command: .)

• cp /home/volh/14/rregal/5411/syrup.sas syrup.sas • cp /home/volh/14/rregal/5411/syrup.sas7bdat syrup.sas7bdat • cp /home/volh/14/rregal/5411/turtle.sas7bdat turtle.sas7bdat • ls

Now open the windowed access to SAS • sas –dms An X window should be opened automatically.

You can set options for the editing window at the top of the Program Editor window Tools/Options/Program Editor On the General tab you can select Promt to save on window close by making that square darkened. If the square is already darkened, leave it darkened. To remind you to save the commands in a .sas file. On the Editing tab Choosing lines on a carriage return adds a new line of commands when you press the Enter key. Next, uncheck Clear text on submit if this is not already unchecked.

By default the editor window is in overtype mode. I prefer input mode where typing in just pushes old test along, not replacing the old text. In Tools/ Options/Preferences/Editing choose Insert for the Cursor option if you don’t want text overwritten. NOT CHOOSING Automatically store selection keeps highlighting text as highlighted. To paste the text, you need to use Edit/Paste

In the editor window • File/Open/syrup.sas

We could run/submit the entire file, but here we will execute a few commands at a time to check the results.

Highlight, then Submit/Run

libname lab '.'; run;

The . in UNIX means the current working directory. As long as you have “changed directories”, cd, to 5411, this means your 5411 directory.

proc contents data=lab.syrup; run;

This should work as long as you have copied syrup.sas7bdat to your current working directory. The data ‘lab.syrup’ stands for ./syrup.sas7dat, a SAS data set in the current working directory.

In the output window you should see the “contents” of the imported data set. When importing to SAS if you have spaces in variable names, these get replaced with underscores, even if the space is at the end of the name. It’s wise to check the contents to see how the variables names transferred. There can also be problems with numerical data ending up as character in SAS if the first rows of the Excel file have all dots for missing. The first rows are checked to guess what the variable type happens to be.

Highlight , then Submit/Run

proc print data=lab.syrup; run; The printed data are in the Output window.

To see a listing of all possible graphics devices that you can use with gplot, highlight then Submit/Run

proc gdevice catalog=sashelp.devices; run;

In the window with the device names, including pdfc for pdf color, scroll down to see some of the choices, and then close the window.

To plot data, we can use procedure “plot” or procedure “gplot”. • plot produces simple typewriter type plots in the output window. o These are handy for quick looks at the data and if you are running SAS in batch mode where you can’t open graphics windows. • gplot produces prettier graphs for a particular graphics device. o By default graphs pop up in a Graphics window. o You can set the device to other options such as pdfc, color pdf.

To do the basic typewriter plot, highlight, then Submit/Run

proc plot data=lab.syrup; plot time*brand=temperature/vpos=24; run;

Look at the Output window for the plot. The vpos=24 made the vertical axis 24 lines high for easier viewing on screen.

To generate prettier plots, highlight, then Submit/Run

symbol1 value=dot color=red; symbol2 value=square color=blue; axis2 logbase=10;

proc gplot data=lab.syrup; plot time*brand=temperature; plot time*brand=temperature/vaxis=axis2; run; If all goes well, the Graphics window is opened and has two plots you can view by scrolling through the window.

With the plot showing the log scale weights, • Export as image • Name this syrup.gif

Now we make a pdf file of the plot. This works whether you are in a windowed system or running SAS in batch mode. Highlight, then Submit/Run

goptions device=pdfc gsfmode=append gsfname=grafout; filename grafout 'syrup.pdf'; proc gplot data=lab.syrup; plot time*brand=temperature; plot time*brand=temperature/vaxis=axis2; run;

To access the pdf file, you need to release the file from SAS. To reset all graphics options to their defaults, highlight, then Submit/Run the following commands. This undoes the goptions pdfc commands.

goptions reset=goptions; run;

If you want to clear the graphics window to start over,

proc greplay igout=work.gseg nofs; delete _all_; run;

You then close the Graphics window at the red x. The next time SAS askes for graphics, a new Graphics window will open with the graph. Check in the Fetch window for the syrup.pdf file.

You can exit Using Fetch, bring both syrup.gif and syrup.pdf over to the Mac. Open both files to see how they turned out.

Email the files • bridge.pdf • syrup.gif • syrup.pdf to yourself or save them to a memory stick.

Hand in the following on October 24 with the next assignment: • syrup.gif • syrup.pdf • bridge.pdf

One way to view saved SAS files

You can view a SAS data set through

Solutions/Analysis/Analyst File/Open/syrup.sas7bdat

A window should appear with the data.

Plotting in the Analyst Application

One way to plot and view saved SAS files is through the Analyst application. Start the Analyst application we used for power calculations.

Solutions/Analysis/Analyst File/Open/syrup.sas7bdat

You can view the syrup data in this window.

Now for the turtle data. These are width, lengths, and heights of turtle shells, carapaces, for male and female painted turtle. Close the Analyst window and start Interactive Data Analysis Solutions/Analysis/Interactive Data Analysis

Get the turtle data into Insight by choosing lab/turtle/Open This should work as long as you copied the turtle.sas7bdat fiel to your current working directory.

Choose colors for the sexes Edit/Windows/Tools Click on sex click on red/female/OK click on blue/male/OK

To view bivariate plots of the data with all pairings of variables, a “scatterplot matrix”:

In the data window, choose length, with, and height. Click on length/ shift-click height Analysis/Scatterplot

View each variable plotted versus sex. Click on empty cell in the data window to un-select variables. Analysis/Scatter Plot Put length, width, height in Y. Put sex in X. OK

View the 3-D data. Analysis/ Rotating Plot Put height in Z, width in Y, length in X OK Use the spinning arrow buttons to view the data.

Close the plot windows and the turtle data Interactive Data Analysis window to close SAS “Insight”. You do not have to hand in anything from the Insight plots.

Now exit from SAS • File/Exit in the Editor window

Now exit the X-windows terminal program. • File/Exit

SAS Documentation and Guides

The programming guide that comes up with “sas –dms” is one way to learn more about SAS. In addition SAS documentation is on the web. Go to my web site under Links to link to SAS documentation. Expand the Contents tab to see

• Base SAS/ SAS Language Reference: Concepts/ Data Step Concepts • SAS Products/ SAS Graph/SAS Graph Concepts/ • Introduction to SAS Graph Software o Look at this introduction for some example plots • Then expand SAS/Graph Statements • Symbol statement • Scroll down to values to see the optional plotting symbols.