Select Statement in SAS

Select Statement in SAS

  • Post author:
  • Post category:Base SAS
  • Post comments:0 Comments
  • Reading time:5 mins read

The Select statement in SAS allows you to execute statements conditionally based on the value of a single categorical variable. In most cases, the variable has three or more valid values.

It is SAS’s implementation of the “case” statement – a better and more efficient way to handle mutually exclusive possibilities than IF/THEN/ELSE.

A series of IF and ELSE IF statements can be replaced by a single SELECT statement.

Here is one way to use a SELECT statement:

The expression following the SELECT statement is known as a select-expression; the expression following a WHEN statement is known as a when-expression.

This example compares the select-expression (AgeGroup) with the when-expressions.

data class;
	set sashelp.class;
	select (age);
		when (16) agegroup=1;
		when (15) agegroup=2;
		when (18) agegroup=3;
		otherwise ;
	end;
run;
Select Statement in SAS
Select Statement in SAS

If the comparison is true, the when-expression statement is executed. If the comparison is false next when is compared to select.

OTHERWISE executes if none of the comparisons is true. The otherwise-expression can be null, as shown. If you remove an OTHERWISE statement, the programme will end because none of the previous comparisons is true.

You can place more than one value in the when-expression, like this:

data class;
	set sashelp.class;
	select (age);
		when (11) agegroup=1;
		when (12, 13) agegroup=2;
		when (15, 16, 18) agegroup=3;
		otherwise ;
	end;
run;
Select Statement in SAS
Select Statement in SAS

SELECT statement in SAS when a select expression is missing

If no select expression is provided, the when-expression is evaluated. If true, the statement is executed.

If the result is false, SAS moves to the next when-expression or the following WHEN statement if none are present.

If no, when expression evaluates to true, SAS proceeds to the OTHERWISE statement if it is present.

SAS issues an error message if the result of all when-expressions is false and no OTHERWISE statement is present.

Suppose more than one WHEN statement has a true when expression, only the first WHEN statement is used. After a when-expression is true, no other when-expressions are evaluated.

data heart;
	set sashelp.heart(keep=systolic);
	length cholgroup $6.;
	select;
		when (systolic le 110) cholgroup='low';
		when (systolic le 140) cholgroup='medium';
		otherwise cholgroup='high';
	end;
run;
proc print data=heart(obs=10);
run;
Select Statement in SAS
Select Statement in SAS

Conclusion

It is more efficient to utilise IF/THEN/ELSE statements for decision groups requiring less than three levels of nesting. Using the SELECT statement might result in resource savings when there are three or more layers of nesting. An improvement in readability can reduce the amount of time spent reviewing and debugging.

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.