Getting Started Using SAS Software 1.1 The SAS Language 2 1.2 SAS Data Sets 4 1.3 The Two Parts of a SAS Program 6 1.4 The DATA Step’s Built-in Loop 8 1.5 Choosing a Mode for Submitting SAS Programs 10 1.6 Windows and Commands in the SAS Windowing Environment 12 1.7 Submitting a Program in the SAS Windowing Environment 14 1.8 Reading the SAS Log 16 1.9 Viewing Your Results in the Output Window 18 1.10 Creating HTML Output 20 1.11 SAS Data Libraries 22 1.12 Viewing Data Sets with SAS Explorer 24 1.13 Using SAS System Options 26
1.1 The SAS Language Many software applications are either menu driven, or command driven (enter a command see the result). SAS is neither. With SAS, you use statements to write a series of instructions called a SAS program. The program communicates what you want to do and is written using the SAS language. There are some menu-driven front ends to SAS, for example SAS Enterprise Guide software, which make SAS appear like a point-and-click program. However, these front ends still use the SAS language to write programs for you. You will have much more flexibility using SAS if you learn to write your own programs using the SAS language. Maybe learning a new language is the last thing you want to do, but be assured that although there are parallels between SAS and languages you know (be they English or FORTRAN), SAS is much easier to learn. SAS programs A SAS program is a sequence of statements executed in order. A statement gives information or instructions to SAS and must be appropriately placed in the program. An everyday analogy to a SAS program is a trip to the bank. You enter your bank, stand in line, and when you finally reach the teller’s window, you say what you want to do. The statements you give can be written down in the form of a program: I would like to make a withdrawal. My account number is 0937. I would like $200. Give me five 20s and two 50s. Note that you first say what you want to do, then give all the information the teller needs to carry out your request. The order of the subsequent statements may not be important, but you must start with the general statement of what you want to do. You would not, for example, go up to a bank teller and say, “Give me five 20s and two 50s.” This is not only bad form, but would probably make the teller’s heart skip a beat or two. You must also make sure that all the subsequent statements belong with the first. You would not say, “I want the largest box you have” when making a withdrawal from your checking account. That statement belongs with “I would like to open a safe deposit box.” A SAS program is an ordered set of SAS statements like the ordered set of instructions you use when you go to the bank. SAS statements As with any language, there are a few rules to follow when writing SAS programs. Fortunately for us, the rules for writing SAS programs are much fewer and simpler than those for English. The most important rule is Every SAS statement ends with a semicolon. This sounds simple enough. But while children generally outgrow the habit of forgetting the period at the end of a sentence, SAS programmers never seem to outgrow forgetting the semi-colon at the end of a SAS statement. Even the most experienced SAS programmer will at least occasionally forget the semicolon. You will be two steps ahead if you remember this simple rule. Layouts of SAS programs, there really aren’t any rules about how to format your SAS program. While it is helpful to have a neat looking program with each statement on a line by itself and indentions to show the various parts of the program, it isn’t necessary. SAS statements can be in upper- or lowercase. Statements can continue on the next line (as long as you don’t split words in two). Statements can be on the same line as other statements. Statements can start in any column. So you see, SAS is so flexible that it is possible to write programs so disorganized that no one can read them, not even you. (Of course, we don’t recommend this.) Comments to make your programs more understandable, you can insert comments into your programs. It doesn’t matter what you put in your comments SAS doesn’t look at it. You could put your favorite cookie recipe in there if you want. However, comments are usually used to annotate the program, making it easier for someone to read your program and understand what you have done and why. There are two styles of comments you can use: one starts with an asterisk (*) and ends with a semicolon (;). The other style starts with a slash asterisk (/*) and ends with an asterisk slash (*/). The following SAS program shows the use of both of these style comments: * Read animals’ weights from file; DATA animals; INFILE ’c:\MyRawData\Zoo.dat’; INPUT Lions Tigers; PROC PRINT DATA = animals; /* Print the results */ RUN; Since some operating environments interpret a slash asterisk (/*) in the first column as the end of a job, be careful when using this style of comment not to place it in the first column. For this reason, we chose the asterisk-semicolon style of comment for this book. Errors People who are just learning a programming language often get frustrated because their programs do not work correctly the first time they write them. To make matters worse, SAS errors often come up in bright red letters, and for the poor person whose results turn out more red than black, this can be a very humbling experience. You should expect errors. Most programs simply don’t work the first time, if for no other reason than you are human. You forget a semicolon, misspell a word, have your fingers in the wrong place on the keyboard. It happens. Often one small mistake can generate a whole list of errors. Don’t panic if you see red. 第一章 SAS入门 1.1 SAS编程语言 1.2 SAS数据集 1.3 SAS程序的两部分 1.4 数据步的内部循环 1.5 选择SAS程序的提交模式 1.6 SAS窗口环境的窗口与命令 1.7 在SAS窗口环境中提交程序 1.8 阅读SAS记录 1.9 在输出窗口中查看结果 1.10 创建HTML形式的结果输出 1.11 SAS数据库 1.12 使用SAS Explorer查看数据集 1.13 使用SAS系统选项 1.1 SAS编程语言
许多应用软件要不使用菜单驱动,要不使用命令行驱动(输入一条命令,然后查看结果)。可是SAS两者都不是,在SAS中你可以写一系列指令,构成一个SAS程序。该程序是使用SAS语言编写的,它向计算机传递你想执行的操作。SAS中同样存在一些菜单驱动前端,例如企业版SAS向导软件,它使得SAS看起来像一个鼠标点击的程序。然而这些前端仍然是使用SAS语言来为你编写程序。如果你学会了用SAS语言来为自己编写程序,那将更加灵活。也许学习一门新语言是你最不想做的一件事,但是请放心,虽然SAS和其它你所知道的语言(如英语或FORTRAN)一样,但是SAS更加简单。 SAS程序 一个SAS程序是由一系列的按顺序执行的语句组成的。每个语句向SAS提供信息或指令,所以必须在程序中处于合适的位置。一个通俗的比喻是上银行,首先你走进一家银行,然后排队,最后当你到达出纳窗口的时候,告诉出纳员你想做什么。你给出的陈述可以用一个程序的形式写下来: 我想取款。 我的账号是0937。 我想取200元。 请给我5张20的和2张50的。 注意,这里你先说你想做什么,然后才将其它所有信息告诉出纳员,出纳员执行你的请求。这里,后面语句的顺序也许并不重要,但是首先必须使用你想做什么的语句开头。你不会直接这样对一个银行出纳员说“给我5张20的和2张50的”。这不仅是一种错误的形式,而且可能导致出纳的心跳加速。同样,你需要确保随后所有的语句都属于第一句。当你取钱的时候,你不会说“我想要你最大的盒子”。那条语句属于“我想打开一个保险箱”。一个SAS程序就是一系列和你去银行时使用的一系列的指令一样的有序SAS语句。 SAS语句 和其它任何语言一样,同样有一些规则来编写SAS程序。幸运的是编写SAS程序的规则要比英语要少很多,而且简单得多。 最重要的一条规则是 每条SAS语句都使用分号结尾。 这条规则听上去非常简单。但是当孩子们逐渐抛弃忘记在句子结尾写句号的习惯时,SAS程序员们好像从来都记不住要在SAS语句的结尾写分号。甚至经验丰富的SAS程序员也会偶尔忘记写分号。如果你记住了这条简单的规则,你已经前进了2步了。 SAS程序的布局 SAS程序对格式没有任何的规定。虽然一个格式整齐,每条语句独占一行并且使用缩进来区分各部分的程序对阅读很有帮助,但是这并不是必需的。 SAS语句不区分大小写 SAS语句可以续行(只要不将单词一分为二) 同一行可以有多条SAS语句 SAS语句可以从任何一行开始 注释 你可以在程序中插入注释,使之更具可读性。注释中可以放入任何东西,因为SAS不会去读取里面的一切,只要你喜欢,可以将你最喜欢的糕点的烹饪法放入其中。 不过注释一般用来注解程序,使得其他人更加易于阅读程序和理解你在做什么,为什么这么做。 SAS中有两种可选的注释风格:一种是使用星号(*)号开头,然后分号(;)结尾。另一种方式是(/*)开头,然后(*/)结尾。下面的SAS程序展示了这两种注释方法: * Read animals’ weights from file; DATA animals; INFILE ’c:\MyRawData\Zoo.dat’ ; INPUT Lions Tigers; PROC PRINT DATA = animals; /* Print the results */ RUN ; 因为在有些操作环境中,第一行的(/*)被解释为一个工作的结束,所以注意当使用这种注释时,记得不要将它放在第一行。出于这种原因,在这本书中我们选择星号-分号的注释风格。 错误 一些人在学习一门编程语言的时候,经常会感到很沮丧,因为他们第一次写的程序不能正确运行。更糟糕的是SAS的错误都是用明亮的红色来标示的,对那些结果中红色比黑色还多的人来说,这是一次非常遗憾的经历。你应该预料到会有错误。只要你还是人,那么大部分程序一般都会由于忘记分号、拼错单词、敲错字母等原因,第一次都不能正确运行。如果这些事发生了,通常一个小小的失误会引出一长串的红色错误,所以如果遇到这种事,千万别崩溃。
|