BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi All,

I have a variable whose values are both numeric and charaacters.e.g., 14.2 MG/DL, 19.2 %, no result 12.13 mmol/l, etc... its a mixed of different units r values. My question is 'Is it possible to read only the numeric portion of these values ie. 14.2, 19.2 (if it is text mark as .) 12.13, etc....Will this work with substring & scan function.

It would b helpful if I can have the solution ASAP.
Thanks in advance.
Priya
2 REPLIES 2
deleted_user
Not applicable
While this question is not related to "Mathematical Optimization and Operations Research with SAS" and while there might be more elegant ways to solve your challenge you might want to use a combination of existing SAS functions to tackle it.
I understand that your orginial variable is a character string containing alphanumeric symbols. Your goal is to read the numeric part and convert it to a numeric variable. Your digit symbol is ".".
The following example might illustrate what you are looking for:
data test;
input string $ 1-22;
length num 8;
digit1=scan(string,1,".");
i=anydigit(digit1,1);
j=anydigit(digit1,-23);
str1=trim(left(substr(digit1,i,j)));
digit2=scan(string,-1,".");
i=anydigit(digit2,1);
j=anydigit(digit2,-23);
str2=trim(left(substr(digit2,i,j)));
all=compress(str1||"."||str2);
num=all;
keep string num;
cards;
14.2 MG/DL
19.2 %
no result 12.13 mmol/l
run;
deleted_user
Not applicable
if you can use a data step, put the variable into the _infile_ buffer and then you have all the capabilities and flexibility of "input" statements to parse the text.
A paper given on this technique in an earlier SUGI "More _Infile_ Magic" http://www2.sas.com/proceedings/sugi28/086-28.pdf.

PeterC

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 837 views
  • 0 likes
  • 1 in conversation