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

I want to extract the first numeric substring that shows up in a character variable. For example, 

data have;
 	infile datalines delimiter = "," missover;
	input string $;
	datalines;
	abc 12345 &*ag21
	bld#5432 
	8642 12 asd
	kmdsf 657
run;

data want;
	length string $30. substring $30.;
	infile datalines dsd dlm='|' truncover ;
	input string $ substring $;
	datalines;
	abc 12345 &*ag21|12345
	bld#5432|5432
	8642 12 asd|8642
	kmdsf 657|657
run;

Is there a good way to do this in a data step? 

1 ACCEPTED SOLUTION

Accepted Solutions
AMSAS
SAS Super FREQ

You want to look at PRXPOSN Function - Extracting First and Last Names 

data have;
 	infile datalines delimiter = "," missover;
	input string $;
	datalines;
	abc 12345 &*ag21
	bld#5432 
	8642 12 asd
	kmdsf 657
run;

data results ;
	length string $30. substring $30.;
	infile datalines dsd dlm='|' truncover ;
	input string $ substring $;
	datalines;
	abc 12345 &*ag21|12345
	bld#5432|5432
	8642 12 asd|8642
	kmdsf 657|657
run;

data wamt ;
	retain patternID ;
	set have ;
	if _n_=1 then do ;
		patternID=prxparse('/([0-9]+)/') ;
	end ;
	found=prxmatch(patternID,string) ;
	want=prxposn(patternID,1,string) ;
	put string= found= want= ;

run ;

View solution in original post

1 REPLY 1
AMSAS
SAS Super FREQ

You want to look at PRXPOSN Function - Extracting First and Last Names 

data have;
 	infile datalines delimiter = "," missover;
	input string $;
	datalines;
	abc 12345 &*ag21
	bld#5432 
	8642 12 asd
	kmdsf 657
run;

data results ;
	length string $30. substring $30.;
	infile datalines dsd dlm='|' truncover ;
	input string $ substring $;
	datalines;
	abc 12345 &*ag21|12345
	bld#5432|5432
	8642 12 asd|8642
	kmdsf 657|657
run;

data wamt ;
	retain patternID ;
	set have ;
	if _n_=1 then do ;
		patternID=prxparse('/([0-9]+)/') ;
	end ;
	found=prxmatch(patternID,string) ;
	want=prxposn(patternID,1,string) ;
	put string= found= want= ;

run ;

SAS Innovate 2025: Call for Content

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!

Submit your idea!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1 reply
  • 320 views
  • 1 like
  • 2 in conversation