Conversion functions in SAS

sas
format

#1

Hi,

I used to import data sets from different sources and I need to convert data from number to Character and Character to number. Please help me with ways to do these conversions.

Regards,
Steve


#2

@Steve

Mostly used functions to convert number to character and vice versa are INPUT and PUT. Let’s look at these functions is detail:-

INPUT():- This function enables to explicitly convert character data values to numeric values.

Syntax: INPUT(Character_Variable, format), FORMAT contains the SAS format that you want applied to the variable that is specified in the source. The format type must be the same as the source type, either character or numeric

Example:

Data Test;
Input Sale$;
datalines;
12345
56789
;
Run;

Data Check;
Set Test;
Amount=Input(Sale,Best5.);
Run;

PUT():- This function enables to explicitly convert numeric data values to character values.

Syntax: PUT(Numerical_Variable, format), Informat must be numeric informat

Exmple:-

Data Test;
Input ID;
datalines;
1001
1002
;
Run;

Data Check;
Set Test;
P_ID=put(ID,4.);
Run;

Let’s look at SAS Formats:-

SAS formats have the form of

<$> format <w>.<d>, Here $ indicates character format, w indicates width and d means number of decimal position.

Examples:-

Format                       Stored Value                         Result
$4.                             Amitaggarwal                       Amit
12.                              1234.2864                         1234
12.2                             1234.2864                         1234.29 
COMMA12.2                        1234.2864                        1,234.29
DOLLAR12.2                       1234.2864                        $1,234.29

Regards,
Sunil