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:
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.
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.
SAS Programming Related Tutorials
|SASS (Syntactically Awesome Style sheets) Tutorial||R Programming language Tutorial|
SAS Programming Related Interview Questions
|Logistics Interview Questions||SAS Programming,SQL server Interview Questions|
|Clinical SAS Interview Questions||SASS (Syntactically Awesome Style sheets) Interview Questions|
|SQL Server Analysis Services (SSAS) Interview Questions||R Programming language Interview Questions|
|SAS DI Interview Questions||Advanced SAS Interview Questions|
|Base Sas Interview Questions||SAS Macro Interview Questions|
|Clinical Data Management Interview Questions|
Sas Programming Tutorial
Input And Infile
Set, Merge, And Update
Table Lookup Tools
Proc Means And Proc Uimivariate
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.