Creating a new variable in sas enterprise guide

When you want to conditionally process a data set (such as creating a new variable based on a condition), you typically think of using IF-THEN logic, but IF-THEN logic is only valid in DATA step. In SAS Enterprise Guide, the Query Builder uses PROC SQL behind the scenes. In PROC SQL, the CASE statement is used to do conditional processing.

In SAS Enterprise Guide 4.2, the method of using the CASE statement has changed. This sample introduces you to those changes by explaining how to create a new variable named TYPE that is based on the value of a variable named CODE. Specifically, if CODE=1, then TYPE is equal to 'Fruit'. If CODE=2, then TYPE is equal to 'Vegetable'.

Note: For an example of using the CASE statement in SAS Enterprise Guide 4.1, see Sample 32160.

About the sample data

Here is the SAS code that is used to create the sample data set.

data produce; input name $ code; cards; banana 1 apple 1 lettuce 2 carrot 2 pepper 2 orange 1 potato 2 corn 2 grape 1 mango 1 ; run;

Using the CASE statement to create a new variable

  1. Select File ► New ► Program, and then submit the sample code to create the sample data set.
  2. In the Process Flow window, right-click on the PRODUCE data set and select Query Builder.
  3. In the Query Builder, click Computed Columns, and then click New.
  4. In the New Computed Column wizard, select the Recoded Column option, and then click Next.
  5. On page 2 of the wizard, select the column that will be the basis for your new column. For this sample, select the CODE column, and then click Next.
  6. On page 3 of the wizard, select the values you want to use in place of the current values. The new values will be written to a new variable instead of overwriting the values of the current variable. Click Add, and then select the Replace Condition tab.

image label

image label

image label

image label

CASE WHEN t1.code = 1 THEN 'Fruit' WHEN t1.code = 2 THEN 'Vegetable' ELSE 'other' END

image label

image label

Additional Documentation

For more information about using SAS Enterprise Guide, see the SAS Enterprise Guide documentation page.

These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.

These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.

In SAS Enterprise Guide 4.2, the method of using the CASE statement has changed. This sample introduces you to those changes by explaining how to create a new variable based on a condition.

Type:Sample
Date Modified:2010-01-06 14:09:24
Date Created:2009-12-23 14:12:56

Operating System and Release Information

Product FamilyProductHostProduct ReleaseSAS Release
StartingEndingStartingEnding
SAS SystemSAS Enterprise GuideMicrosoft® Windows® for x64 4.2 9.2 TS2M0
Microsoft Windows Server 2003 Datacenter Edition 4.2 9.2 TS2M0
Microsoft Windows Server 2003 Enterprise Edition 4.2 9.2 TS2M0
Microsoft Windows Server 2003 Standard Edition 4.2 9.2 TS2M0
Microsoft Windows XP Professional 4.2 9.2 TS2M0
Windows Vista 4.2 9.2 TS2M0