Previous Page | Next Page

Doing More with Data Management

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/CopyThe Subset or Copy a Table window appears.

    Subset or Copy A Table Window

    [Subset or Copy A Table Window]

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

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

    Output Table or View Window

    [Output Table or View Window]

  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 SAS. 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 New Columns window appears.

    Define or Modify a Column Window

    [Define or Modify a Column Window]

    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

    [Enter Numeric Expression Window]

    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

    [Select Type of Function Window]

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

    Select Character Function Window

    [Select Character Function Window]

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

    Specify Arguments to a Function Window

    [Specify Arguments to a Function Window]

    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] SubmitWhen 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

    [New DAYNUM Column]

    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 theFSVIEW 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.

Previous Page | Next Page | Top of Page