In a dataset there is a varible list where numeric as well as character values are there, how can I get only numeric observations from it in sas. example dataline are here:in only one variable list VAR1 there are values e.g:
variable name:
VAR
................
observations:
123_854
xyz
456_672
abc
789_458
frf
233_562
gth
....
qxd
345_672
I have to extract only that numeric values e.g(233_562 etc) from that whole dataline, How can I get that.
edited. Previously it was solved
Hi @annypanny
So you can do this:
data want;
set have;
if prxmatch('/\d+\s*$/',VAR1)>0 then delete;
run;
proc print;
Best,
@annypanny Welcome to the SAS Community 🙂
Are these eight separate variables or one variable with eight values in it?
there is one variable and there are many values, I have just written eight values here. Thanks
Are these values stored in a single variable, or in separate variables within a given observation?
Does that mean you want to get rid of the commas as well? If so
data _null_;
var1 = '123, xyz, 456, abc, 789, frf, 233, gth';
var2 = compbl(compress(var1, '', 'ka'));
put var2=;
run;
Hi @annypanny
I am a bit confused:
are our input data like this:
data have;
var1 = '123, xyz, 456, abc, 789, frf, 233, gth';
run;
or like this?
data have;
input VAR1 $;
datalines;
123
xyz
456
abc
789
frf
233
gth
;
run;
In the second case, you can use the following code to output only observation containing numbers:
data want;
set have;
if prxmatch('/\d+\s*$/',VAR1)>0 then output;
run;
Best,
Hi @annypanny
So you can do this:
data want;
set have;
if prxmatch('/\d+\s*$/',VAR1)>0 then delete;
run;
proc print;
Best,
Awesome!
You're welcome @annypanny
and how we can do the vice versa i.e., how to extract a only numeric character which can also contain special characters like "_". example 1234_4567 in the above question
this act as a solution for me thanks
The number of values does not matter. See this
data _null_;
var1 = '123, xyz, 456, abc, 789, frf, 233, gth,
123, xyz, 456, abc, 789, frf, 233, gth,
123, xyz, 456, abc, 789, frf, 233, gth,
123, xyz, 456, abc, 789, frf, 233, gth,
123, xyz, 456, abc, 789, frf, 233, gth';
var2 = compbl(compress(var1, '', 'ka'));
put var2;
run;
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.