![]() | ![]() | ![]() |
SAS Enterprise Guide provides easy access to data sources through a graphical interface, which means that 99% of the time you can work in SAS without knowing the SAS programming language. However, if you want to manipulate data, such as changing date values or parsing a character string, then you will need to employ some SAS programming knowledge in order to achieve your goals.
One very common data manipulation is converting a variable type from either character to numeric or from numeric to character. This conversion is done by using the PUT and INPUT functions.
SAS does not allow you to change the type of a variable that is already defined, so a new variable must be created. SAS Enterprise Guide enables you to create new variables (computed columns) by using the Advanced Expression builder within the Query Builder. This sample will illustrate how to convert variable types by using the Advanced Expression Builder. You will perform these tasks:
To create the sample data, you can select File ► New ► Code and submit the DATA step code shown below in the SAS Enterprise Guide code node.
DATA SAMPLE;
INPUT DATE :$10. NUM;
CARDS;
1/10/2006 123
4/24/2005 456
7/4/2007 789
;
RUN;
|
After you submit the code, the table opens automatically.
Display the Process Flow window, right-click on your sample data set, and select Filter and Query. This will open the Enterprise Guide Query Builder.
Click Computed Columns and then select New ► Build Expression to open the Advanced Expression Editor dialog box.
Working with the character date value first, you will use the INPUT function to create a new numeric SAS variable. The INPUT function converts character strings to numeric values, using the appropriate informat that corresponds to the character string.
For example, if you had a value of 08MAR2000, you would use the DATEw. informat to read the value. For the date values in the sample data set, you need to use the MMDDYYw. informat. The structure of the expression looks like this:
INPUT(date,mmddyy10.)
|
The first argument to the INPUT function is the variable that you want to convert. The second argument is the appropriate informat and width.
Using the Advanced Expression Editor, click the Functions tab. In the Category list, scroll down to Special, select INPUT(character) from the Functions list, and then click Add to Expression.
Click the Data tab and select the DATE variable. Then, select the <charValue> placeholder in the Expression text box and click Add to Expression. You cannot get a list of available informats through the Advanced Expression Editor, so you must select the second placeholder <value> and type MMDDYY10.. The expression should now look like this:
Click OK to exit the Advanced Expression editor, and then rename the variable from Calculation1 to a name of your choice. Click Close to return to the Query Builder where you should now see the calculated column in the query builder.
You will now perform similar tasks in order to convert the numeric value to a character value, except you will use the PUT function instead of the INPUT function. The PUT function converts a numeric variable into a character string, using the appropriate format that corresponds to the numeric value. The formatted value is then stored as a character string.
Because you want to create a character string with three leading zeroes, you will use the Zw. numeric format. The structure of the expression looks like this:
PUT(num,Z6.)
|
The first argument to the PUT function is the variable that you want to convert. The second argument is the appropriate format and width.
In the Query Builder, click Computed Columns again, and then select New ► Build Expression to open the Advanced Expression Editor dialog box.
Using the Advanced Expression Editor, click the Functions tab. In the Category list, scroll down to Special, select PUT(numeric) from the Functions list, and then click Add to Expression.

Click the Data tab and select the NUM variable. Then, select the <numValue> placeholder in the Expression text box and click Add to Expression. You cannot get a list of available informats through the Advanced Expression Editor, so you must select the second placeholder <value> and type Z6.. The expression should now look like this:

Click OK to exit the Advanced Expression editor, and then rename the variable from Calculation1 to a name of your choice. Click Close to return to the Query Builder where you should now see the calculated column in the query builder.

In the Query Builder, select the variables that you want to use in the query, including the two new variables. Click Run. You should see the new variables in the resulting data set.

If you are converting SAS dates, be aware that a SAS date value is a number equal to the number of days since January 1, 1960. This is what the INPUT function has created from the character date string: an unformatted SAS date value. To display the value as a recognizable date, you must apply a date format to the variable.
On the Select Data tab in the Query Builder, select the new date variable, and then click
(Properties) to the right. This will open the Properties dialog box for the date variable.

By default, there is no format applied to the variable. Click Change (to the left of the Format field) to open the Formats dialog box.
From the Categories list, select Date, and then select the date format and width you want to apply to the variable.

Click OK to return to the Query Builder and then click Run to re-run the query.
You should see the newly formatted values in the resulting data set.

For more information about using SAS Enterprise Guide, see the SAS Enterprise Guide documentation page.
| Type: | Sample |
| Topic: | SAS Reference ==> Functions ==> Special ==> INPUT SAS Reference ==> Functions ==> Special ==> PUT Common Programming Tasks ==> Working with Character Data Data Management ==> Manipulation and Transformation ==> Date and Time Data Management ==> Manipulation and Transformation ==> Expressions |
| Date Modified: | 2009-06-05 11:21:43 |
| Date Created: | 2009-03-25 16:22:43 |
| Product Family | Product | Host | Product Release | SAS Release | ||
| Starting | Ending | Starting | Ending | |||
| SAS System | SAS Enterprise Guide | Microsoft Windows 95/98 | 4.1 | |||
| Microsoft Windows 2000 Advanced Server | 4.1 | |||||
| Microsoft Windows 2000 Datacenter Server | 4.1 | |||||
| Microsoft Windows 2000 Server | 4.1 | |||||
| Microsoft Windows 2000 Professional | 4.1 | |||||
| Microsoft Windows NT Workstation | 4.1 | |||||
| Microsoft Windows Server 2003 Datacenter Edition | 4.1 | |||||
| Microsoft Windows Server 2003 Enterprise Edition | 4.1 | |||||
| Microsoft Windows Server 2003 Standard Edition | 4.1 | |||||
| Microsoft Windows XP Professional | 4.1 | |||||
| Windows Millennium Edition (Me) | 4.1 | |||||
| Windows Vista | 4.1 | |||||



