BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
annypanny
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @annypanny 

 

So you can do this:

data want;
	set have;
	if prxmatch('/\d+\s*$/',VAR1)>0 then delete;
run;
proc print;

Best,

View solution in original post

16 REPLIES 16
PeterClemmensen
Tourmaline | Level 20

@annypanny Welcome to the SAS Community 🙂

 

Are these eight separate variables or one variable with eight values in it?

annypanny
Quartz | Level 8

there is one variable and there are many values, I have just written eight values here. Thanks

annypanny
Quartz | Level 8
there is one variable and there are many values, I have just written eight values here. Thanks
PeterClemmensen
Tourmaline | Level 20

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;
annypanny
Quartz | Level 8
what if VAR1 has more than thousand values not eight values, then what can I do? Thanks in advance
ed_sas_member
Meteorite | Level 14

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,

annypanny
Quartz | Level 8
the second one is the case and I want to extract only character values.
ed_sas_member
Meteorite | Level 14

Hi @annypanny 

 

So you can do this:

data want;
	set have;
	if prxmatch('/\d+\s*$/',VAR1)>0 then delete;
run;
proc print;

Best,

annypanny
Quartz | Level 8

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

PeterClemmensen
Tourmaline | Level 20

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 16 replies
  • 5051 views
  • 5 likes
  • 4 in conversation