BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

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<
;


 

 

4 REPLIES 4
Reeza
Super User

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<
;


 

 


 

Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
s_lassen
Meteorite | Level 14

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.

Patrick
Opal | Level 21

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;

Patrick_1-1591166623442.png

 

 

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 3768 views
  • 5 likes
  • 5 in conversation