Reading Combinations of Numeric and Character Data - SAS Programming

For this example you have measurements in a file that are expressed either in inches or centimeters. Each measurement is followed directly by its unit of measurement (I or C).Here is a program to read this combination of data and create a variable that represents all measurements in centimeters:

Example

DATA CONVERT;
INPUT DUMMY $ 86J
DUMMY2=SUBSTR (DUMMY,1,LENGTH(DUMMY)-1)? 0
IF INDEX (DUMMY,'I') NE 0 THEN $
CM=2.54 *INPUT (DUMMY2,5.);
ELSE IF INDEX (DUMMY,'CM SE 0 THEN 0
CM*INPUT (DUMMY2,5.);
ELSE CM=.; 0
DROP DUMMY DUMMY2;
DATALINES;
23C 1001 121 133C
351 45C 35 471
RUN;

There are many solutions to this problem. We present one solution which illustrates the use of several string functions.After we discuss the present code, we show an alternative solution, also using string functions.

The LENGTH function 0 returns the position of the right-most nonblank character in a string. Since you read in DUMMY with list-directed INPUT and do not supply a LENGTH statement, it has a default length of 8 bytes.

The LENGTH function returns the actual number of non-blank bytes for you. For the first data value (23C),the LENGTH function returns a 3.DUMMY2 0 is created with the SUBSTR function with a starting position of 1 and a length of one less than the length of the string, which is the numeral part of the string.To check for a trailing I, you use the INDEX function ©.

This function returns the first position in the first argument (DUMMY) that contains the second argument (letter I).You don't care what position this is, but if there is no I in the string,the INDEX function returns a 0. So,any result from the INDEX function which is non-zero indicates an I in the string.Just to be careful, you also test for a C © in the data and set any measurement without an I or a C to missing 0.

Another, perhaps easier and cleaner, solution to this example which does not use the INDEX function is shown next.(We presented the previous example to demonstrate a use of the INDEX function.)These examples show that with the large variety of string functions available,there are many ways to solve problems such as this one.

Example

DATA CONVERT?
INPUT DUMMY $ g$;
LENGTH LAST $ 1;
LENGTH=LENGTH (DUMMY); O
LAST=SUBSTR {DUMMY,LENGTH,1); €>
FIRST=SUBSTR (DUMMY,1,LENGTH-1); ©
IF LAST EQ 'I' THEN CM=2.54*INPUT (FIRST,5.); 0
ELSE IF LAST EQ 'C' THEN CM=INPUT (FIRST,5.);
ELSE CM=.;
KEEP CM?
DATALINES;
23C 1001 121 133C
351 45C 35 471
RUN;

First,you read in the data value and store it as a character value in DUMMY.Next, you use the LENGTH function 0 to find the number of non-blank characters in the measurement.Statement © assigns a value to the variable LAST equal to the rightmost byte of the string in DUMMY.(The LENGTH statement sets the length of LAST to 1 byte.)To separate the "number part" from the trailing character, you use the SUBSTR function in statement ©.

This statement creates FIRST and assigns to it a value equal to all but the last byte of the string in DUMMY.It is then straightforward to test if the trailing letter is an I or a C, and perform a conversion or not.The INPUT functions 4,5 are used to do the character-to-numeric conversions.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics