Reputation: 1756
My raw data is in the form
Var
12 A+
14 A+
AB+ 19
AB:20
20
25
27 New
I want to extract the numeric portion of it only.
Can anybody please help me how to process this data in sas.
Thank you in advance. Rgds.
Upvotes: 2
Views: 24244
Reputation: 1696
You could use the COMPRESS function, which takes the form
COMPRESS(<source><, chars><, modifiers>)
Update: There are many ways to achieve this. As per their comments, RWill and Keith provide the best solutions:
var2=input(compress(var,compress(var,,"d")),best.);
or even better:
var2=input(compress(var,,"kd"),best.);
Upvotes: 6
Reputation: 1511
Just as @itzy mentioned above, Perl regular expression will do with ease:
var2=prxchange("s/[^0-9]//",-1,var);
This will remove all non-numerical characters. In this statement, 's/' begins a string, [^0-9] means all non-numerical characters. -1 defines an until-end match.
Upvotes: 3
Reputation: 11765
I would use the regular expression function built in to SAS. Start by reading in the whole line as a character variable, and then use prxmatch
or one of the other regular expression functions to extract just the numeric component using the \d
wildcard.
Upvotes: 0