Chapter Contents

Previous

Next
Doing More with SAS/ASSIST Software

Converting a Character column to a Numeric column

Sometimes numeric data is stored in columns that have character data types. In order to do calculations with this numeric data, you must convert the character columns to numeric. You can use Subset/Copy on the Data Management menu to change a column from a character to a numeric format. In this section, you convert the character column DAY, which is found in the ORANGES table, to a numeric column. The value in the character column DAY is a (character) number from 1 to 6.


Additional Information

For additional information on changing columns from character to numeric and vice versa, refer to the PUT and INPUT functions and formats and informats in SAS Language Reference: Dictionary.


Instructions

  1. Follow this selection path:

    Tasks
    [arrow]
    Data Management
    [arrow]
    Subset/Copy
    The Subset or Copy a Table window appears.

    Subset or Copy A Table Window

    [IMAGE]

  2. Use the Table button to select the SASUSER.ORANGES table.

  3. Select Output data. The Output Table or View window appears.

    Output Table or View Window

    [IMAGE]

  4. Type ORANGNUM in the Table/View field.

    ORANGNUM is a new table that you create to contain the new numeric column DAYNUM. By leaving the defaults of Temporary and Table selected, you create a temporary table in the WORK library, which is deleted when you exit the SAS System. An indicator next to Temporary and Table shows that these items are selected.

  5. Select OK to return to the Subset or Copy a Table window.

  6. Select Define new columns. The Define or Modify a column window appears.

    Define or Modify a Column Window

    [IMAGE]

    You use this window to define the new columns that you are adding to the output table.

  7. In the column field, type DAYNUM as the name of the column.

  8. Leave the Length as 8.

    Leave the Type as Numeric.

  9. Select Initialize. The Enter Numeric Expression window appears.

    Enter Numeric Expression Window

    [IMAGE]

    You use this window to build an expression that is used to define the new column.

  10. Select Function. The Select the Type of Function window appears.

    Select Type of Function Window

    [IMAGE]

  11. Select Character functions. The Select Data window for character functions appears.

    Select Data Window for Character Functions

    [IMAGE]

  12. Select INPUT(c,ifmt). The Specify Arguments to a Function window appears.

    Specify Arguments to a Function Window

    [IMAGE]

    You select INPUT(c,ifmt) because the source column DAY is character.

  13. In the Value for parameter field next to CHAR, type DAY.

    In the Value for parameter field next to INFORM, type 2.1.

    You type DAY in the CHAR parameter field because you want to use the values of the DAY column in the ORANGES table as the source for the DAYNUM column that you are creating.

    The informat 2.1 is used in this example so you can check to see that this task completed correctly. However, you can use any valid informat. In this example, if you select a format such as 1., the only indication that the new column is numeric is that it is right justified when displayed in the FSVIEW window. However, if you use 2.1 as an informat, the new column DAYNUM is right justified and converted to display tenths.

  14. Select OK four times until you return to the Subset or Copy a table window.

    Follow this selection path:

    Run
    [arrow]
    Submit
    When prompted, select OK, then Goback to view the new table.

    The new DAYNUM column is created, and the data is copied to the output table. An FSVIEW window appears with the data in the output table.

  15. Scroll to the right or use the RIGHT command or function key to see the new column DAYNUM.

    New DAYNUM Column

    [IMAGE]

    To confirm that the column is numeric, browse the properties of the table. See Altering the Properties of a SAS Table for more information.

  16. To exit the FSVIEW window, follow this selection path:

    File
    [arrow]
    Close


Converting Character Dates and Times to SAS Date and Time Values

You can use the above procedure to convert character dates and times to SAS date and time values. In the Specify Arguments to a Function window, specify an appropriate date, time, or datetime informat for INFORM. For example, if you have a column of character dates in the form 05MAR1999, use the DATE9. informat. In the Define or Modify a Column window, you might want to specify an appropriate date, time, or datetime format by selecting Format.


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.