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.
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:
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;
It seems like didn't apply for the last one.
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?
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;
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:
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.