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

Good day

 

I want to extract percentage values from the first column, i have highlighted the second column how i will like to output the values. 

PLASMA CELLS= 0.13%  OF LEUCOCYTES.

0.13%

MALIGNANT PLASMA CELLS = 0.049%

0.049%

0.47%

0.47%

OF WHICH =91% HAVE A NEOPLASTIC PHENOTYPE

91%

1 ACCEPTED SOLUTION

Accepted Solutions
AMSAS
SAS Super FREQ

Go check out the documentation on Perl Regular Expressions, then try this:

/* Set up test data */
data got ;
	infile cards ;
	input string $60. ;
cards;
1234567890123456789012345678901234567890
PLASMA CELLS= 0.13%  OF LEUCOCYTES.
MALIGNANT PLASMA CELLS = 0.049%
0.47%
OF WHICH =91% HAVE A NEOPLASTIC 14.2% PHENOTYPE
;
run ;

data want ;
	/* Create the regular expression 
	   This looks for 1 or 2 numerics, followed by a decimal point,
	   followed by 0-3 numerics and a percent sign e.g. 
       10.001%, 1.1%
	*/
	if _n_=1 then do ;
		retain regExpID ;
		regExpID=prxparse('/([0-9]{1,2}\.{0,1}[0-9]{0,3}%)/') ;
	end ;
	set got ;
	/* see if there's a match in the string*/
	position=prxmatch(regExpID,string) ;
	put position= string= ;
	/* We have a match */
	do while (position); 
		/* extract the value */
		percent=prxposn(regExpID,1,string) ;
		output ;
		put percent= ;
		next=length(percent)+position ;
		string=substr(string,next) ;
		/* see if there is another match */
		position=prxmatch(regExpID,string) ;	
		put string= ;
	end ;
run  ;

View solution in original post

6 REPLIES 6
andreas_lds
Jade | Level 19

Looks like a great job for a regular expression. Is there always only one percentage value in each string?

Dinkepile
Obsidian | Level 7
In some theres more than one percentage but I thought if I can get this one
then maybe I can be able to incorporate the other.

andreas_lds
Jade | Level 19

And what do you expect as result, if there is more than one value to keep?

AMSAS
SAS Super FREQ

Go check out the documentation on Perl Regular Expressions, then try this:

/* Set up test data */
data got ;
	infile cards ;
	input string $60. ;
cards;
1234567890123456789012345678901234567890
PLASMA CELLS= 0.13%  OF LEUCOCYTES.
MALIGNANT PLASMA CELLS = 0.049%
0.47%
OF WHICH =91% HAVE A NEOPLASTIC 14.2% PHENOTYPE
;
run ;

data want ;
	/* Create the regular expression 
	   This looks for 1 or 2 numerics, followed by a decimal point,
	   followed by 0-3 numerics and a percent sign e.g. 
       10.001%, 1.1%
	*/
	if _n_=1 then do ;
		retain regExpID ;
		regExpID=prxparse('/([0-9]{1,2}\.{0,1}[0-9]{0,3}%)/') ;
	end ;
	set got ;
	/* see if there's a match in the string*/
	position=prxmatch(regExpID,string) ;
	put position= string= ;
	/* We have a match */
	do while (position); 
		/* extract the value */
		percent=prxposn(regExpID,1,string) ;
		output ;
		put percent= ;
		next=length(percent)+position ;
		string=substr(string,next) ;
		/* see if there is another match */
		position=prxmatch(regExpID,string) ;	
		put string= ;
	end ;
run  ;
Dinkepile
Obsidian | Level 7
Thank you so much. The functions used above are new knowledge added. Truly appreciate
Ksharp
Super User
data got ;
	infile cards ;
	input string $60. ;
cards;
1234567890123456789012345678901234567890
PLASMA CELLS= 0.13%  OF LEUCOCYTES.
MALIGNANT PLASMA CELLS = 0.049%
0.47%
OF WHICH =91% HAVE A NEOPLASTIC 14.2% PHENOTYPE
;
run ;

data want;
 set got;
 pid=prxparse('/[\d\.]+%/');
 s=1;e=length(string);
 call prxnext(pid,s,e,string,p,l);
 do while(p>0);
  want=substr(string,p,l);output;
  call prxnext(pid,s,e,string,p,l);
 end;
 drop pid s e p l;
 run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 6 replies
  • 980 views
  • 3 likes
  • 4 in conversation