In this first example,you create a new variable,GRADE, by recoding the values of the variable SCORE into groups using IF-THEN/ELSE
statements in the DATA step.The ranges of the values for SCORE for each of the five GRADE levels are:
SCORE RangeGRADE<65 0
The following simple program accomplishes the desired transformation in a straightforward manner.It certainly gets the job done,and it serves to illustrate a number of good coding features.
Output from Example- Using IF-THEN/ELSE Statements to Recode a Variable
This simple example serves to illustrate three points about coding:
There is more than one equivalent way to write these IF statements.The previous program uses the form:IF value_l LE SCORE LT value_2 THEN GRADE=assigned value;
An alternative syntax (which requires a bit more typing) is:IF value_l LE SCORE AND SCORE LT value_2 THEN GRADE=assigned value;A further alternative is:IF SCORE GE value_l AND SCORE LT value_2 THEN GRADE=assigned value;
They are all equivalent.Take your pick.Some people find it easier to understand the recoding process at a glance when the statements are expanded; some find it easier to use the condensed, almost graphical, version.Do what's best for you.
A Note on Efficiency
A point to observe here is the use of ELSE wherever appropriate.The code could have been written without the ELSE statements,and the results would have been the same,but the program would have taken longer to run.
In this simple case,the increase in CPU time would be negligible, but with long complex programs with many observations and tight resources, the savings can be substantial, if not critical. Practicing good habits is never wasteful.
Without the ELSE statements,the program would execute each IF statement on each observation, regardless of the value of SCORE. All IF tests would be conducted,even after an assignment was made for GRADE.With the ELSE statements in the program, the system stops testing an observation as soon as a " hit" is made.For the first observation,since the value of SCORE is between 0 and 65, the value of GRADE is set equal to 0, and the last three IF tests are not performed.
A Common Error
And now for the danger zone. A common error is to code the first IF statement as follows:IF SCORE LT 65 THEN GRADE=0; *** WRONG WAY! ;
instead of the following:IF 0 LE SCORE LT 65 THEN GRADE=0; CORRECT WAY ;
At first glance it seems like these two statements are equivalent.The difference here is a bit subtle, but it belies a common trap, and is one that even the most experienced programmers have fallen into.
The first statement above assigns a value of 0 to the variable GRADE for any observation that is missing a value for SCORE.The reason for this is the way in which the SAS System stores missing values.Suffice it to say that they are smaller than any negative number,and therefore if the value for SCORE is missing, GRADE receives a value of 0.So,writing the IF statement the wrong way causes all the students with missing values for SCORE to be grouped with the failing students.
This may be what some evil teacher wished, but the important point to remember is that if the code is written one way,missing values are included in a category; if it is written the other way,they are not.Always watch out for this when writing statements containing tests based on inequality operators.A short comment about comments is needed here.Any section of a SAS program that begins with an asterisk (*) and ends with a semicolon (;) is interpreted as a comment by the SAS System and is not executed as are other programming lines.
SAS Programming Related Tutorials
|SASS (Syntactically Awesome Style sheets) Tutorial||R Programming language Tutorial|
SAS Programming Related Interview Questions
|Logistics Interview Questions||SAS Programming,SQL server Interview Questions|
|Clinical SAS Interview Questions||SASS (Syntactically Awesome Style sheets) Interview Questions|
|SQL Server Analysis Services (SSAS) Interview Questions||R Programming language Interview Questions|
|SAS DI Interview Questions||Advanced SAS Interview Questions|
|Base Sas Interview Questions||SAS Macro Interview Questions|
|Clinical Data Management Interview Questions|
Sas Programming Tutorial
Input And Infile
Set, Merge, And Update
Table Lookup Tools
Proc Means And Proc Uimivariate
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.