Dear
I need to extract numeric value from the data below. Please suggest. Thank you
output needed;
1
1
2
2
4
4
5
10
50
data one;
input a $1-13;
datalines;
1
1+
2
2+
10
4
4+
normal
50
abnormal
many negative
5<
;
COMPRESS() with the 'kd' option to keep digits.
data want;
set have;
want = input(compress(variable, , 'kd'), 8.);
run;
@knveraraju91 wrote:
Dear
I need to extract numeric value from the data below. Please suggest. Thank you
output needed;
1
1
2
2
4
4
5
10
50
data one; input a $1-13; datalines; 1 1+ 2 2+ 10 4 4+ normal 50 abnormal many negative 5< ;
Alternatively, you can use perl regular expressions as below
data one;
input a $1-13;
datalines;
1
1+
2
2+
10
4
4+
normal
50
abnormal
many negative
5<
;
data want;
set one;
want=prxchange('s/\D//oi',-1,a);
if want ne '';
run;
I would use the COMPRESS function to get rid of the "<", and then use the TRAILSGN (trailing sign) informat:
data want;
set one;
n=input(compress(a,"<"),?? trailsgn.);
if not missing(n);
run;
The "??" suppresses log warnings about invalid values.
Here a way using an Informat with a Regular Expression. I've also included the values with no digits in it in case you also want to convert these to some number. If not then simply remove these values from the informat and add a selection to your data step like:
if missing(want_var) then delete;
proc format;
invalue toDigits(default=20 upcase)
'NORMAL' = 100
'ABNORMAL' = 101
'MANY NEGATIVE' = 102
's/\D*(\d+)\D*/$1/' (regexpe) = _same_
other=_same_
;
run;
/* use the informat to directly read the data (want_var1)
or to convert a string from another variable (want_var2)
*/
data sample;
input have_var $1-13 @1 want_var1 toDigits.;
want_var2=input(have_var,toDigits.);
datalines;
1
1+
2
2+
10
4
4+
normal
50
abnormal
many negative
5<
;
proc print;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.