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

Hello,

 

I have a challenging question.  I have dataset shown below.  I would like to extract the continuous numbers from all kinds of different text formats.  In the end, I need the numbers without any spaces and lines.   I am wondering if there is a way to do that.  Thank you.

data datain;

format name $ 500.;

input name &;

cards;

CAR ID 5648MA

0009463

000-4721

  6325IL

PAUL__0065

0435KK-GGGG 1

;

run;

 

So, the following is results I am looking for:

5648,

0009463,

0004721,

6325,

0065,

0435.

1 ACCEPTED SOLUTION

Accepted Solutions
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @ybz12003 

 

Here is a modified version of the function provided by @ed_sas_member . I modified your input to test it with more complicated input including several occurrences of single-digit or multi-digit groups.

 

 

data datain;
infile cards truncover;
input name $char100.;
cards;
CAR ID 5648MA
0009463
000-4721
1  6325IL 34 2 
PAUL__0065
1 2 3 45 abc
0435KK-GGGG 1
12
test_1_4_ 9 more
;
run;

data want; set datain;
	length num $100;
	num = compress(prxchange('s/\b\d\b//',-1,prxchange('s/[^\d]/ /',-1,name)));
run;

 

result:

 

 

num.gif

 

 

 

 

View solution in original post

6 REPLIES 6
ed_sas_member
Meteorite | Level 14

Hi @ybz12003 

What do you mean by continuous number ?

Do you mean extract all digits?

 

Please test:

data want;
	set datain;
	name_d = compress(name,,"dK");
run;
ybz12003
Rhodochrosite | Level 12

It seems like didn't apply for the last one.

ed_sas_member
Meteorite | Level 14

Hi @ybz12003 

 

Please try this:

data want;
	set datain;
	name_d  = prxchange('s/\b\d\b//',-1,prxchange('s/[^\d\s]//',-1,name));
run;

Capture d’écran 2020-02-10 à 17.03.07.png

 

Tom
Super User Tom
Super User

Please provide what you want out from each of those example values.  Do you want to create a number from each string?  multiple numbers?  What if there are more than 15 digits in a row such that SAS cannot store it as a number?

novinosrin
Tourmaline | Level 20

Not recommending nor ideal, but good and fun to experiment 

 

data datain;

format name $ 500.;

input name &;

cards;
CAR ID 5648MA
0009463
000-4721
  6325IL
PAUL__0065
0435KK-GGGG 1
;

data want;
	set datain;
	_n_ = countw(name,'-','kd');
	length want $10;
	do _n_=1 to _n_;
	 want=compress(scan(name,_n_,'-','kd'),,'kd');
	 if length(want)>1 then output;
	end;
run;
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @ybz12003 

 

Here is a modified version of the function provided by @ed_sas_member . I modified your input to test it with more complicated input including several occurrences of single-digit or multi-digit groups.

 

 

data datain;
infile cards truncover;
input name $char100.;
cards;
CAR ID 5648MA
0009463
000-4721
1  6325IL 34 2 
PAUL__0065
1 2 3 45 abc
0435KK-GGGG 1
12
test_1_4_ 9 more
;
run;

data want; set datain;
	length num $100;
	num = compress(prxchange('s/\b\d\b//',-1,prxchange('s/[^\d]/ /',-1,name)));
run;

 

result:

 

 

num.gif

 

 

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1079 views
  • 6 likes
  • 5 in conversation